package org.mockejb.interceptor.test;
import java.lang.reflect.*;
import java.util.LinkedList;
import java.util.List;
import org.mockejb.interceptor.*;
import junit.framework.TestCase;
public class InvocationContextTest extends TestCase {
private InvocationContext invocationContext;
private List interceptorList;
public InvocationContextTest( String name ) {
super( name );
}
public void setUp() throws Exception {
interceptorList = new LinkedList();
invocationContext = createInvocationContext( interceptorList );
}
public void testInvocation() throws Exception {
invocationContext.proceed();
assertTrue( ! invocationContext.getInterceptorIterator().hasNext() );
TestInterceptor interceptor1 = new TestInterceptor();
interceptorList.add( interceptor1 );
invocationContext.proceed();
assertTrue( interceptor1.wasInvoked() );
TestInterceptor interceptor2 = new TestInterceptor();
interceptorList.add( interceptor2 );
invocationContext.proceed();
assertTrue( interceptor1.wasInvoked() );
assertTrue( interceptor2.wasInvoked() );
invocationContext.proceed();
assertTrue( interceptor1.wasInvoked() );
assertTrue( interceptor2.wasInvoked() );
assertEquals( 0, interceptor1.getCallIndexBefore() );
assertEquals( 0, interceptor1.getCallIndexAfter() );
assertEquals( 1, interceptor2.getCallIndexBefore() );
assertEquals( 1, interceptor2.getCallIndexAfter() );
}
public void testException() throws Exception {
TestInterceptor interceptor1 = new TestInterceptor();
interceptorList.add( interceptor1 );
TestInterceptor interceptor2 = new TestInterceptor();
interceptorList.add( interceptor2 );
interceptor2.setThrowException( new RuntimeException());
try {
invocationContext.proceed();
fail( "Expected RuntimeExeption" );
}
catch( RuntimeException ex ){
}
assertTrue( interceptor1.wasInvoked() );
assertTrue( interceptor2.wasInvoked() );
assertTrue( ! invocationContext.getInterceptorIterator().hasPrevious() );
}
public void testContext() throws Exception {
invocationContext.setContext( TestInterceptor.TEST_CONTEXT, "test");
TestInterceptor interceptor1 = new TestInterceptor();
interceptorList.add( interceptor1 );
invocationContext.proceed();
assertEquals("test", interceptor1.getContext());
}
public InvocationContext createInvocationContext( List interceptorList) throws Exception {
Method ifaceMethod = TestIface.class.getMethod( "test", null );
Method objMethod = TestImpl.class.getMethod( "test", null );
InvocationContext invocationContext = new InvocationContext( interceptorList, null, ifaceMethod,
new TestImpl(), objMethod, null );
return invocationContext;
}
}