View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.aspect;
4   
5   import java.util.Hashtable;
6   import org.codehaus.aspectwerkz.regexp.ClassPattern;
7   import org.codehaus.aspectwerkz.regexp.Pattern;
8   import org.virtualmock.call.InvokedCall;
9   import org.virtualmock.call.RecordedCall;
10  import org.virtualmock.util.Toolbox;
11  
12  
13  /***
14   * This class consists of utility methods which have been abstracted out of the
15   * Advice class.  This makes them easier to debug and unit test.  It also acts
16   * as  an encapsulation layer between the advices and the rest of the class.
17   * This is the only class which deals with  aspect-specific classes, such as
18   * JoinPoints.
19   *
20   * @author Chad Woolley
21   * @version $Revision: 1.15 $
22   */
23  public class AspectUtils {
24      /***
25       * Returns true if the specified class should be excluded from VirtualMock
26       * processing.
27       *
28       * @param targetClass the class which will be checked to see if it should
29       *        be excluded
30       *
31       * @return true if the specified class should be excluded
32       *
33       * @todo hardcoded to exclude VMTest class as an example
34       */
35      public boolean isClassExcluded(Class targetClass) {
36          String targetClassName = targetClass.getName();
37  
38          String classExclusionPattern = "org.virtualmock.VMTest";
39          ClassPattern classPattern =
40              Pattern.compileClassPattern(classExclusionPattern);
41          boolean isExcluded = classPattern.matches(targetClassName);
42  
43          return isExcluded;
44      }
45  
46      /***
47       * Log4j logger
48       *
49       * @param argumentValues DOCUMENT ME! (Constructor Parameter)
50       *
51       * @return DOCUMENT ME! (Method Return)
52       */
53  
54      // Commented for Hansel
55      // private static Logger logger =
56      //    Logger.getLogger(AspectUtils.class.getName());
57  
58      /***
59       * Create dummy argument names.
60       *
61       * @param argumentValues the argument values for which the dummy argument
62       *        names will be created.
63       *
64       * @return an array of dummy argument names.
65       */
66      public String[] createArgumentNames(Object[] argumentValues) {
67          // can't do the following line in aspectwerkz like you can in AspectJ,
68          // because java throws away param names when compiling bytecode:
69          //   String[] argumentNames = 
70          //      methodJoinPoint.getMethod().getParameterNames();
71          // instead, we just create names as arg0, arg1, etc.
72          String[] argumentNames = new String[argumentValues.length];
73  
74          for (int j = 0; j < argumentValues.length; j++) {
75              StringBuffer parameterName = new StringBuffer();
76              parameterName.append("arg");
77              parameterName.append(j);
78              argumentNames[j] = parameterName.toString();
79          }
80  
81          return argumentNames;
82      }
83  
84      /***
85       * Transform two arrays of matching argument types and argument values into
86       * a hashtable of corresponding key/value pairs, with the argumentType as
87       * the key.
88       *
89       * @param argumentTypes an array of argument types
90       * @param argumentValues an array of argument values
91       *
92       * @return the argument types and values represented as a hashtable
93       */
94      public Hashtable hashArguments(Class[] argumentTypes,
95          Object[] argumentValues) {
96          Hashtable arguments = null;
97  
98          for (int i = 0; i < argumentValues.length; i++) {
99              if (argumentValues[i] != null) {
100                 if (arguments == null) {
101                     arguments = new Hashtable();
102                 }
103 
104                 arguments.put(argumentTypes[i], argumentValues[i]);
105             }
106         }
107 
108         return arguments;
109     }
110 
111     /***
112      * Performs the logic of playing back a recorded mock call.
113      *
114      * @param invokedCall the call that was invoked
115      * @param recordedCall the recorded call that matches this invoked call
116      *
117      * @return the recorded mock return type (which may be null if one was not
118      *         recorded)
119      *
120      * @throws Throwable the recorded mock exception (if one was recorded)
121      */
122     public Object playbackMockCall(InvokedCall invokedCall,
123         RecordedCall recordedCall) throws Throwable {
124         // indicate that this expected call was actually invoked by the
125         // class under test.
126         Toolbox.getCallManager().indicateCalled(invokedCall);
127 
128         // get the recorded mock exception (if specified)
129         Throwable throwable = recordedCall.getThrowable();
130 
131         // throw the exception, if one was recorded
132         if (throwable != null) {
133             // DEBUG: disabled for Hansel
134             // if (logger.isDebugEnabled()) {
135             //     logger.debug("THROWING MOCK EXCEPTION: " + throwable);
136             // }
137             throw throwable;
138         }
139 
140         // get the recorded mock return value (if specified)
141         Object returnValue = recordedCall.getReturnValue();
142 
143         // return the value, if one was recorded
144         if (returnValue != null) {
145             // DEBUG: disabled for Hansel
146             // if (logger.isDebugEnabled()) {
147             //    logger.debug("RETURNING MOCK VALUE: " + returnValue);
148             // }
149             return returnValue;
150         }
151 
152         // DEBUG: disabled for Hansel
153         //        if (logger.isDebugEnabled()) {
154         //            logger.debug("RETURNING MOCK NULL VALUE");
155         //            logger.debug("exiting playbackMockCall method");
156         //        }
157         // return null if there is no exception to throw or value to return
158         return null;
159     }
160 }