KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > bsh > BshHelperTest


1 package dynaop.bsh;
2
3 import java.io.Serializable JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import dynaop.Aspects;
10 import dynaop.ClassPointcut;
11 import dynaop.MethodPointcut;
12 import dynaop.Pointcuts;
13 import dynaop.util.Classes;
14
15 import junit.framework.TestCase;
16
17 /**
18  *
19  *
20  * @author Bob Lee (crazybob@crazybob.org)
21  */

22 public class BshHelperTest extends TestCase {
23
24     public void testDeclaringClass() throws Throwable JavaDoc{
25         BshHelper helper = new BshHelper(new Aspects());
26         
27         assertTrue(helper.declaringClass(Collection JavaDoc.class).picks(
28             Collection JavaDoc.class.getMethod("isEmpty", null)));
29         assertTrue(helper.declaringClass("Collection").picks(
30             Collection JavaDoc.class.getMethod("isEmpty", null)));
31     }
32     
33     public void testClassPointcutUnion() throws Throwable JavaDoc {
34         BshHelper helper = new BshHelper(new Aspects());
35         
36         Object JavaDoc[] collections = new Object JavaDoc[] { "List", Collection JavaDoc.class,
37             Pointcuts.instancesOf(Collection JavaDoc.class) };
38         Object JavaDoc[] maps = new Object JavaDoc[] { "Map", Map JavaDoc.class,
39             Pointcuts.instancesOf(Map JavaDoc.class) };
40         
41         for (int x = 0; x < collections.length; x++) {
42             for (int y = 0; y < maps.length; y++) {
43                 try {
44                     assertTrue(collections[x] + ", " + maps[y],
45                         ((ClassPointcut) helper.union(
46                             collections[x], maps[y])).picks(List JavaDoc.class));
47                     assertFalse(collections[x] + ", " + maps[y],
48                         ((ClassPointcut) helper.union(
49                             collections[x], maps[y])).picks(
50                                     Serializable JavaDoc.class));
51                 }
52                 catch (RuntimeException JavaDoc e) {
53                     assertTrue(collections[x] instanceof String JavaDoc);
54                     assertTrue(maps[x] instanceof String JavaDoc);
55                 }
56             }
57         }
58     }
59
60     interface SerializableMap extends Map JavaDoc, Serializable JavaDoc {}
61     
62     public void testClassPointcutIntersection() throws Throwable JavaDoc {
63         BshHelper helper = new BshHelper(new Aspects());
64         
65         Object JavaDoc[] collections = new Object JavaDoc[] { "Serializable",
66             Serializable JavaDoc.class, Pointcuts.instancesOf(Serializable JavaDoc.class) };
67         Object JavaDoc[] maps = new Object JavaDoc[] { "Map", Map JavaDoc.class,
68             Pointcuts.instancesOf(Map JavaDoc.class) };
69         
70         for (int x = 0; x < collections.length; x++) {
71             for (int y = 0; y < maps.length; y++) {
72                 try {
73                     assertTrue(collections[x] + ", " + maps[y],
74                         ((ClassPointcut) helper.intersection(
75                             collections[x], maps[y])).picks(
76                                 SerializableMap.class));
77                     assertFalse(collections[x] + ", " + maps[y],
78                         ((ClassPointcut) helper.intersection(
79                             collections[x], maps[y])).picks(
80                                     List JavaDoc.class));
81                 }
82                 catch (RuntimeException JavaDoc e) {
83                     assertTrue(collections[x] instanceof String JavaDoc);
84                     assertTrue(maps[x] instanceof String JavaDoc);
85                 }
86             }
87         }
88     }
89
90     public void testMethodPointcutUnion() throws Throwable JavaDoc {
91         BshHelper helper = new BshHelper(new Aspects());
92         
93         Method JavaDoc sizeMethod = Collection JavaDoc.class.getMethod("size", null);
94         Method JavaDoc isEmptyMethod = Collection JavaDoc.class.getMethod("isEmpty", null);
95         
96         Object JavaDoc[] size = new Object JavaDoc[] { "size", sizeMethod,
97             Pointcuts.singleton(sizeMethod) };
98         Object JavaDoc[] isEmpty = new Object JavaDoc[] { "isEmpty", isEmptyMethod,
99             Pointcuts.singleton(isEmptyMethod) };
100         
101         for (int x = 0; x < size.length; x++) {
102             for (int y = 0; y < isEmpty.length; y++) {
103                 try {
104                     assertTrue(size[x] + ", " + isEmpty[y],
105                         ((MethodPointcut) helper.union(
106                             size[x], isEmpty[y])).picks(sizeMethod));
107                     assertFalse(size[x] + ", " + isEmpty[y],
108                         ((MethodPointcut) helper.union(
109                             size[x], isEmpty[y])).picks(
110                                 Classes.HASHCODE_METHOD));
111                 }
112                 catch (RuntimeException JavaDoc e) {
113                     assertTrue(size[x] instanceof String JavaDoc);
114                     assertTrue(isEmpty[x] instanceof String JavaDoc);
115                 }
116             }
117         }
118     }
119
120     public void testMethodPointcutIntersection() throws Throwable JavaDoc {
121         BshHelper helper = new BshHelper(new Aspects());
122         
123         Method JavaDoc sizeMethod = Collection JavaDoc.class.getMethod("size", null);
124         
125         Object JavaDoc[] size = new Object JavaDoc[] { "size", sizeMethod,
126             Pointcuts.singleton(sizeMethod) };
127
128         for (int x = 0; x < size.length; x++) {
129             for (int y = 0; y < size.length; y++) {
130                 try {
131                     assertTrue(size[x] + ", " + size[y],
132                         ((MethodPointcut) helper.intersection(
133                             size[x], size[y])).picks(sizeMethod));
134                     assertFalse(size[x] + ", " + size[y],
135                         ((MethodPointcut) helper.intersection(
136                             size[x], size[y])).picks(
137                                 Classes.HASHCODE_METHOD));
138                 }
139                 catch (RuntimeException JavaDoc e) {
140                     assertTrue(size[x] instanceof String JavaDoc);
141                     assertTrue(size[y] instanceof String JavaDoc);
142                 }
143             }
144         }
145     }
146 }
147
Popular Tags