1 7 package ch.ethz.prose.crosscut; 8 9 import java.io.IOException ; 11 import java.util.*; 12 13 import junit.framework.*; 14 import ch.ethz.inf.iks.jvmai.jvmdi.ItemManipulationException; 15 import ch.ethz.jvmai.ExceptionJoinPoint; 16 import ch.ethz.jvmai.JVMAspectInterface; 17 import ch.ethz.prose.*; 18 import ch.ethz.prose.engine.*; 19 import ch.ethz.prose.filter.PointCutter; 20 21 27 public 28 class ExceptionCutTest extends TestCase { 29 30 32 public static class MyExtensionManager extends LocalAspectManager { 34 public MyExtensionManager(boolean ic, JVMAspectInterface ai) 35 { super(ic, ai); } 36 protected void createJoinPointManager(boolean ic, JVMAspectInterface ai) 37 { jpm = new MyJoinPointManager(ic, ai, false); if (ic) jpm.connectToJVMAI(); } 38 39 } 40 41 static class MyJoinPointManager extends JoinPointManager { 42 public MyJoinPointManager(boolean ic, JVMAspectInterface ai, boolean rev) { super(ic,ai,rev); } 43 public Map getReq2listener() { return req2listener; } 44 public Set getClassLoadListeners() { return classLoadListeners; } 45 } 46 47 static class TestException extends Throwable  48 { 49 public TestException() {super();} 50 public TestException(String s) {super(s);} 51 } 52 53 static class ExampleClass 54 { 55 public ExampleClass() {} 56 public static void method1() throws Exception { throw new Exception (); } 57 public static void method2() throws TestException { throw new TestException(); } 58 public void method3() throws UnknownError { throw new UnknownError (); } 59 public void method4() throws IllegalAspectException, ItemManipulationException { throw new IllegalAspectException(); } 60 public void method5() throws IOException { int i=0; if (i==0) throw new IOException (); throw new IllegalAspectException(); } 61 public void method6() { throw new RuntimeException (); } 62 } 63 64 public static class TestExtension extends DefaultAspect 65 { 66 public Crosscut c1 = new ThrowCut() 67 { 68 public void THROW_ARGS() 69 { counter1++; } 70 protected PointCutter pointCutter() 71 { 72 return null; 73 } 74 75 public Class [] potentialCrosscutClasses() 76 { 77 Class [] result = { ExampleClass.class, 78 Object .class, 79 ThrowCut.class, 80 Exception .class, 81 TestException.class, 82 UnknownError .class, 83 IllegalAspectException.class, 84 ItemManipulationException.class 85 }; 86 return result; 87 } 88 }; 89 } 90 91 public static class TestExtension2 extends DefaultAspect 92 { 93 public Crosscut c2 = new ThrowCut() 94 { 95 public void THROW_ARGS() 96 { 97 counter2++; 98 eventClass = ((ExceptionJoinPoint)thisJoinPoint()).getException().getClass(); 99 } 100 protected PointCutter pointCutter() 101 { 102 return null; 103 } 104 }; 105 public Class [] getAllClasses() { return ((AbstractCrosscut)c2).potentialCrosscutClasses(); } 106 } 107 108 static int counter2=0; 109 ExampleClass exCls; 110 ThrowCut testCrosscut = null; 111 Map myReq2listener = null; 112 static int counter1 = 0; 113 static Class eventClass = null; 114 115 119 public ExceptionCutTest(String name) 120 { 121 super(name); 122 } 123 124 String oldExMgr; 125 128 protected 129 void setUp() 130 { 131 try 132 { 133 oldExMgr=System.getProperty("ch.ethz.prose.EXManager"); 134 System.setProperty("ch.ethz.prose.EXManager", "ch.ethz.prose.crosscut.ExceptionCutTest$MyExtensionManager"); 135 ProseSystem.startup(); 136 } 137 catch ( Exception e ) 138 { 139 e.printStackTrace(); 140 tearDown(); 141 Assert.fail("ProseSystem.startup() failed."); 142 } 143 exCls = new ExampleClass(); 144 } 145 146 protected void tearDown() 147 { 148 try 149 { 150 System.err.println("TearingDown" + this); 151 ProseSystem.teardown(); 152 System.setProperty("ch.ethz.prose.EXManager", oldExMgr); 153 } 154 catch (Exception e) 155 { 156 Assert.fail("ProseSystem.teardown() failed"); 157 } 158 } 159 160 161 public void test0020_createRequestsAndEvents() 162 { 163 ProseSystem.getAspectManager().insert(new TestExtension()); 164 myReq2listener = ((MyJoinPointManager)ProseSystem.getAspectManager().getJoinPointManager()).getReq2listener(); 165 assertTrue("Exactly 5 JoinPoints created", myReq2listener.size() == 5); 166 167 counter1 = 0; 168 try { ExampleClass.method1(); } catch (Throwable e) { } 169 try { ExampleClass.method2(); } catch (Throwable e) { } 170 try { exCls.method3(); } catch (Throwable e) { } 171 try { exCls.method4(); } catch (Throwable e) { } 172 assertEquals("Exactly 4 ExceptionEvents reported", 4,counter1); 173 174 counter1 = 0; 175 try { exCls.method5(); } catch (Throwable e) { } 176 try { exCls.method6(); } catch (Throwable e) { } 177 assertTrue("0 ExceptionEvents reported", counter1 == 0); 178 } 179 180 181 public void test0030_allLoadedClasses() 182 { 183 Iterator it, il, ir; 184 int removeme=0; 185 TestExtension2 t2 = new TestExtension2(); 186 187 ProseSystem.getAspectManager().insert(t2); 189 CrosscutRequest joinPointsWeHaveAskedFor = ((Crosscut)(t2.getCrosscuts().get(0))).createRequest(); 190 ProseSystem.getAspectManager().withdraw(t2); 191 192 Vector requestedExceptionClasses = new Vector(); 194 it = joinPointsWeHaveAskedFor.iterator(); 195 while (it.hasNext()) 196 { 197 JoinPointRequest jpr = (JoinPointRequest)(it.next()); 198 if (jpr instanceof ExceptionThrowRequest) 199 requestedExceptionClasses.add(((ExceptionThrowRequest)jpr).getExceptionClass()); 200 } 201 202 Class [] allClasses = t2.getAllClasses(); 205 Vector loadedExceptionClasses = new Vector(); 206 for (int j=0; j<allClasses.length;j++) 207 if (Throwable .class.isAssignableFrom((allClasses[j]))) 208 loadedExceptionClasses.add(allClasses[j]); 209 210 il = loadedExceptionClasses.iterator(); 212 while (il.hasNext()) 213 assertTrue("Crosscut request contains loaded class", requestedExceptionClasses.contains(il.next())); 214 ir = requestedExceptionClasses.iterator(); 215 while (ir.hasNext()) 216 217 assertTrue("loaded classes contains requested class", loadedExceptionClasses.contains(ir.next())); 218 219 223 List throwableObjects = new Vector(); 225 il = loadedExceptionClasses.iterator(); 226 227 while (il.hasNext()) 228 { 229 Class actualClass = (Class )il.next(); 230 try 231 { 232 throwableObjects.add(actualClass.newInstance()); 233 } 234 catch (InstantiationException ie) 235 { } 236 catch (IllegalAccessException iae) 237 { } 238 } 239 240 ProseSystem.getAspectManager().insert(t2); 244 245 il = throwableObjects.iterator(); 246 counter2=0; 247 while (il.hasNext()) 248 { 249 Throwable exc= (Throwable )il.next(); 250 try {throw exc;} catch(Throwable e) {} } 252 assertEquals("For every Exception Class an event", counter2, throwableObjects.size()); 253 ProseSystem.getAspectManager().withdraw(t2); 254 } 255 256 260 public static 261 Test suite() 262 { 263 return new TestSuite(ExceptionCutTest.class); 264 } 265 266 } 267 268 269 | Popular Tags |