1 package dynaop.bsh; 2 3 import java.io.Serializable ; 4 import java.lang.reflect.Method ; 5 import java.util.Collection ; 6 import java.util.List ; 7 import java.util.Map ; 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 22 public class BshHelperTest extends TestCase { 23 24 public void testDeclaringClass() throws Throwable { 25 BshHelper helper = new BshHelper(new Aspects()); 26 27 assertTrue(helper.declaringClass(Collection .class).picks( 28 Collection .class.getMethod("isEmpty", null))); 29 assertTrue(helper.declaringClass("Collection").picks( 30 Collection .class.getMethod("isEmpty", null))); 31 } 32 33 public void testClassPointcutUnion() throws Throwable { 34 BshHelper helper = new BshHelper(new Aspects()); 35 36 Object [] collections = new Object [] { "List", Collection .class, 37 Pointcuts.instancesOf(Collection .class) }; 38 Object [] maps = new Object [] { "Map", Map .class, 39 Pointcuts.instancesOf(Map .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 .class)); 47 assertFalse(collections[x] + ", " + maps[y], 48 ((ClassPointcut) helper.union( 49 collections[x], maps[y])).picks( 50 Serializable .class)); 51 } 52 catch (RuntimeException e) { 53 assertTrue(collections[x] instanceof String ); 54 assertTrue(maps[x] instanceof String ); 55 } 56 } 57 } 58 } 59 60 interface SerializableMap extends Map , Serializable {} 61 62 public void testClassPointcutIntersection() throws Throwable { 63 BshHelper helper = new BshHelper(new Aspects()); 64 65 Object [] collections = new Object [] { "Serializable", 66 Serializable .class, Pointcuts.instancesOf(Serializable .class) }; 67 Object [] maps = new Object [] { "Map", Map .class, 68 Pointcuts.instancesOf(Map .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 .class)); 81 } 82 catch (RuntimeException e) { 83 assertTrue(collections[x] instanceof String ); 84 assertTrue(maps[x] instanceof String ); 85 } 86 } 87 } 88 } 89 90 public void testMethodPointcutUnion() throws Throwable { 91 BshHelper helper = new BshHelper(new Aspects()); 92 93 Method sizeMethod = Collection .class.getMethod("size", null); 94 Method isEmptyMethod = Collection .class.getMethod("isEmpty", null); 95 96 Object [] size = new Object [] { "size", sizeMethod, 97 Pointcuts.singleton(sizeMethod) }; 98 Object [] isEmpty = new Object [] { "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 e) { 113 assertTrue(size[x] instanceof String ); 114 assertTrue(isEmpty[x] instanceof String ); 115 } 116 } 117 } 118 } 119 120 public void testMethodPointcutIntersection() throws Throwable { 121 BshHelper helper = new BshHelper(new Aspects()); 122 123 Method sizeMethod = Collection .class.getMethod("size", null); 124 125 Object [] size = new Object [] { "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 e) { 140 assertTrue(size[x] instanceof String ); 141 assertTrue(size[y] instanceof String ); 142 } 143 } 144 } 145 } 146 } 147 | Popular Tags |