1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock;
4   
5   import junit.framework.Test;
6   import junit.framework.TestCase;
7   import org.easymock.MockControl;
8   import org.hansel.CoverageDecorator;
9   import org.virtualmock.matcher.ArgMatcher;
10  import org.virtualmock.matcher.EqualsMatcher;
11  import org.virtualmock.test.testcase.EasyMockTestCase;
12  
13  
14  /***
15   * Tests the Signature class
16   *
17   * @author Chad Woolley
18   * @version $Revision: 1.9 $
19   */
20  public class VMTest extends EasyMockTestCase {
21      VM vm = null;
22  
23      /***
24       * Set up the test
25       *
26       * @throws Exception any exception thrown during setup.
27       */
28      public void setUp() throws Exception {
29          super.setUp();
30          vm = new VM(mockCallManager, mockVerificationManager);
31      }
32  
33      /***
34       * Hansel support
35       *
36       * @return the decorated Test
37       */
38      public static Test suite() {
39          CoverageDecorator cd =
40              new CoverageDecorator(VMTest.class, new Class[] {VM.class});
41          cd.setDisplayStatistics(true);
42  
43          return cd;
44      }
45  
46      /***
47       * Tear down the test
48       *
49       * @throws Exception any exception thrown during teardown.
50       */
51      public void tearDown() throws Exception {
52          super.tearDown();
53          controlCallManager.verify();
54      }
55  
56      /***
57       * Tests the addMockClass method
58       */
59      public void testCanAddMockClass() {
60          mockCallManager.addMockClass(Object.class.getName());
61          controlCallManager.replay();
62  
63          vm.addMockClass(Object.class);
64      }
65  
66      /***
67       * Tests the various recordArgMatcher method
68       */
69      public void testCanRecordArgMatcher() {
70          // use a nice control to avoid EasyMock failing comparison on internally created arrays
71          ArgMatcher argMatcher = new EqualsMatcher();
72          ArgMatcher[] argMatchers = new ArgMatcher[] {argMatcher};
73          mockCallManager.recordArgMatchers(argMatchers);
74          controlCallManager.setDefaultMatcher(MockControl.ARRAY_MATCHER);
75          controlCallManager.replay();
76  
77          vm.recordArgMatcher(argMatcher);
78      }
79  
80      /***
81       * Tests the various recordArgMatcher method
82       */
83      public void testCanRecordArgMatcherThreeArgs() {
84          // use a nice control to avoid EasyMock failing comparison on internally created arrays
85          ArgMatcher argMatcher = new EqualsMatcher();
86          ArgMatcher[] argMatchers =
87              new ArgMatcher[] {argMatcher, argMatcher, argMatcher};
88          mockCallManager.recordArgMatchers(argMatchers);
89          controlCallManager.setDefaultMatcher(MockControl.ARRAY_MATCHER);
90          controlCallManager.replay();
91  
92          vm.recordArgMatchers(argMatcher, argMatcher, argMatcher);
93      }
94  
95      /***
96       * Tests the various recordArgMatcher method
97       */
98      public void testCanRecordArgMatcherTwoArgs() {
99          // use a nice control to avoid EasyMock failing comparison on internally created arrays
100         ArgMatcher argMatcher = new EqualsMatcher();
101         ArgMatcher[] argMatchers = new ArgMatcher[] {argMatcher, argMatcher};
102         mockCallManager.recordArgMatchers(argMatchers);
103         controlCallManager.setDefaultMatcher(MockControl.ARRAY_MATCHER);
104         controlCallManager.replay();
105 
106         vm.recordArgMatchers(argMatcher, argMatcher);
107     }
108 
109     /***
110      * Tests the various recordArgMatcher method
111      */
112     public void testCanRecordArgMatchers() {
113         // use a nice control to avoid EasyMock failing comparison on internally created arrays
114         ArgMatcher argMatcher = new EqualsMatcher();
115         ArgMatcher[] argMatchers = new ArgMatcher[] {argMatcher};
116         mockCallManager.recordArgMatchers(argMatchers);
117         controlCallManager.setDefaultMatcher(MockControl.ARRAY_MATCHER);
118         controlCallManager.replay();
119 
120         vm.recordArgMatchers(argMatchers);
121     }
122 
123     /***
124      * Tests the recordException method
125      */
126     public void testCanRecordException() {
127         Throwable throwable = new Throwable();
128         mockCallManager.recordThrowableForLastCall(throwable);
129         controlCallManager.replay();
130 
131         vm.recordException(throwable);
132     }
133 
134     /***
135      * Tests the recordReturnValue method with a boolean
136      */
137     public void testCanRecordReturnValueBoolean() {
138         boolean b = false;
139         Boolean booleanValue = new Boolean(b);
140         mockCallManager.recordReturnValueForLastCall(booleanValue);
141         controlCallManager.replay();
142 
143         vm.recordReturnValue(b);
144     }
145 
146     /***
147      * Tests the recordReturnValue method with a byte
148      */
149     public void testCanRecordReturnValueByte() {
150         byte b = 0;
151         Byte byteValue = new Byte(b);
152         mockCallManager.recordReturnValueForLastCall(byteValue);
153         controlCallManager.replay();
154 
155         vm.recordReturnValue(b);
156     }
157 
158     /***
159      * Tests the recordReturnValue method with a char
160      */
161     public void testCanRecordReturnValueChar() {
162         char c = '\u0000';
163         Character charValue = new Character(c);
164         mockCallManager.recordReturnValueForLastCall(charValue);
165         controlCallManager.replay();
166 
167         vm.recordReturnValue(c);
168     }
169 
170     /***
171      * Tests the recordReturnValue method with a double
172      */
173     public void testCanRecordReturnValueDouble() {
174         double d = 0.0d;
175         Double doubleValue = new Double(d);
176         mockCallManager.recordReturnValueForLastCall(doubleValue);
177         controlCallManager.replay();
178 
179         vm.recordReturnValue(d);
180     }
181 
182     /***
183      * Tests the recordReturnValue method with a float
184      */
185     public void testCanRecordReturnValueFloat() {
186         float f = 0.0f;
187         Float floatValue = new Float(f);
188         mockCallManager.recordReturnValueForLastCall(floatValue);
189         controlCallManager.replay();
190 
191         vm.recordReturnValue(f);
192     }
193 
194     /***
195      * Tests the recordReturnValue method with an int
196      */
197     public void testCanRecordReturnValueInt() {
198         int i = 1;
199         Integer integerValue = new Integer(i);
200         mockCallManager.recordReturnValueForLastCall(integerValue);
201         controlCallManager.replay();
202 
203         vm.recordReturnValue(i);
204     }
205 
206     /***
207      * Tests the recordReturnValue method with a long
208      */
209     public void testCanRecordReturnValueLong() {
210         long l = 0L;
211         Long longValue = new Long(l);
212         mockCallManager.recordReturnValueForLastCall(longValue);
213         controlCallManager.replay();
214 
215         vm.recordReturnValue(l);
216     }
217 
218     /***
219      * Tests the recordReturnValue method with an Object
220      */
221     public void testCanRecordReturnValueObject() {
222         Object object = new Object();
223         mockCallManager.recordReturnValueForLastCall(object);
224         controlCallManager.replay();
225 
226         vm.recordReturnValue(object);
227     }
228 
229     /***
230      * Tests the recordReturnValue method with a short
231      */
232     public void testCanRecordReturnValueShort() {
233         short s = 0;
234         Short shortValue = new Short(s);
235         mockCallManager.recordReturnValueForLastCall(shortValue);
236         controlCallManager.replay();
237 
238         vm.recordReturnValue(s);
239     }
240 
241     /***
242      * Test the TestCase getters and setters
243      *
244      * @throws Exception
245      */
246     public void testCanSetAndGetTestCase() throws Exception {
247         mockCallManager.setTestCase(this);
248         mockCallManager.getTestCase();
249         controlCallManager.setReturnValue(this);
250         controlCallManager.replay();
251 
252         vm.setTestCase(this);
253 
254         TestCase testCase = vm.getTestCase();
255         assertEquals(this, testCase);
256     }
257 
258     /***
259      * Tests the playbackCalls method
260      */
261     public void testPlaybackCalls() {
262         mockCallManager.setPhase(Constants.PLAYBACK_PHASE);
263         controlCallManager.replay();
264 
265         vm.playbackCalls();
266     }
267 
268     /***
269      * Tests the verify method
270      */
271     public void testVerify() {
272         mockCallManager.setPhase(Constants.VERIFY_PHASE);
273         mockVerificationManager.verify();
274 
275         controlCallManager.replay();
276         controlVerificationManager.replay();
277 
278         vm.verify();
279 
280         controlVerificationManager.verify();
281     }
282 }