1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.aspect;
4   
5   import java.security.InvalidKeyException;
6   import junit.framework.Test;
7   import junit.framework.TestCase;
8   import org.hansel.CoverageDecorator;
9   import org.virtualmock.VMTest;
10  import org.virtualmock.call.InvokedCall;
11  import org.virtualmock.call.RecordedCall;
12  import org.virtualmock.call.Signature;
13  import org.virtualmock.util.Toolbox;
14  
15  
16  /***
17   * Tests the AspectUtils class
18   *
19   * @author Chad Woolley
20   * @version $Revision: 1.4 $
21   */
22  public class AspectUtilsTest extends TestCase {
23      AspectUtils aspectUtils = null;
24      Class classType = null;
25      RecordedCall recordedCall = null;
26      Signature signature = null;
27      String className = null;
28      String methodName = null;
29      String returnValue = null;
30      Throwable throwable = null;
31      String[] argumentNames = null;
32      Class[] argumentTypes = null;
33      Object[] argumentValues = null;
34  
35      /***
36       * Set up the test
37       *
38       * @throws Exception any exception thrown during setup.
39       */
40      public void setUp() throws Exception {
41          super.setUp();
42          aspectUtils = new AspectUtils();
43          argumentNames = new String[] {"arg0", "arg1"};
44          argumentTypes = new Class[] {String.class, String.class};
45          argumentValues = new Object[] {"ARG0_VALUE", "ARG1_VALUE"};
46          classType = AspectUtils.class;
47          className = "AspectUtils";
48          methodName = "MyMethod";
49          returnValue = "RETURN_VALUE";
50          signature =
51              new Signature(returnValue.getClass(), classType, methodName,
52                  argumentTypes);
53      }
54  
55      /***
56       * Hansel support
57       *
58       * @return the decorated Test
59       */
60      public static Test suite() {
61          CoverageDecorator cd =
62              new CoverageDecorator(AspectUtilsTest.class,
63                  new Class[] {AspectUtils.class});
64          cd.setDisplayStatistics(true);
65  
66          return cd;
67      }
68  
69      /***
70       * Tear down the test
71       *
72       * @throws Exception any exception thrown during teardown.
73       */
74      public void tearDown() throws Exception {
75          super.tearDown();
76      }
77  
78      /***
79       * Test the createArgumentNames method
80       */
81      public void testCanCreateArgumentNames() {
82          String[] newArgumentNames =
83              aspectUtils.createArgumentNames(argumentValues);
84  
85          for (int i = 0; i < newArgumentNames.length; i++) {
86              assertEquals(newArgumentNames[i], argumentNames[i]);
87          }
88      }
89  
90      /***
91       * Test the hashArguments method
92       */
93      public void testCanHashArguments() {
94          aspectUtils.hashArguments(argumentTypes, argumentValues);
95      }
96  
97      /***
98       * Test that the hashArguments method accepts null argument values
99       */
100     public void testCanHashNullArguments() {
101         argumentValues[0] = null;
102         argumentValues[1] = null;
103         aspectUtils.hashArguments(argumentTypes, argumentValues);
104     }
105 
106     /***
107      * Test playing back a mock call with a exception thrown.
108      *
109      * @throws Throwable thrown by AspectUtils execute method
110      */
111     public void testCanPlaybackWithException() throws Throwable {
112         throwable = new InvalidKeyException();
113         signature = new Signature(classType, methodName, null);
114 
115         InvokedCall invokedCall = new InvokedCall(signature, null);
116         recordedCall = Toolbox.getCallManager().recordCall(signature, null);
117         Toolbox.getCallManager().recordThrowableForLastCall(throwable);
118 
119         try {
120             aspectUtils.playbackMockCall(invokedCall, recordedCall);
121             fail("Expected InvalidKeyException exception");
122         } catch (InvalidKeyException e) {
123         }
124     }
125 
126     /***
127      * Test playing back a mock call with null returned.
128      *
129      * @throws Throwable thrown by AspectUtils execute method
130      */
131     public void testCanPlaybackWithNullReturnValue() throws Throwable {
132         signature = new Signature(classType, methodName, null);
133 
134         InvokedCall invokedCall = new InvokedCall(signature, null);
135         recordedCall = Toolbox.getCallManager().recordCall(signature, null);
136 
137         Object actualReturnValue =
138             aspectUtils.playbackMockCall(invokedCall, recordedCall);
139         assertNull(actualReturnValue);
140     }
141 
142     /***
143      * Test playing back a mock call with a return value.
144      *
145      * @throws Throwable thrown by AspectUtils execute method
146      */
147     public void testCanPlaybackWithReturnValue() throws Throwable {
148         signature =
149             new Signature(returnValue.getClass(), classType, methodName, null);
150         recordedCall = Toolbox.getCallManager().recordCall(signature, null);
151 
152         InvokedCall invokedCall = new InvokedCall(signature, null);
153         Toolbox.getCallManager().recordReturnValueForLastCall(returnValue);
154 
155         Object actualReturnValue =
156             aspectUtils.playbackMockCall(invokedCall, recordedCall);
157         assertEquals(returnValue, actualReturnValue);
158     }
159 
160     /***
161      * Test that the isClassExcluded method returns false
162      */
163     public void testIsJoinPointExcludedReturnsFalse() {
164         classType = String.class;
165 
166         boolean isExcluded = aspectUtils.isClassExcluded(classType);
167         assertFalse(isExcluded);
168     }
169 
170     /***
171      * Test that the isClassExcluded method returns true
172      */
173     public void testIsJoinPointExcludedReturnsTrue() {
174         // TODO: this test commented because it fails with a noclassdef found 
175         //       error when running the unit test under AspectJ
176         // TODO: this is currently hardcoded to excluded EasyMock
177         classType = VMTest.class;
178 
179         boolean isExcluded = aspectUtils.isClassExcluded(classType);
180         assertTrue(isExcluded);
181     }
182 }