KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > crosscut > ExceptionCatchCutTest


1 package ch.ethz.prose.crosscut;
2
3 import junit.framework.*;
4 import ch.ethz.prose.DefaultAspect;
5 import ch.ethz.prose.ProseSystem;
6 import ch.ethz.prose.filter.Exceptions;
7 import ch.ethz.prose.filter.PointCutter;
8
9
10 /**
11  * JUnit testcase for class CatchCut
12  *
13  * @version $Revision: 1.3 $
14  * @author Angela Nicoara
15  */

16 public class ExceptionCatchCutTest extends TestCase {
17
18       ExceptionCatchCutTest excCatchCutTest = null;
19       ExampleCatch1 excCatch1;
20       ExampleCatch2 excCatch2;
21       ExampleCatch3 excCatch3;
22       int catchCounter = 0;
23
24
25       public class TestException extends Exception JavaDoc {
26         public TestException() {super(); }
27         public TestException(String JavaDoc s) {super(s);}
28       }
29
30       public class TestException2 extends Exception JavaDoc {
31         public TestException2() {super(); }
32         public TestException2(String JavaDoc s) {super(s);}
33       }
34     
35       public class TestException3 extends Exception JavaDoc {
36         public TestException3() {super(); }
37         public TestException3(String JavaDoc s) {super(s);}
38       }
39     
40       public class TestException4 extends RuntimeException JavaDoc { }
41       public class TestException5 extends RuntimeException JavaDoc { }
42       public class TestException6 extends RuntimeException JavaDoc { }
43     
44       public class ExampleCatch1
45       {
46         public void throwMethod1() { try{ throw new TestException(); } catch(TestException e) { /**System.out.println("ExceptionCatchCutTest1"); */ } }
47         public void throwMethod2() { try{ throw new TestException2(); } catch(TestException2 e) { /**System.out.println("ExceptionCatchCutTest2"); */ } }
48       }
49     
50       public class ExampleCatch2
51       {
52         public void throwMethod3(){ try{ throw new TestException3(); } catch(TestException3 e) { /**System.out.println("ExceptionCatchCutTest3"); */ } }
53         public void throwMethod4(){ try{ throw new TestException4(); } catch(TestException4 e) { /**System.out.println("ExceptionCatchCutTest4"); */ } }
54         public void throwMethod42(){ try{ throw new TestException(); } catch(TestException e) { /**System.out.println("ExceptionCatchCutTest42"); */ } }
55       }
56     
57       public class ExampleCatch3
58       {
59         public void throwMethod5(){ try{ throw new TestException2("test31 test32"); } catch(TestException2 e) { /**System.out.println("ExceptionCatchCutTest5"); */} }
60         public void throwMethod6(){ try{ throw new TestException("test41 test42"); } catch(TestException e) { /**System.out.println("ExceptionCatchCutTest6"); */ } }
61         public void throwMethod7() { }
62       }
63
64       public class TestExtension1 extends DefaultAspect
65       {
66         public Crosscut c1 = new CatchCut()
67         {
68             public void CATCH_ARGS()
69             {
70                 catchCounter++;
71                 //System.out.println("CATCH_ARGS1 -> catchCounter = " + catchCounter);
72
}
73     
74             protected PointCutter pointCutter()
75             {
76                 //System.out.println("TestExtension1 -> pointCutter CATCH1");
77
return ( Exceptions.type(".*TestException") ); // - check all the classes that contain the word "TestException"
78
//return ( Exceptions.type("TestException") ); // - check all the classes that contain the word "TestException"
79
//return ( Exceptions.withMessage("3|4") ); // - check all the exceptions that contain the messages "3" or "4"
80
//return ( Within.type("Example.*") ); //OK
81
//return ( Within.type(".*Example") ); // - check all the classes that contain the word "Example"
82
//return ( Within.type("Example") ); // - check all the classes that contain the word "Example"
83
//return ( Within.type("ExampleCatch1") ); //OK
84
//return ( Within.method("Method1|Method3") ); // - check all the methods that contain the words "Method1" or "Method3"
85
//return ( Within.method("Method4") ); // - check all the methods that contain the words "Method4"
86
//return null;
87
}
88         };
89         
90         // the second crosscut for "CatchCut"
91
public Crosscut c11 = new CatchCut()
92         {
93             public void CATCH_ARGS()
94             {
95                 catchCounter++;
96                 //System.out.println("CATCH_ARGS11 -> catchCounter = " + catchCounter);
97
}
98     
99             protected PointCutter pointCutter()
100             {
101                 //System.out.println("TestExtension1 -> pointCutter CATCH11");
102
//return ( Exceptions.subtypeOf(java.lang.Exception.class ) ); //OK
103
return ( Exceptions.subtypeOf(java.lang.RuntimeException JavaDoc.class ) ); //OK
104
//return ( Exceptions.subtypeOf(java.lang.IllegalArgumentException.class ) ); //OK
105
//return null;
106
}
107         };
108       }
109
110       public class TestExtension2 extends DefaultAspect
111       {
112         public Crosscut c2 = new CatchCut()
113         {
114             public void CATCH_ARGS()
115             {
116                 catchCounter++;
117                 System.out.println("CATCH_ARGS2 -> catchCounter = " + catchCounter);
118             }
119     
120             protected PointCutter pointCutter()
121             {
122                 //System.out.println("TestExtension2 -> pointCutter CATCH2");
123
//return ( Exceptions.subtypeOf(java.lang.Exception.class ) ); //OK
124
return ( Exceptions.subtypeOf(java.lang.RuntimeException JavaDoc.class ) ); //OK
125
//return ( Exceptions.subtypeOf(java.lang.IllegalArgumentException.class ) ); //OK
126
//return null;
127
}
128         };
129       }
130
131
132       public class TestExtension3 extends DefaultAspect
133       {
134         public Crosscut c3 = new ThrowCut()
135         {
136             public void THROW_ARGS()
137             {
138                 catchCounter++;
139                 //System.out.println("THROW_ARGS -> catchCounter = " + catchCounter);
140
}
141     
142             protected PointCutter pointCutter()
143             {
144                 //System.out.println("TestExtension3 -> pointCutter THROW");
145
//return ( Exceptions.type(".*Exception") ); //OK
146
return ( Exceptions.type("Exception") ); // - check all the classes that contain the word "Exception"
147
//return null;
148
}
149         };
150       }
151
152
153     /**
154      * Construct test with given name.
155      * @param name test name
156      */

157     public ExceptionCatchCutTest (String JavaDoc name)
158     {
159         super(name);
160     }
161
162     /**
163      * Set up fixture.
164      */

165     protected void setUp()
166     {
167         try{
168             ProseSystem.startup();
169         }
170         catch (Exception JavaDoc e)
171         {
172             e.printStackTrace();
173             tearDown();
174             Assert.fail("ProseSystem.startup() failed");
175         }
176
177         excCatch1 = new ExampleCatch1();
178         excCatch2 = new ExampleCatch2();
179         excCatch3 = new ExampleCatch3();
180     }
181
182     protected void tearDown()
183     {
184         try {
185             //System.err.println("TearingDown");
186
ProseSystem.teardown();
187         }
188         catch (Exception JavaDoc e) {
189             Assert.fail("ProseSystem.teardown() failed");
190         }
191     }
192
193
194     public void test0001_createRequestsAndEvents()
195     {
196         // load the following classes in JVM
197
Class JavaDoc toload;
198         toload = TestException.class;
199         toload = TestException2.class;
200         toload = TestException3.class;
201         toload = TestException4.class;
202         toload = TestException5.class;
203         toload = TestException6.class;
204
205         TestExtension1 myExtension1 = new TestExtension1();
206         ProseSystem.getAspectManager().insert(myExtension1);
207         //System.err.println("ExceptionCatchCutTest - test0001_createRequestsAndEvents -> " + myExtension1.c1.createRequest());
208

209         //ProseSystem.getAspectManager().insert(new TestExtension2());
210

211         //TestExtension3 myExtension3 = new TestExtension3();
212
//ProseSystem.getAspectManager().insert(myExtension3);
213
//System.err.println("ExceptionCatchCutTest - test0001_createRequestsAndEvents -> " + myExtension3.c3.createRequest());
214

215         //System.out.println("before -> " + catchCounter);
216

217         excCatch1.throwMethod1();
218         excCatch1.throwMethod2();
219         excCatch2.throwMethod3();
220         excCatch2.throwMethod4();
221         excCatch2.throwMethod42();
222         excCatch3.throwMethod5();
223         excCatch3.throwMethod6();
224         excCatch3.throwMethod7();
225
226         //System.out.println("after -> " + catchCounter);
227

228         assertEquals("6 ExceptionCatchEvents reported ", 8,catchCounter);
229   }
230
231   /**
232    * Test suite.
233    * @return test instance
234    */

235   public static Test suite()
236   {
237         return new TestSuite(ExceptionCatchCutTest.class);
238   }
239 }
240
Popular Tags