001: package org.mockejb.interceptor.test;
002:
003: import java.lang.reflect.Method;
004:
005: import java.util.List;
006:
007: import junit.framework.TestCase;
008:
009: import org.mockejb.interceptor.*;
010:
011: /**
012: * Tests standard pointcuts supplied with MockEJB as well as
013: * basic AspectSystem functions.
014: *
015: * @author Alexander Ananiev
016: */
017: public class PointcutTest extends TestCase {
018:
019: private TestIface testProxy;
020: private InvocationContext targetInvoker;
021:
022: private AspectSystem aspectSystem;
023:
024: public void setUp() throws Exception {
025: aspectSystem = AspectSystemFactory.getAspectSystem();
026: aspectSystem.clear();
027: }
028:
029: public void tearDown() throws Exception {
030: aspectSystem.getAspectList().clear();
031: }
032:
033: // TODO: refactor to multiple methods
034: public void testClassPointcut() throws Exception {
035:
036: TestInterceptor interceptor = new TestInterceptor();
037: Object target = new TestImpl();
038:
039: // test w/o subclasses
040: aspectSystem.clear();
041: aspectSystem.add(new ClassPointcut(TestIface.class),
042: interceptor);
043:
044: Method m = TestIface.class.getMethod("test", null);
045:
046: List interceptorList = aspectSystem.findInterceptors(m, m);
047:
048: assertTrue(interceptorList.size() > 0);
049:
050: // test with subclasses
051: aspectSystem.clear();
052: aspectSystem.add(new ClassPointcut(TestIface.class, true),
053: interceptor);
054: m = TestImpl.class.getMethod("test", null);
055:
056: interceptorList = aspectSystem.findInterceptors(m, m);
057: assertTrue(interceptorList.size() > 0);
058:
059: // make it fail
060: m = this .getClass().getMethod("setUp", null);
061: interceptorList = aspectSystem.findInterceptors(m, m);
062:
063: assertTrue(interceptorList.size() == 0);
064:
065: // test equals method
066: aspectSystem.clear();
067: aspectSystem.add(new ClassPointcut(TestIface.class),
068: interceptor);
069:
070: aspectSystem.add(new ClassPointcut(TestIface.class),
071: interceptor);
072:
073: assertTrue(aspectSystem.getAspectList().size() == 1);
074:
075: }
076:
077: public void testClassPatternPointcut() throws Exception {
078:
079: TestInterceptor interceptor = new TestInterceptor();
080: Object target = new TestImpl();
081:
082: aspectSystem.clear();
083: aspectSystem.add(new ClassPatternPointcut(".*test"),
084: interceptor);
085:
086: Method m = TestIface.class.getMethod("test", null);
087:
088: List interceptorList = aspectSystem.findInterceptors(m, m);
089:
090: assertTrue(interceptorList.size() > 0);
091:
092: // test equals method
093:
094: aspectSystem.clear();
095: aspectSystem.add(new ClassPatternPointcut("Test"), interceptor);
096:
097: aspectSystem.add(new ClassPatternPointcut("Test"), interceptor);
098:
099: assertTrue(aspectSystem.getAspectList().size() == 1);
100:
101: }
102:
103: public void testMethodPatternPointcut() throws Exception {
104:
105: TestInterceptor interceptor = new TestInterceptor();
106: Object target = new TestImpl();
107:
108: aspectSystem.clear();
109: aspectSystem.add(new MethodPatternPointcut("TestIface.test()"),
110: interceptor);
111:
112: Method m = TestIface.class.getMethod("test", null);
113:
114: List interceptorList = aspectSystem.findInterceptors(m, m);
115:
116: assertTrue(interceptorList.size() > 0);
117:
118: // test equals method
119:
120: aspectSystem.clear();
121: aspectSystem
122: .add(new MethodPatternPointcut("Test"), interceptor);
123:
124: aspectSystem
125: .add(new MethodPatternPointcut("Test"), interceptor);
126:
127: assertTrue(aspectSystem.getAspectList().size() == 1);
128:
129: }
130:
131: public void testPointcutPair() throws Exception {
132:
133: TestInterceptor interceptor = new TestInterceptor();
134: Object target = new TestImpl();
135:
136: // make it fail
137: aspectSystem.clear();
138: aspectSystem.add(PointcutPair.and(new MethodPatternPointcut(
139: "test()"), new ClassPointcut(this .getClass())),
140: interceptor);
141:
142: Method m = TestIface.class.getMethod("test", null);
143:
144: List interceptorList = aspectSystem.findInterceptors(m, m);
145:
146: assertTrue(interceptorList.size() == 0);
147:
148: // make it succeed
149: aspectSystem.clear();
150: aspectSystem.add(PointcutPair.and(new MethodPatternPointcut(
151: "test()"), new ClassPointcut(TestIface.class)),
152: interceptor);
153:
154: m = TestIface.class.getMethod("test", null);
155:
156: interceptorList = aspectSystem.findInterceptors(m, m);
157:
158: assertTrue(interceptorList.size() == 1);
159:
160: // test or
161: aspectSystem.clear();
162: aspectSystem.add(PointcutPair.or(new MethodPatternPointcut(
163: "test()"), new ClassPointcut(this .getClass())),
164: interceptor);
165:
166: m = TestIface.class.getMethod("test", null);
167:
168: interceptorList = aspectSystem.findInterceptors(m, m);
169:
170: assertTrue(interceptorList.size() == 1);
171:
172: // test complex condition
173: aspectSystem.clear();
174: aspectSystem.add(
175: PointcutPair.or(new MethodPatternPointcut("fail()"),
176: PointcutPair.and(new ClassPointcut(
177: TestIface.class),
178: new MethodPatternPointcut("test()"))),
179: interceptor);
180:
181: m = TestIface.class.getMethod("test", null);
182:
183: interceptorList = aspectSystem.findInterceptors(m, m);
184:
185: assertTrue(interceptorList.size() == 1);
186:
187: // TODO: test equals method
188:
189: }
190:
191: }
|