001: package org.mockejb.interceptor;
002:
003: import java.util.*;
004:
005: /**
006: * Stores the information about all calls to the target object
007: * in the list. Clients can examine the list to see the parameters of the calls.
008: * Example of usage:
009: * <code><pre>
010: * InvocationRecorder recorder = new InvocationRecorder();
011: * aspectSystem.add( new ClassPointcut( SampleHelper.class, false), recorder);
012: * InvocationContext dummyMethodInvocation = recorder.findByTargetMethod( "dummyMethod");
013: * assertNotNull(dummyMethodInvocation);
014: * </pre></code>
015: *
016: * @author Alexander Ananiev
017: */
018: public class InvocationRecorder implements Interceptor {
019:
020: private List invocationContextList = new ArrayList();
021:
022: public void intercept(InvocationContext invocationContext)
023: throws Exception {
024:
025: Exception thrownException = null;
026: Object returnObj = null;
027:
028: try {
029: invocationContext.proceed();
030: } finally {
031:
032: invocationContextList.add(invocationContext);
033: }
034:
035: }
036:
037: /**
038: * Returns the list of all invocations that happened since the recorder
039: * was added.
040: * @return list of invocations
041: */
042: public List getMethodInvocationList() {
043: return invocationContextList;
044: }
045:
046: /**
047: * Returns true is it has at least one record
048: *
049: * @return true if at least one recording was made
050: */
051: public boolean hasRecords() {
052: return !invocationContextList.isEmpty();
053: }
054:
055: /**
056: * Clears all currently stored invocations
057: */
058: public void clear() {
059: invocationContextList.clear();
060: }
061:
062: /**
063: * Finds the first invocation for a given target method pattern.
064: * @param methodPattern regexp pattern to run against the string representation of a method
065: * @return invocation context or null if method not found
066: *
067: */
068: public InvocationContext findByTargetMethod(String methodPattern) {
069:
070: RegexpWrapper regexp = new RegexpWrapper(methodPattern);
071:
072: Iterator i = invocationContextList.iterator();
073:
074: while (i.hasNext()) {
075: InvocationContext invocation = (InvocationContext) i.next();
076: if (regexp.containedInString(invocation.getTargetMethod()
077: .toString()))
078: return invocation;
079: }
080:
081: return null;
082: }
083:
084: /**
085: * @deprecated Use findByProxyMethod instead
086: * @param methodPattern regexp pattern to run against the string representation of a method
087: * @return invocation context or null if method not found
088: */
089: public InvocationContext findByInterceptedMethod(
090: String methodPattern) {
091: return findByProxyMethod(methodPattern);
092: }
093:
094: /**
095: * Finds the first invocation for a given proxy method pattern.
096: * @param methodPattern regexp pattern to run against the string representation of a method
097: * @return invocation context or null if method not found
098: *
099: */
100: public InvocationContext findByProxyMethod(String methodPattern) {
101:
102: RegexpWrapper regexp = new RegexpWrapper(methodPattern);
103: Iterator i = invocationContextList.iterator();
104:
105: while (i.hasNext()) {
106: InvocationContext invocation = (InvocationContext) i.next();
107: if (regexp.containedInString(invocation.getProxyMethod()
108: .toString()))
109: return invocation;
110: }
111:
112: return null;
113: }
114:
115: public String toString() {
116: return invocationContextList.toString();
117: }
118:
119: }
|