001: /*****************************************************************************
002: * Copyright (c) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Idea by Rachel Davies, Original code by various *
009: *****************************************************************************/package org.nanocontainer.aop.dynaop;
010:
011: import junit.framework.TestCase;
012: import org.nanocontainer.aop.ClassPointcut;
013: import org.nanocontainer.aop.ComponentPointcut;
014: import org.nanocontainer.aop.MalformedRegularExpressionException;
015: import org.nanocontainer.aop.MethodPointcut;
016: import org.nanocontainer.aop.PointcutsFactory;
017: import org.nanocontainer.testmodel.Dao;
018:
019: import java.io.Serializable;
020: import java.lang.reflect.Method;
021:
022: /**
023: * @author Stephen Molitor
024: */
025: public class DynaopPointcutsFactoryTestCase extends TestCase {
026:
027: private PointcutsFactory cuts = new DynaopPointcutsFactory();
028: private Method apple;
029: private Method apricot;
030: private Method banana;
031: private Method getA;
032: private Method misleadingGetA;
033: private Method isA;
034: private Method getB;
035: private Method isB;
036: private Method setA;
037: private Method setB;
038: private Method equals;
039: private Method hashCode;
040: private Method toString;
041: private Method subFooMethod;
042:
043: public void testAllClasses() {
044: ClassPointcut cut = cuts.allClasses();
045: assertTrue(cut.picks(Object.class));
046: assertTrue(cut.picks(Serializable.class));
047: assertTrue(cut.picks(Foo.class));
048: assertTrue(cut.picks(this .getClass()));
049: }
050:
051: public void testInstancesOf() {
052: ClassPointcut cut = cuts.instancesOf(Serializable.class);
053: assertTrue(cut.picks(Serializable.class));
054: assertTrue(cut.picks(String.class));
055: assertFalse(cut.picks(Foo.class));
056: }
057:
058: public void testClassName() {
059: ClassPointcut cut = cuts.className("Foo*");
060: assertTrue(cut.picks(Foo.class));
061: assertTrue(cut.picks(FooBar.class));
062: assertFalse(cut.picks(Bar.class));
063:
064: assertFalse(cuts.className("^Foo*").picks(Foo.class));
065: }
066:
067: public void testOneClass() {
068: ClassPointcut cut = cuts.oneClass(Foo.class);
069: assertTrue(cut.picks(Foo.class));
070: assertFalse(cut.picks(FooBar.class));
071: }
072:
073: public void testPackageName() {
074: ClassPointcut cut = cuts
075: .packageName("org.nanocontainer.aop.dynaop");
076: assertTrue(cut.picks(Foo.class));
077: assertTrue(cut.picks(Bar.class));
078: assertFalse(cut.picks(org.nanocontainer.testmodel.Dao.class));
079: }
080:
081: public void testClassPointcutIntersection() {
082: ClassPointcut a = cuts.union(cuts.oneClass(Foo.class), cuts
083: .oneClass(Bar.class));
084: ClassPointcut b = cuts.union(cuts.oneClass(Foo.class), cuts
085: .oneClass(FooBar.class));
086: ClassPointcut c = cuts.intersection(a, b);
087: assertTrue(c.picks(Foo.class));
088: assertFalse(c.picks(Bar.class));
089: assertFalse(c.picks(FooBar.class));
090: }
091:
092: public void testClassPointcutNot() {
093: ClassPointcut cut = cuts.not(cuts.oneClass(Foo.class));
094: assertFalse(cut.picks(Foo.class));
095: assertTrue(cut.picks(Bar.class));
096: }
097:
098: public void testClassPointcutUnion() {
099: ClassPointcut cut = cuts.union(cuts.oneClass(Foo.class), cuts
100: .oneClass(Bar.class));
101: assertTrue(cut.picks(Foo.class));
102: assertTrue(cut.picks(Bar.class));
103: assertFalse(cut.picks(FooBar.class));
104: }
105:
106: public void testAllMethods() {
107: MethodPointcut cut = cuts.allMethods();
108: Method[] methods = Foo.class.getMethods();
109: for (int i = 0; i < methods.length; ++i) {
110: assertTrue(cut.picks(methods[i]));
111: }
112: }
113:
114: public void testGetMethods() {
115: MethodPointcut cut = cuts.getMethods();
116: assertTrue(cut.picks(getA));
117: assertTrue(cut.picks(getB));
118: assertTrue(cut.picks(misleadingGetA));
119: assertFalse(cut.picks(isA));
120: assertFalse(cut.picks(isB));
121: assertFalse(cut.picks(setA));
122: assertFalse(cut.picks(setB));
123: assertFalse(cut.picks(apple));
124: assertFalse(cut.picks(apricot));
125: assertFalse(cut.picks(banana));
126: }
127:
128: public void testIsMethods() {
129: MethodPointcut cut = cuts.isMethods();
130: assertFalse(cut.picks(getA));
131: assertFalse(cut.picks(getB));
132: assertTrue(cut.picks(isA));
133: assertTrue(cut.picks(isB));
134: assertFalse(cut.picks(setA));
135: assertFalse(cut.picks(setB));
136: assertFalse(cut.picks(apple));
137: assertFalse(cut.picks(apricot));
138: assertFalse(cut.picks(banana));
139: }
140:
141: public void testSetMethods() {
142: MethodPointcut cut = cuts.setMethods();
143: assertFalse(cut.picks(getA));
144: assertFalse(cut.picks(getB));
145: assertFalse(cut.picks(isA));
146: assertFalse(cut.picks(isB));
147: assertTrue(cut.picks(setA));
148: assertTrue(cut.picks(setB));
149: assertFalse(cut.picks(apple));
150: assertFalse(cut.picks(apricot));
151: assertFalse(cut.picks(banana));
152: }
153:
154: public void testSignature() {
155: assertTrue(cuts.signature("getA").picks(getA));
156: assertTrue(cuts.signature("getA").picks(misleadingGetA));
157: assertTrue(cuts.signature("getA()").picks(getA));
158: assertTrue(cuts.signature("String getA\\(String\\)").picks(
159: misleadingGetA));
160: assertFalse(cuts.signature("String getA\\(String\\)").picks(
161: getA));
162: assertTrue(cuts.signature("get*").picks(getA));
163: assertTrue(cuts.signature("get*").picks(getB));
164: assertTrue(cuts.signature("get*").picks(misleadingGetA));
165: }
166:
167: public void testOneMethod() {
168: MethodPointcut cut = cuts.oneMethod(getA);
169: assertTrue(cut.picks(getA));
170: assertFalse(cut.picks(getB));
171: assertFalse(cut.picks(isA));
172: assertFalse(cut.picks(isB));
173: assertFalse(cut.picks(setA));
174: assertFalse(cut.picks(setB));
175: assertFalse(cut.picks(apple));
176: assertFalse(cut.picks(apricot));
177: assertFalse(cut.picks(banana));
178: }
179:
180: public void testReturnType() {
181: MethodPointcut cut = cuts.returnType(cuts
182: .oneClass(String.class));
183: assertTrue(cut.picks(getA));
184: assertTrue(cut.picks(getB));
185: assertFalse(cut.picks(isA));
186: assertFalse(cut.picks(isB));
187: assertFalse(cut.picks(setA));
188: assertFalse(cut.picks(setB));
189: assertFalse(cut.picks(apple));
190: assertFalse(cut.picks(apricot));
191: assertFalse(cut.picks(banana));
192: }
193:
194: public void testMethodPointcutIntersection() {
195: MethodPointcut a = cuts.union(cuts.oneMethod(apple), cuts
196: .oneMethod(apricot));
197: MethodPointcut b = cuts.union(cuts.oneMethod(apple), cuts
198: .oneMethod(banana));
199: MethodPointcut c = cuts.intersection(a, b);
200:
201: assertTrue(c.picks(apple));
202: assertFalse(c.picks(apricot));
203: assertFalse(c.picks(banana));
204: }
205:
206: public void testMethodPointcutNot() {
207: MethodPointcut cut = cuts.not(cuts.oneMethod(getA));
208: assertFalse(cut.picks(getA));
209: assertTrue(cut.picks(getB));
210: assertTrue(cut.picks(isA));
211: assertTrue(cut.picks(isB));
212: assertTrue(cut.picks(setA));
213: assertTrue(cut.picks(setB));
214: assertTrue(cut.picks(apple));
215: assertTrue(cut.picks(apricot));
216: assertTrue(cut.picks(banana));
217: }
218:
219: public void testMethodPointcutUnion() {
220: MethodPointcut cut = cuts.union(cuts.oneMethod(apple), cuts
221: .oneMethod(apricot));
222: assertTrue(cut.picks(apple));
223: assertTrue(cut.picks(apricot));
224: assertFalse(cut.picks(banana));
225: assertFalse(cut.picks(getA));
226: assertFalse(cut.picks(getB));
227: assertFalse(cut.picks(isA));
228: assertFalse(cut.picks(isB));
229: assertFalse(cut.picks(setA));
230: assertFalse(cut.picks(setB));
231: }
232:
233: public void testComponent() {
234: ComponentPointcut cut = cuts.component(Dao.class);
235: assertTrue(cut.picks(Dao.class));
236: assertFalse(cut.picks("Dao"));
237: }
238:
239: public void testComponentName() {
240: ComponentPointcut cut = cuts.componentName("foo*");
241: assertTrue(cut.picks("foo"));
242: assertTrue(cut.picks("foobar"));
243: assertFalse(cut.picks("bar"));
244: }
245:
246: public void testMalformedPatternExceptionRethrown() {
247: try {
248: cuts.className("(");
249: fail("MalformedRegularExpressionException should have been raised");
250: } catch (MalformedRegularExpressionException e) {
251: }
252:
253: try {
254: cuts.signature("(");
255: fail("MalformedRegularExpressionException should have been raised");
256: } catch (MalformedRegularExpressionException e) {
257: }
258: }
259:
260: public void testObjectMethods() {
261: MethodPointcut cut = cuts.objectMethods();
262: assertTrue(cut.picks(equals));
263: assertTrue(cut.picks(hashCode));
264: assertTrue(cut.picks(toString));
265: assertFalse(cut.picks(isA));
266: assertFalse(cut.picks(setA));
267: }
268:
269: public void testDeclaringClass() {
270: MethodPointcut cut = cuts.declaringClass(cuts
271: .oneClass(Object.class));
272: assertTrue(cut.picks(equals));
273: assertTrue(cut.picks(hashCode));
274: assertTrue(cut.picks(toString));
275: assertFalse(cut.picks(isA));
276: assertFalse(cut.picks(setA));
277: }
278:
279: public void testMembersOf() {
280: MethodPointcut cut = cuts.membersOf(Foo.class);
281: assertTrue(cut.picks(apple));
282: assertTrue(cut.picks(equals));
283: assertFalse(cut.picks(subFooMethod));
284: }
285:
286: public void testCustomClassPointcuts() {
287: ClassPointcut picksFoo = new ClassPointcut() {
288: public boolean picks(Class clazz) {
289: return clazz.equals(Foo.class);
290: }
291: };
292: ClassPointcut picksBar = new ClassPointcut() {
293: public boolean picks(Class clazz) {
294: return clazz.equals(Bar.class);
295: }
296: };
297: ClassPointcut cut = cuts.union(picksFoo, picksBar);
298: assertTrue(cut.picks(Foo.class));
299: assertTrue(cut.picks(Bar.class));
300: assertFalse(cut.picks(FooBar.class));
301: }
302:
303: public void testCustomMethodPointcuts() {
304: MethodPointcut picksApple = new MethodPointcut() {
305: public boolean picks(Method method) {
306: return method.equals(apple);
307: }
308: };
309: MethodPointcut picksApricot = new MethodPointcut() {
310: public boolean picks(Method method) {
311: return method.equals(apricot);
312: }
313: };
314: MethodPointcut cut = cuts.union(picksApple, picksApricot);
315: assertTrue(cut.picks(apple));
316: assertTrue(cut.picks(apricot));
317: assertFalse(cut.picks(banana));
318: }
319:
320: protected void setUp() throws Exception {
321: super .setUp();
322: apple = Foo.class.getMethod("apple", new Class[] {});
323: apricot = Foo.class.getMethod("apricot", new Class[] {});
324: banana = Foo.class.getMethod("banana", new Class[] {});
325: getA = Foo.class.getMethod("getA", new Class[] {});
326: misleadingGetA = Foo.class.getMethod("getA",
327: new Class[] { String.class });
328: isA = Foo.class.getMethod("isA", new Class[] {});
329: getB = Foo.class.getMethod("getB", new Class[] {});
330: isB = Foo.class.getMethod("isB", new Class[] {});
331: setA = Foo.class
332: .getMethod("setA", new Class[] { String.class });
333: setB = Foo.class
334: .getMethod("setA", new Class[] { String.class });
335: equals = Object.class.getMethod("equals",
336: new Class[] { Object.class });
337: hashCode = Object.class.getMethod("hashCode", new Class[] {});
338: toString = Object.class.getMethod("toString", new Class[] {});
339: subFooMethod = SubFoo.class.getMethod("subFooMethod",
340: new Class[] {});
341: }
342:
343: }
|