1
2
3 package org.virtualmock.aspect.aspectwerkz;
4
5 import org.apache.log4j.Logger;
6 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
7 import org.codehaus.aspectwerkz.joinpoint.MethodJoinPoint;
8 import org.codehaus.aspectwerkz.xmldef.advice.AroundAdvice;
9 import org.virtualmock.aspect.AspectUtils;
10 import org.virtualmock.call.CallManager;
11 import org.virtualmock.call.RecordedCall;
12 import org.virtualmock.call.Signature;
13 import org.virtualmock.util.PrimitiveUtils;
14 import org.virtualmock.util.Toolbox;
15
16
17 /***
18 * This is the advice which intercepts all caller-side method calls which are
19 * made from the unit test class to the class under test (except those which
20 * are excluded through the pointcuts). It then 'records' it as a mock call,
21 * which will be played back later by the MethodInterceptorAdvice.
22 *
23 * @author Chad Woolley
24 * @version $Revision: 1.22 $
25 */
26 public class UnitTestInterceptorAdvice extends AroundAdvice {
27 /*** Log4j logger. */
28 private static Logger logger =
29 Logger.getLogger(UnitTestInterceptorAdvice.class.getName());
30 private static AspectwerkzAspectUtils aspectwerkzAspectUtils =
31 new AspectwerkzAspectUtils(new AspectUtils());
32
33 /***
34 * The template method from the superclass which contains all the logic for
35 * this advice.
36 *
37 * @param joinPoint the JoinPoint passed by AspectWerkz
38 *
39 * @return DOCUMENT ME! (Method Return)
40 *
41 * @throws Throwable Throwable thrown by the method which is being advised
42 *
43 * @todo The code in this class related to primitives is a workaround for
44 * an AspectWerkz problem with handling null return values for
45 * primitive return types
46 */
47 public Object execute(final JoinPoint joinPoint) throws Throwable {
48 if (logger.isDebugEnabled()) {
49 logger.debug("Entering execute method");
50 }
51
52 MethodJoinPoint methodJoinPoint = (MethodJoinPoint) joinPoint;
53
54 Signature signature =
55 aspectwerkzAspectUtils.getSignatureFromMethodJoinPoint(methodJoinPoint);
56
57 CallManager callManager = Toolbox.getCallManager();
58
59 if (!callManager.isRecordPhase()) {
60
61 if (logger.isDebugEnabled()) {
62 logger.debug("Not in record phase, letting method proceed");
63 logger.debug("Proceeding with method signature = " + signature);
64 }
65
66 return joinPoint.proceed();
67 }
68
69 if (!callManager.isClassMocked(signature.getClassName())) {
70
71 if (logger.isDebugEnabled()) {
72 logger.debug("CLASS IS NOT BEING MOCKED, signature = "
73 + signature);
74 }
75
76 return joinPoint.proceed();
77 }
78
79 if (logger.isDebugEnabled()) {
80 logger.debug("CLASS IS BEING MOCKED, signature = " + signature);
81 }
82
83 Object[] argValues =
84 aspectwerkzAspectUtils.getArgValuesFromMethodJoinPoint(methodJoinPoint);
85
86
87 RecordedCall recordedCall =
88 (RecordedCall) callManager.recordCall(signature, argValues);
89
90 if (logger.isDebugEnabled()) {
91 logger.debug("recorded call " + recordedCall);
92 }
93
94
95
96 if (recordedCall.isReturnTypePrimitive()) {
97 Class returnType = recordedCall.getReturnType();
98 PrimitiveUtils primitiveUtils = new PrimitiveUtils();
99 Object returnValue =
100 primitiveUtils.getDefaultPrimitiveValue(returnType);
101
102 return returnValue;
103 }
104
105
106 return null;
107 }
108 }