1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.call;
4   
5   import junit.framework.Test;
6   import junit.framework.TestCase;
7   import org.hansel.CoverageDecorator;
8   import org.virtualmock.call.Call;
9   import org.virtualmock.call.CallQueue;
10  import org.virtualmock.call.Signature;
11  
12  
13  /***
14   * Tests the CallQueue class
15   *
16   * @author Chad Woolley
17   * @version $Revision: 1.2 $
18   */
19  public class CallQueueTest extends TestCase {
20      Call call1 = null;
21      Call call2 = null;
22      Call call3 = null;
23      Call call3match = null;
24      CallQueue queue = null;
25      Signature signature1 = null;
26      Signature signature2 = null;
27      Signature signature3 = null;
28      Signature signature3match = null;
29      Signature signatureNotAdded = null;
30  
31      /***
32       * Hansel support
33       *
34       * @return the decorated Test
35       */
36      public static Test suite() {
37          CoverageDecorator cd =
38              new CoverageDecorator(CallQueueTest.class,
39                  new Class[] {CallQueue.class});
40          cd.setDisplayStatistics(true);
41  
42          return cd;
43      }
44  
45      /***
46       * Set up the test
47       *
48       * @throws Exception any exception thrown during setup.
49       */
50      public void setUp() throws Exception {
51          super.setUp();
52          queue = new CallQueue();
53          signature1 =
54              new Signature(String.class, "myMethod1",
55                  new Class[] {String.class, Integer.class});
56          signature2 =
57              new Signature(String.class, "myMethod2",
58                  new Class[] {String.class, Integer.class});
59          signature3 =
60              new Signature(String.class, "myMethod3",
61                  new Class[] {String.class, Integer.class});
62          signature3match =
63              new Signature(String.class, "myMethod3",
64                  new Class[] {String.class, Integer.class});
65          signatureNotAdded =
66              new Signature(String.class, "notAddedToQueue",
67                  new Class[] {String.class, Integer.class});
68          call1 = new Call(signature1, new Object[] {"call1", new Integer(1)});
69          call2 = new Call(signature2, new Object[] {"call2", new Integer(2)});
70          call3 = new Call(signature3, new Object[] {"call3", new Integer(3)});
71          call3match =
72              new Call(signature3match,
73                  new Object[] {"call3match", new Integer(4)});
74      }
75  
76      /***
77       * Tear down the test
78       *
79       * @throws Exception any exception thrown during teardown.
80       */
81      public void tearDown() throws Exception {
82          super.tearDown();
83      }
84  
85      /***
86       * Test getCalls()
87       */
88      public void testCanGetCalls() {
89          queue.add(call1);
90  
91          assertEquals(call1, queue.getCalls().get(0));
92      }
93  
94      /***
95       * Test getFirstCallWithMatchingSignature method
96       */
97      public void testCanGetFirstCallWithMatchingSignature() {
98          queue.add(call1);
99          queue.add(call2);
100 
101         assertEquals(call2,
102             queue.getFirstCallWithMatchingSignature(call2.getSignature()));
103     }
104 
105     /***
106      * Test getIndexOfNextCallWithMatchingSignature method
107      */
108     public void testCanGetIndexOfNextCallWithMatchingSignature() {
109         queue.add(call1);
110         queue.add(call2);
111         queue.add(call3);
112 
113         assertEquals(2,
114             queue.getIndexOfNextCallWithMatchingSignature(1,
115                 call3.getSignature()));
116     }
117 
118     /***
119      * Test getIndexOfNextCallWithMatchingSignature method when there are no
120      * matches
121      */
122     public void testCanGetIndexOfNextCallWithMatchingSignatureIfNoneMatch() {
123         queue.add(call1);
124         queue.add(call2);
125 
126         assertEquals(-1,
127             queue.getIndexOfNextCallWithMatchingSignature(1,
128                 call3.getSignature()));
129     }
130 
131     /***
132      * Test getNextCallWithMatchingSignature method
133      */
134     public void testCanGetNextCallWithMatchingSignature() {
135         queue.add(call1);
136         queue.add(call2);
137         queue.add(call3);
138 
139         assertEquals(call3,
140             queue.getNextCallWithMatchingSignature(1, call3.getSignature()));
141     }
142 
143     /***
144      * Test getNextCallWithMatchingSignature method when there are no matches
145      */
146     public void testCanGetNextCallWithMatchingSignatureIfNoneMatch() {
147         queue.add(call1);
148         queue.add(call2);
149 
150         assertNull(queue.getNextCallWithMatchingSignature(1,
151                 call3.getSignature()));
152     }
153 
154     /***
155      * Test the getMatchingSignatureCount method
156      */
157     public void testGetMatchingSignatureCount() {
158         queue.add(call1);
159         queue.add(call2);
160         queue.add(call2);
161 
162         assertEquals(1, queue.getMatchingSignatureCount(signature1));
163         assertEquals(2, queue.getMatchingSignatureCount(signature2));
164     }
165 
166     /***
167      * Test hasCallWithMatchingSignature method
168      */
169     public void testHasCallWithMatchingSignatureAndCallArgWorks() {
170         queue.add(call1);
171         queue.add(call2);
172 
173         assertTrue(queue.hasCallWithMatchingSignature(call2));
174     }
175 
176     /***
177      * Test hasCallWithMatchingSignature method
178      */
179     public void testHasCallWithMatchingSignatureAndSignatureArgWorks() {
180         queue.add(call1);
181         queue.add(call2);
182 
183         assertTrue(queue.hasCallWithMatchingSignature(signature2));
184         assertFalse(queue.hasCallWithMatchingSignature(signatureNotAdded));
185     }
186 
187     /***
188      * Test clearing of the queue
189      */
190     public void testQueueClear() {
191         queue.add(call1);
192         queue.add(call2);
193         queue.add(call3);
194 
195         queue.clear();
196         assertEquals(queue.size(), 0);
197     }
198 
199     /***
200      * Test queue size
201      */
202     public void testReturnsCorrectQueueSize() {
203         queue.add(call1);
204         queue.add(call2);
205         queue.add(call3);
206 
207         assertEquals(queue.size(), 3);
208     }
209 
210     /***
211      * Test setting of signature sequence and retrieval by Call Id
212      */
213     public void testReturnsNullCallIdIfNoCallsInQueue() {
214         assertEquals(null, queue.getCallByCallId(""));
215     }
216 
217     /***
218      * Test setting of signature sequence and retrieval by Call Id
219      */
220     public void testSignatureSequenceAndCallId() {
221         queue.add(call1);
222         queue.add(call2);
223         queue.add(call3);
224         queue.add(call3match);
225 
226         assertEquals(1, call1.getSignatureSequence());
227         assertEquals(1, call2.getSignatureSequence());
228         assertEquals(1, call3.getSignatureSequence());
229         assertEquals(2, call3match.getSignatureSequence());
230 
231         assertEquals(call2, queue.getCallByCallId(call2.getCallId()));
232     }
233 }