001: package org.mockejb.interceptor.test;
002:
003: import java.lang.reflect.*;
004: import java.util.LinkedList;
005: import java.util.List;
006:
007: import org.mockejb.interceptor.*;
008:
009: import junit.framework.TestCase;
010:
011: /**
012: * @author Alexander Ananiev
013: */
014: public class InvocationContextTest extends TestCase {
015:
016: private InvocationContext invocationContext;
017: private List interceptorList;
018:
019: /**
020: * Constructor for InvocationContextTest.
021: * @param name
022: */
023: public InvocationContextTest(String name) {
024: super (name);
025: }
026:
027: public void setUp() throws Exception {
028:
029: interceptorList = new LinkedList();
030:
031: invocationContext = createInvocationContext(interceptorList);
032:
033: }
034:
035: public void testInvocation() throws Exception {
036:
037: // Run w/o interceptors, make sure no errors
038: invocationContext.proceed();
039: assertTrue(!invocationContext.getInterceptorIterator()
040: .hasNext());
041:
042: // add one
043: TestInterceptor interceptor1 = new TestInterceptor();
044: interceptorList.add(interceptor1);
045:
046: invocationContext.proceed();
047: assertTrue(interceptor1.wasInvoked());
048:
049: // add two
050: TestInterceptor interceptor2 = new TestInterceptor();
051: interceptorList.add(interceptor2);
052:
053: invocationContext.proceed();
054: // make sure that they both were called
055: assertTrue(interceptor1.wasInvoked());
056: assertTrue(interceptor2.wasInvoked());
057:
058: // call again and make sure they were called again
059: invocationContext.proceed();
060: assertTrue(interceptor1.wasInvoked());
061: assertTrue(interceptor2.wasInvoked());
062:
063: // make sure they were called in the right order
064: assertEquals(0, interceptor1.getCallIndexBefore());
065: assertEquals(0, interceptor1.getCallIndexAfter());
066: assertEquals(1, interceptor2.getCallIndexBefore());
067: assertEquals(1, interceptor2.getCallIndexAfter());
068: }
069:
070: public void testException() throws Exception {
071:
072: // add one
073: TestInterceptor interceptor1 = new TestInterceptor();
074: interceptorList.add(interceptor1);
075: // add two
076: TestInterceptor interceptor2 = new TestInterceptor();
077: interceptorList.add(interceptor2);
078:
079: interceptor2.setThrowException(new RuntimeException());
080:
081: try {
082: invocationContext.proceed();
083: fail("Expected RuntimeExeption");
084: } catch (RuntimeException ex) {
085: }
086:
087: assertTrue(interceptor1.wasInvoked());
088: assertTrue(interceptor2.wasInvoked());
089: // make sure the "finally" worked
090: assertTrue(!invocationContext.getInterceptorIterator()
091: .hasPrevious());
092:
093: }
094:
095: public void testContext() throws Exception {
096:
097: invocationContext.setContext(TestInterceptor.TEST_CONTEXT,
098: "test");
099:
100: TestInterceptor interceptor1 = new TestInterceptor();
101: interceptorList.add(interceptor1);
102:
103: invocationContext.proceed();
104:
105: assertEquals("test", interceptor1.getContext());
106:
107: }
108:
109: /**
110: * Creates InvocationContext for dummy methods
111: * @param interceptorList
112: * @return
113: * @throws Exception
114: */
115: public InvocationContext createInvocationContext(
116: List interceptorList) throws Exception {
117:
118: Method ifaceMethod = TestIface.class.getMethod("test", null);
119: Method objMethod = TestImpl.class.getMethod("test", null);
120:
121: InvocationContext invocationContext = new InvocationContext(
122: interceptorList, null, ifaceMethod, new TestImpl(),
123: objMethod, null);
124:
125: return invocationContext;
126: }
127: }
|