001: // $Id: ExceptionCutTest.java,v 1.2 2004/05/12 17:26:51 anicoara Exp $
002: // =====================================================================
003: //
004: // (history at end)
005: //
006:
007: package ch.ethz.prose.crosscut;
008:
009: // used packages
010: import java.io.IOException;
011: import java.util.*;
012:
013: import junit.framework.*;
014: import ch.ethz.inf.iks.jvmai.jvmdi.ItemManipulationException;
015: import ch.ethz.jvmai.ExceptionJoinPoint;
016: import ch.ethz.jvmai.JVMAspectInterface;
017: import ch.ethz.prose.*;
018: import ch.ethz.prose.engine.*;
019: import ch.ethz.prose.filter.PointCutter;
020:
021: /**
022: * JUnit testcase for class ThrowCut.
023: *
024: * @version $Revision: 1.2 $
025: * @author Philippe Schoch
026: */
027: public class ExceptionCutTest extends TestCase {
028:
029: // Define your own AspectManager for setting your own JoinPointManager!
030: public static class MyExtensionManager extends LocalAspectManager {
031: public MyExtensionManager(boolean ic, JVMAspectInterface ai) {
032: super (ic, ai);
033: }
034:
035: protected void createJoinPointManager(boolean ic,
036: JVMAspectInterface ai) {
037: jpm = new MyJoinPointManager(ic, ai, false);
038: if (ic)
039: jpm.connectToJVMAI();
040: }
041:
042: }
043:
044: static class MyJoinPointManager extends JoinPointManager {
045: public MyJoinPointManager(boolean ic, JVMAspectInterface ai,
046: boolean rev) {
047: super (ic, ai, rev);
048: }
049:
050: public Map getReq2listener() {
051: return req2listener;
052: }
053:
054: public Set getClassLoadListeners() {
055: return classLoadListeners;
056: }
057: }
058:
059: static class TestException extends Throwable {
060: public TestException() {
061: super ();
062: }
063:
064: public TestException(String s) {
065: super (s);
066: }
067: }
068:
069: static class ExampleClass {
070: public ExampleClass() {
071: }
072:
073: public static void method1() throws Exception {
074: throw new Exception();
075: }
076:
077: public static void method2() throws TestException {
078: throw new TestException();
079: }
080:
081: public void method3() throws UnknownError {
082: throw new UnknownError();
083: }
084:
085: public void method4() throws IllegalAspectException,
086: ItemManipulationException {
087: throw new IllegalAspectException();
088: }
089:
090: public void method5() throws IOException {
091: int i = 0;
092: if (i == 0)
093: throw new IOException();
094: throw new IllegalAspectException();
095: }
096:
097: public void method6() {
098: throw new RuntimeException();
099: }
100: }
101:
102: public static class TestExtension extends DefaultAspect {
103: public Crosscut c1 = new ThrowCut() {
104: public void THROW_ARGS() {
105: counter1++;
106: }
107:
108: protected PointCutter pointCutter() {
109: return null;
110: }
111:
112: public Class[] potentialCrosscutClasses() {
113: Class[] result = { ExampleClass.class, Object.class,
114: ThrowCut.class, Exception.class,
115: TestException.class, UnknownError.class,
116: IllegalAspectException.class,
117: ItemManipulationException.class };
118: return result;
119: }
120: };
121: }
122:
123: public static class TestExtension2 extends DefaultAspect {
124: public Crosscut c2 = new ThrowCut() {
125: public void THROW_ARGS() {
126: counter2++;
127: eventClass = ((ExceptionJoinPoint) this JoinPoint())
128: .getException().getClass();
129: }
130:
131: protected PointCutter pointCutter() {
132: return null;
133: }
134: };
135:
136: public Class[] getAllClasses() {
137: return ((AbstractCrosscut) c2).potentialCrosscutClasses();
138: }
139: }
140:
141: static int counter2 = 0;
142: ExampleClass exCls;
143: ThrowCut testCrosscut = null;
144: Map myReq2listener = null;
145: static int counter1 = 0;
146: static Class eventClass = null;
147:
148: /**
149: * Construct test with given name.
150: * @param name test name
151: */
152: public ExceptionCutTest(String name) {
153: super (name);
154: }
155:
156: String oldExMgr;
157:
158: /**
159: * Set up fixture.
160: */
161: protected void setUp() {
162: try {
163: oldExMgr = System.getProperty("ch.ethz.prose.EXManager");
164: System
165: .setProperty("ch.ethz.prose.EXManager",
166: "ch.ethz.prose.crosscut.ExceptionCutTest$MyExtensionManager");
167: ProseSystem.startup();
168: } catch (Exception e) {
169: e.printStackTrace();
170: tearDown();
171: Assert.fail("ProseSystem.startup() failed.");
172: }
173: exCls = new ExampleClass();
174: }
175:
176: protected void tearDown() {
177: try {
178: System.err.println("TearingDown" + this );
179: ProseSystem.teardown();
180: System.setProperty("ch.ethz.prose.EXManager", oldExMgr);
181: } catch (Exception e) {
182: Assert.fail("ProseSystem.teardown() failed");
183: }
184: }
185:
186: public void test0020_createRequestsAndEvents() {
187: ProseSystem.getAspectManager().insert(new TestExtension());
188: myReq2listener = ((MyJoinPointManager) ProseSystem
189: .getAspectManager().getJoinPointManager())
190: .getReq2listener();
191: assertTrue("Exactly 5 JoinPoints created", myReq2listener
192: .size() == 5);
193:
194: counter1 = 0;
195: try {
196: ExampleClass.method1();
197: } catch (Throwable e) {
198: }
199: try {
200: ExampleClass.method2();
201: } catch (Throwable e) {
202: }
203: try {
204: exCls.method3();
205: } catch (Throwable e) {
206: }
207: try {
208: exCls.method4();
209: } catch (Throwable e) {
210: }
211: assertEquals("Exactly 4 ExceptionEvents reported", 4, counter1);
212:
213: counter1 = 0;
214: try {
215: exCls.method5();
216: } catch (Throwable e) {
217: }
218: try {
219: exCls.method6();
220: } catch (Throwable e) {
221: }
222: assertTrue("0 ExceptionEvents reported", counter1 == 0);
223: }
224:
225: public void test0030_allLoadedClasses() {
226: Iterator it, il, ir;
227: int removeme = 0;
228: TestExtension2 t2 = new TestExtension2();
229:
230: // 1. we 'test' insert this extension, and get its crosscut request.
231: ProseSystem.getAspectManager().insert(t2);
232: CrosscutRequest joinPointsWeHaveAskedFor = ((Crosscut) (t2
233: .getCrosscuts().get(0))).createRequest();
234: ProseSystem.getAspectManager().withdraw(t2);
235:
236: // 2. we extract from this crosscut request the 'requested Exception classes'
237: Vector requestedExceptionClasses = new Vector();
238: it = joinPointsWeHaveAskedFor.iterator();
239: while (it.hasNext()) {
240: JoinPointRequest jpr = (JoinPointRequest) (it.next());
241: if (jpr instanceof ExceptionThrowRequest)
242: requestedExceptionClasses
243: .add(((ExceptionThrowRequest) jpr)
244: .getExceptionClass());
245: }
246:
247: //2. we add in 'loaded Exception Classes' the exceptions currently loaded --
248: // should be the same as 'joinPoints we have asked for'
249: Class[] allClasses = t2.getAllClasses();
250: Vector loadedExceptionClasses = new Vector();
251: for (int j = 0; j < allClasses.length; j++)
252: if (Throwable.class.isAssignableFrom((allClasses[j])))
253: loadedExceptionClasses.add(allClasses[j]);
254:
255: // 3. now we do the test: do we get exaclty what we have asked for>
256: il = loadedExceptionClasses.iterator();
257: while (il.hasNext())
258: assertTrue("Crosscut request contains loaded class",
259: requestedExceptionClasses.contains(il.next()));
260: ir = requestedExceptionClasses.iterator();
261: while (ir.hasNext())
262:
263: assertTrue("loaded classes contains requested class",
264: loadedExceptionClasses.contains(ir.next()));
265:
266: // Now for every loaded throwable class, an instance of it is thrown
267: // and caught which has the effect of incrementing
268: // the counter in the advice method since all throwable classes are registered for crosscutting.
269:
270: // 4. We create a list of throwable objects;
271: List throwableObjects = new Vector();
272: il = loadedExceptionClasses.iterator();
273:
274: while (il.hasNext()) {
275: Class actualClass = (Class) il.next();
276: try {
277: throwableObjects.add(actualClass.newInstance());
278: } catch (InstantiationException ie) { /* well, this exception is will *not* be thrown */
279: } catch (IllegalAccessException iae) { /* neither will this one */
280: }
281: }
282:
283: // 5. isert the extension, measure the number of thrown exception and then
284: // withdraw the extension. Finally we check that the number
285: // of notifications is the same thing as we have thrown.
286: ProseSystem.getAspectManager().insert(t2);
287:
288: il = throwableObjects.iterator();
289: counter2 = 0;
290: while (il.hasNext()) {
291: Throwable exc = (Throwable) il.next();
292: try {
293: throw exc;
294: } catch (Throwable e) {
295: } // this is what we count
296: }
297: assertEquals("For every Exception Class an event", counter2,
298: throwableObjects.size());
299: ProseSystem.getAspectManager().withdraw(t2);
300: }
301:
302: /**
303: * Test suite.
304: * @return test instance
305: */
306: public static Test suite() {
307: return new TestSuite(ExceptionCutTest.class);
308: }
309:
310: }
311:
312: //======================================================================
313: //
314: // $Log: ExceptionCutTest.java,v $
315: // Revision 1.2 2004/05/12 17:26:51 anicoara
316: // Adapt Junit tests to 3.8.1 version and the new package structure
317: //
318: // Revision 1.1.1.1 2003/07/02 15:30:43 apopovic
319: // Imported from ETH Zurich
320: //
321: // Revision 1.1 2003/05/05 14:03:27 popovici
322: // renaming from runes to prose
323: //
324: // Revision 1.14 2003/04/27 13:08:55 popovici
325: // Specializers renamed to PointCutter
326: //
327: // Revision 1.13 2003/04/17 15:43:47 popovici
328: // crosscuts renamed to 'getCrosscuts'
329: // createCrosscuts renamed to 'crosscuts'
330: //
331: // Revision 1.12 2003/04/17 15:15:16 popovici
332: // Extension->Aspect renaming
333: //
334: // Revision 1.11 2003/04/17 12:49:42 popovici
335: // Refactoring of the crosscut package
336: // ExceptionCut renamed to ThrowCut
337: // McutSignature is now SignaturePattern
338: //
339: // Revision 1.10 2003/04/17 08:46:47 popovici
340: // Important functionality additions
341: // - Cflow specializers
342: // - Restructuring of the MethodCut, SetCut, ThrowCut, and GetCut (they are much smaller)
343: // - Transactional capabilities
344: // - Total refactoring of Specializer evaluation, which permits fine-grained distinction
345: // between static and dynamic specializers.
346: // - Functionality pulled up in abstract classes
347: // - Uniformization of advice methods patterns and names
348: //
349: // Revision 1.9 2003/03/04 18:36:41 popovici
350: // Organization of imprts
351: //
352: // Revision 1.8 2003/03/04 11:25:59 popovici
353: // Important refactorization step (march):
354: // - removal of 'JoinPointEvents'; JoinPoints now have the same function as events
355: // - reimplementation of the JVMAIDebuggerAspectInterface (better performance, coding conventions, removal of ProseVM
356: // structures
357: //
358: // Revision 1.7 2003/01/27 12:52:22 pschoch
359: // Advice-Method now without parameter
360: //
361: // Revision 1.6 2002/11/27 15:10:54 popovici
362: // Refactorisation of the 'test0030_allLoadedClasses' test. Used to
363: // be very unstable (depending on the number of loaded exceptions/and on timing)
364: //
365: // Revision 1.5 2002/11/27 15:04:25 pschoch
366: // test0030_allLoadedClasses changed: Counter removed. Now, assert that event exception class is same as thrown exception class
367: //
368: // Revision 1.4 2002/11/27 11:45:25 pschoch
369: // Bug Fix in test0030_allLoadedClasses counter into two separate counters and catch clause inserted for special case test0030_allLoadedClasses
370: //
371: // Revision 1.3 2002/11/26 17:15:36 pschoch
372: // RootComponent now added (replaces RootComponent now added (replaces old ProseSystem)
373: // ProseSystem now owns and starts the Aspect interface.
374: // ProseSystem now containes a 'test' AspectManager
375: // AspectManager now owns the JoinPointManager.
376: // ExtensionManger can be 'connected' to the JVM, or disconnected. The
377: // JoinPointManager of a connected Ext.Mgr enables joinpoints; the
378: // JoinPointManger of a disconnected Ext.Mgr never enables join-points
379: // Documentation updated accordingly.
380: //
381: // Revision 1.2 2002/10/31 18:26:44 pschoch
382: // Capability of crosscutting Exceptions added to prose.
383: //
|