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   
9   
10  /***
11   * Tests the RecordedCallQueue class
12   *
13   * @author Chad Woolley
14   * @version $Revision: 1.2 $
15   */
16  public class RecordedCallQueueTest extends TestCase {
17      InvokedCall invokedCall1 = null;
18      InvokedCall invokedCall2 = null;
19      InvokedCall invokedCall3 = null;
20      InvokedCall invokedCallNotAdded = null;
21      RecordedCall recordedCall1 = null;
22      RecordedCall recordedCall2 = null;
23      RecordedCall recordedCall3 = null;
24      RecordedCallQueue queue = null;
25      Signature signatureA = null;
26      Signature signatureB = null;
27      Signature signatureC = null;
28      Signature signatureNotAdded = null;
29  
30      /***
31       * Test constructor
32       *
33       * @param testTitle title of the test
34       */
35      public RecordedCallQueueTest(String testTitle) {
36          super(testTitle);
37      }
38  
39      /***
40       * Hansel support
41       *
42       * @return the decorated Test
43       */
44      public static Test suite() {
45          CoverageDecorator cd =
46              new CoverageDecorator(RecordedCallQueueTest.class,
47                  new Class[] {RecordedCallQueue.class});
48          cd.setDisplayStatistics(true);
49  
50          return cd;
51      }
52  
53      /***
54       * Set up the test
55       *
56       * @throws Exception any exception thrown during setup.
57       */
58      public void setUp() throws Exception {
59          super.setUp();
60          queue = new RecordedCallQueue();
61          signatureA =
62              new Signature(String.class, "myMethod",
63                  new Class[] {String.class, Integer.class});
64  
65          // signatureB and signatureC are equal, but different objects 
66          signatureB =
67              new Signature(String.class, "myMethod2",
68                  new Class[] {String.class, Integer.class});
69          signatureC =
70              new Signature(String.class, "myMethod2",
71                  new Class[] {String.class, Integer.class});
72          signatureNotAdded =
73              new Signature(String.class, "notAddedToQueue",
74                  new Class[] {String.class, Integer.class});
75          recordedCall1 =
76              new RecordedCall(signatureA, new Object[] {"call1", new Integer(1)});
77          recordedCall2 =
78              new RecordedCall(signatureB, new Object[] {"call2", new Integer(2)});
79          recordedCall3 =
80              new RecordedCall(signatureC, new Object[] {"call2", new Integer(2)});
81          invokedCall1 =
82              new InvokedCall(signatureA, new Object[] {"call1", new Integer(1)});
83          invokedCall2 =
84              new InvokedCall(signatureB, new Object[] {"call2", new Integer(2)});
85          invokedCall3 =
86              new InvokedCall(signatureC, new Object[] {"call2", new Integer(2)});
87          invokedCallNotAdded =
88              new InvokedCall(signatureNotAdded,
89                  new Object[] {"notAdded", new Integer(-1)});
90  
91          // sanity checks, call2 and call3 are different objects and are not equal. 
92          // Their signatures are different objects too, but they are equal to each other
93          assertNotSame(recordedCall2, recordedCall3);
94          assertNotSame(signatureB, signatureC);
95          assertEquals(signatureB, signatureC);
96      }
97  
98      /***
99       * Tear down the test
100      *
101      * @throws Exception any exception thrown during teardown.
102      */
103     public void tearDown() throws Exception {
104         super.tearDown();
105     }
106 
107     /***
108      * Test that the getMatchingCall method returns ordered calls when it is
109      * supposed to.
110      */
111     public void testCanGetMatchingCall() {
112         queue.add(recordedCall1);
113         queue.add(recordedCall2);
114         queue.add(recordedCall3);
115 
116         assertEquals(recordedCall1, queue.getMatchingCall(invokedCall1));
117     }
118 
119     /***
120      * Test that the getMatchingCall method returns not null when it is
121      * supposed to
122      */
123     public void testCanGetMatchingCallNotNull() {
124         queue.add(recordedCall1);
125         queue.add(recordedCall2);
126         queue.add(recordedCall3);
127 
128         // call2 matches signatureB and is not invoked
129         assertNotNull(queue.getMatchingCall(invokedCall2));
130     }
131 
132     /***
133      * Test that the getMatchingCall method returns null when it is supposed to
134      */
135     public void testCanGetMatchingCallNull() {
136         queue.add(recordedCall1);
137         queue.add(recordedCall2);
138         queue.add(recordedCall3);
139 
140         // signatureNotAdded was never added to the queue, so there are 
141         // no calls matching it in the queue
142         assertNull(queue.getMatchingCall(invokedCallNotAdded));
143     }
144 
145     /***
146      * Test that the getMatchingCall method handles correctly when no matching
147      * call is found
148      */
149     public void testCanGetMatchingCallWithNoMatchFound() {
150         queue.add(recordedCall1);
151         recordedCall1.setUnlimitedInvocations(false);
152 
153         InvokedCall invokedCall1 =
154             new InvokedCall(signatureA, new Object[] {"xxx", new Integer(99)});
155 
156         assertNull(queue.getMatchingCall(invokedCall1));
157     }
158 
159     /***
160      * Test that the getMatchingCall method handles correctly when unlimited
161      * invocations is true
162      */
163     public void testCanGetMatchingCallWithUnlimitedInvocations() {
164         queue.add(recordedCall1);
165 
166         // invoke call2
167         recordedCall1.incrementInvocationCount();
168         recordedCall1.setUnlimitedInvocations(true);
169 
170         // we can call it twice in a row, with unlimited invocations
171         assertEquals(recordedCall1, queue.getMatchingCall(invokedCall1));
172         assertEquals(recordedCall1, queue.getMatchingCall(invokedCall1));
173     }
174 
175     /***
176      * Test that the getMatchingCall method handles correctly when unlimited
177      * invocations is true and there is more than one non-matching  invoked
178      * call.
179      */
180     public void testCanGetMatchingCallWithUnlimitedInvocationsAndMultipleNonMatchingCalls() {
181         queue.add(recordedCall1);
182         queue.add(recordedCall2);
183 
184         InvokedCall invokedCall1 =
185             new InvokedCall(signatureA, new Object[] {"xxx", new Integer(99)});
186         recordedCall1.setUnlimitedInvocations(true);
187 
188         assertNull(queue.getMatchingCall(invokedCall1));
189     }
190 
191     /***
192      * Test that the getMatchingCall method handles correctly when unlimited
193      * invocations is false
194      */
195     public void testCanGetMatchingCallWithoutUnlimitedInvocations() {
196         queue.add(recordedCall1);
197 
198         // invoke call2
199         recordedCall1.incrementInvocationCount();
200         recordedCall1.setUnlimitedInvocations(false);
201 
202         assertNull(queue.getMatchingCall(invokedCall1));
203     }
204 
205     /***
206      * Test the getMatchingCall method
207      */
208     public void testCanGetNextMatchingUninvokedCall() {
209         queue.add(recordedCall1);
210         queue.add(recordedCall2);
211         queue.add(recordedCall3);
212 
213         // call2 matches signatureB and is not invoked
214         assertEquals(recordedCall2, queue.getMatchingCall(invokedCall2));
215 
216         // invoke call2
217         recordedCall2.incrementInvocationCount();
218 
219         // now call2 was invoked, so it isn't the next one found (call 3 is)...
220         assertNotSame(recordedCall2, queue.getMatchingCall(invokedCall2));
221 
222         // ...but call3 also matches signatureB and it is uninvoked, so it is found
223         assertEquals(recordedCall3, queue.getMatchingCall(invokedCall2));
224 
225         // invoke call3
226         recordedCall3.incrementInvocationCount();
227 
228         // no more uninvoked calls which match, should get null
229         assertEquals(null, queue.getMatchingCall(invokedCall2));
230     }
231 
232     /***
233      * Test getting uninvoked calls
234      */
235     public void testCanGetUninvokedCalls() {
236         recordedCall1.incrementInvocationCount();
237         queue.add(recordedCall1);
238         queue.add(recordedCall2);
239 
240         RecordedCall[] uninvokedCalls = queue.getUninvokedCalls();
241 
242         assertEquals(uninvokedCalls[0], recordedCall2);
243     }
244 }