1 7 package ch.ethz.prose.filter; 8 9 import java.lang.reflect.Method ; 11 import java.lang.reflect.Proxy ; 12 import java.util.List ; 13 14 import junit.framework.*; 15 import ch.ethz.jvmai.CodeJoinPoint; 16 import ch.ethz.jvmai.JoinPoint; 17 import ch.ethz.prose.engine.JoinPointRequest; 18 19 25 public 26 class AbstractCrosscutSpecializerTest extends TestCase { 27 28 static class TestRequest extends JoinPointRequest 30 { 31 long mySignature; 32 public String getKind() 33 { 34 return ""; 35 } 36 37 public int getMask() 38 { 39 return 0; 40 } 41 42 public TestRequest(long signature) 43 { 44 super(null); 45 mySignature = signature; 46 } 47 48 protected void setWatch(Object listeners) { } 49 protected void clearWatch() { } 50 public boolean equals(Object other) 51 { return ((other instanceof TestRequest) && (((TestRequest)other).mySignature==mySignature)); } 52 public int hashCode() { return (int)mySignature; } 53 } 54 55 interface TestEvent extends JoinPoint 56 { 57 public long myValue(); 58 } 59 60 static class TestEventHandler implements java.lang.reflect.InvocationHandler 61 { 62 protected long myValue; 63 public Object invoke(Object proxy,Method m, Object [] args) throws Throwable 64 { 65 if(m.getName().equals("myValue")) 66 return new Long (myValue); 67 else 68 return null; 69 } 70 71 static TestEventHandler create(long val) 72 { 73 TestEventHandler x = new TestEventHandler(); 74 x.myValue = val; 75 return x; 76 } 77 } 78 79 static class SpecializerSmaller extends PointCutter { 80 81 long limit; 82 83 SpecializerSmaller(long theLimit) 84 { 85 limit = theLimit; 86 } 87 88 protected boolean doIsSpecialRequest(JoinPointRequest req) 89 { 90 if (req instanceof TestRequest) 91 { 92 return ((TestRequest)req).mySignature < limit; 93 } 94 else 95 return true; 96 } 97 98 99 protected boolean doIsSpecialEvent(CodeJoinPoint ev) 100 { 101 if (ev instanceof TestEvent) 102 { 103 return ((TestEvent)ev).myValue() < limit; 104 } 105 else 106 return true; 107 } 108 } 109 110 TestEvent e1000; 111 TestEvent e2000; 112 TestEvent e3000; 113 TestRequest r1000; 114 TestRequest r2000; 115 TestRequest r3000; 116 SpecializerSmaller spec1500; ; 117 SpecializerSmaller spec500; 118 119 123 public AbstractCrosscutSpecializerTest(String name) 124 { 125 super(name); 126 } 127 128 131 protected 132 void setUp() 133 { 134 e1000=(TestEvent)Proxy.newProxyInstance(this.getClass().getClassLoader(),new Class [] { TestEvent.class},TestEventHandler.create(1000)); 135 e2000=(TestEvent)Proxy.newProxyInstance(TestEvent.class.getClassLoader(),new Class [] { TestEvent.class},TestEventHandler.create(2000)); 136 e3000=(TestEvent)Proxy.newProxyInstance(TestEvent.class.getClassLoader(),new Class [] { TestEvent.class},TestEventHandler.create(3000)); 137 r1000= new TestRequest(1000); 138 r2000= new TestRequest(2000); 139 r3000= new TestRequest(3000); 140 spec1500= new SpecializerSmaller(1500); 141 spec500= new SpecializerSmaller(500); 142 } 143 144 public void testAnd() 145 { 146 PointFilter andspec= spec1500.AND(spec500); 147 assertTrue(!andspec.isSpecialRequest(r1000)); 148 assertTrue(!andspec.isSpecialRequest(r2000)); 149 assertTrue(!andspec.isSpecialRequest(r3000)); 150 assertTrue(!andspec.isSpecialEvent(e1000)); 151 assertTrue(!andspec.isSpecialEvent(e2000)); 152 assertTrue(!andspec.isSpecialEvent(e3000)); 153 } 154 155 public void testOr() 156 { 157 PointFilter orspec = spec1500.OR(spec500); 158 assertTrue(orspec.isSpecialRequest(r1000)); 159 assertTrue(!orspec.isSpecialRequest(r2000)); 160 assertTrue(!orspec.isSpecialRequest(r3000)); 161 assertTrue(orspec.isSpecialEvent(e1000)); 162 assertTrue(!orspec.isSpecialEvent(e2000)); 163 assertTrue(!orspec.isSpecialEvent(e3000)); 164 } 165 166 public void testComposition() 167 { 168 PointCutter someSpec = spec1500.OR(spec500.AND(spec1500)); 169 List members = someSpec.memberPointFilters(); 170 assertTrue("Two members", members.size() == 2); 171 } 172 173 177 public static 178 Test suite() 179 { 180 return new TestSuite(AbstractCrosscutSpecializerTest.class); 181 } 182 } 183 184 185 | Popular Tags |