package org.mockejb.interceptor;
import java.util.*;
public class InvocationRecorder implements Interceptor {
private List invocationContextList = new ArrayList();
public void intercept( InvocationContext invocationContext ) throws Exception {
Exception thrownException = null;
Object returnObj = null;
try {
invocationContext.proceed();
}
finally {
invocationContextList.add( invocationContext );
}
}
public List getMethodInvocationList(){
return invocationContextList;
}
public boolean hasRecords(){
return ! invocationContextList.isEmpty();
}
public void clear() {
invocationContextList.clear();
}
public InvocationContext findByTargetMethod( String methodPattern ){
RegexpWrapper regexp = new RegexpWrapper( methodPattern );
Iterator i = invocationContextList.iterator();
while( i.hasNext() ){
InvocationContext invocation = (InvocationContext) i.next();
if ( regexp.containedInString( invocation.getTargetMethod().toString() ) )
return invocation;
}
return null;
}
public InvocationContext findByInterceptedMethod( String methodPattern ){
return findByProxyMethod( methodPattern );
}
public InvocationContext findByProxyMethod( String methodPattern ){
RegexpWrapper regexp = new RegexpWrapper( methodPattern );
Iterator i = invocationContextList.iterator();
while( i.hasNext() ){
InvocationContext invocation = (InvocationContext) i.next();
if ( regexp.containedInString( invocation.getProxyMethod().toString() ) )
return invocation;
}
return null;
}
public String toString( ){
return invocationContextList.toString();
}
}