001: //$Id$
002: //=====================================================================
003: //
004: //(history at end)
005: //
006:
007: package ch.ethz.prose.crosscut;
008:
009: import junit.framework.*;
010: import ch.ethz.prose.DefaultAspect;
011: import ch.ethz.prose.ProseSystem;
012: import ch.ethz.prose.filter.Exceptions;
013: import ch.ethz.prose.filter.PointCutter;
014:
015: /**
016: * JUnit testcase for class CatchCut
017: *
018: * @version $Revision: 1.3 $
019: * @author Angela Nicoara
020: */
021: public class ExceptionCatchCutTest extends TestCase {
022:
023: ExceptionCatchCutTest excCatchCutTest = null;
024: ExampleCatch1 excCatch1;
025: ExampleCatch2 excCatch2;
026: ExampleCatch3 excCatch3;
027: int catchCounter = 0;
028:
029: public class TestException extends Exception {
030: public TestException() {
031: super ();
032: }
033:
034: public TestException(String s) {
035: super (s);
036: }
037: }
038:
039: public class TestException2 extends Exception {
040: public TestException2() {
041: super ();
042: }
043:
044: public TestException2(String s) {
045: super (s);
046: }
047: }
048:
049: public class TestException3 extends Exception {
050: public TestException3() {
051: super ();
052: }
053:
054: public TestException3(String s) {
055: super (s);
056: }
057: }
058:
059: public class TestException4 extends RuntimeException {
060: }
061:
062: public class TestException5 extends RuntimeException {
063: }
064:
065: public class TestException6 extends RuntimeException {
066: }
067:
068: public class ExampleCatch1 {
069: public void throwMethod1() {
070: try {
071: throw new TestException();
072: } catch (TestException e) {
073: /**System.out.println("ExceptionCatchCutTest1"); */
074: }
075: }
076:
077: public void throwMethod2() {
078: try {
079: throw new TestException2();
080: } catch (TestException2 e) {
081: /**System.out.println("ExceptionCatchCutTest2"); */
082: }
083: }
084: }
085:
086: public class ExampleCatch2 {
087: public void throwMethod3() {
088: try {
089: throw new TestException3();
090: } catch (TestException3 e) {
091: /**System.out.println("ExceptionCatchCutTest3"); */
092: }
093: }
094:
095: public void throwMethod4() {
096: try {
097: throw new TestException4();
098: } catch (TestException4 e) {
099: /**System.out.println("ExceptionCatchCutTest4"); */
100: }
101: }
102:
103: public void throwMethod42() {
104: try {
105: throw new TestException();
106: } catch (TestException e) {
107: /**System.out.println("ExceptionCatchCutTest42"); */
108: }
109: }
110: }
111:
112: public class ExampleCatch3 {
113: public void throwMethod5() {
114: try {
115: throw new TestException2("test31 test32");
116: } catch (TestException2 e) {
117: /**System.out.println("ExceptionCatchCutTest5"); */
118: }
119: }
120:
121: public void throwMethod6() {
122: try {
123: throw new TestException("test41 test42");
124: } catch (TestException e) {
125: /**System.out.println("ExceptionCatchCutTest6"); */
126: }
127: }
128:
129: public void throwMethod7() {
130: }
131: }
132:
133: public class TestExtension1 extends DefaultAspect {
134: public Crosscut c1 = new CatchCut() {
135: public void CATCH_ARGS() {
136: catchCounter++;
137: //System.out.println("CATCH_ARGS1 -> catchCounter = " + catchCounter);
138: }
139:
140: protected PointCutter pointCutter() {
141: //System.out.println("TestExtension1 -> pointCutter CATCH1");
142: return (Exceptions.type(".*TestException")); // - check all the classes that contain the word "TestException"
143: //return ( Exceptions.type("TestException") ); // - check all the classes that contain the word "TestException"
144: //return ( Exceptions.withMessage("3|4") ); // - check all the exceptions that contain the messages "3" or "4"
145: //return ( Within.type("Example.*") ); //OK
146: //return ( Within.type(".*Example") ); // - check all the classes that contain the word "Example"
147: //return ( Within.type("Example") ); // - check all the classes that contain the word "Example"
148: //return ( Within.type("ExampleCatch1") ); //OK
149: //return ( Within.method("Method1|Method3") ); // - check all the methods that contain the words "Method1" or "Method3"
150: //return ( Within.method("Method4") ); // - check all the methods that contain the words "Method4"
151: //return null;
152: }
153: };
154:
155: // the second crosscut for "CatchCut"
156: public Crosscut c11 = new CatchCut() {
157: public void CATCH_ARGS() {
158: catchCounter++;
159: //System.out.println("CATCH_ARGS11 -> catchCounter = " + catchCounter);
160: }
161:
162: protected PointCutter pointCutter() {
163: //System.out.println("TestExtension1 -> pointCutter CATCH11");
164: //return ( Exceptions.subtypeOf(java.lang.Exception.class ) ); //OK
165: return (Exceptions
166: .subtypeOf(java.lang.RuntimeException.class)); //OK
167: //return ( Exceptions.subtypeOf(java.lang.IllegalArgumentException.class ) ); //OK
168: //return null;
169: }
170: };
171: }
172:
173: public class TestExtension2 extends DefaultAspect {
174: public Crosscut c2 = new CatchCut() {
175: public void CATCH_ARGS() {
176: catchCounter++;
177: System.out.println("CATCH_ARGS2 -> catchCounter = "
178: + catchCounter);
179: }
180:
181: protected PointCutter pointCutter() {
182: //System.out.println("TestExtension2 -> pointCutter CATCH2");
183: //return ( Exceptions.subtypeOf(java.lang.Exception.class ) ); //OK
184: return (Exceptions
185: .subtypeOf(java.lang.RuntimeException.class)); //OK
186: //return ( Exceptions.subtypeOf(java.lang.IllegalArgumentException.class ) ); //OK
187: //return null;
188: }
189: };
190: }
191:
192: public class TestExtension3 extends DefaultAspect {
193: public Crosscut c3 = new ThrowCut() {
194: public void THROW_ARGS() {
195: catchCounter++;
196: //System.out.println("THROW_ARGS -> catchCounter = " + catchCounter);
197: }
198:
199: protected PointCutter pointCutter() {
200: //System.out.println("TestExtension3 -> pointCutter THROW");
201: //return ( Exceptions.type(".*Exception") ); //OK
202: return (Exceptions.type("Exception")); // - check all the classes that contain the word "Exception"
203: //return null;
204: }
205: };
206: }
207:
208: /**
209: * Construct test with given name.
210: * @param name test name
211: */
212: public ExceptionCatchCutTest(String name) {
213: super (name);
214: }
215:
216: /**
217: * Set up fixture.
218: */
219: protected void setUp() {
220: try {
221: ProseSystem.startup();
222: } catch (Exception e) {
223: e.printStackTrace();
224: tearDown();
225: Assert.fail("ProseSystem.startup() failed");
226: }
227:
228: excCatch1 = new ExampleCatch1();
229: excCatch2 = new ExampleCatch2();
230: excCatch3 = new ExampleCatch3();
231: }
232:
233: protected void tearDown() {
234: try {
235: //System.err.println("TearingDown");
236: ProseSystem.teardown();
237: } catch (Exception e) {
238: Assert.fail("ProseSystem.teardown() failed");
239: }
240: }
241:
242: public void test0001_createRequestsAndEvents() {
243: // load the following classes in JVM
244: Class toload;
245: toload = TestException.class;
246: toload = TestException2.class;
247: toload = TestException3.class;
248: toload = TestException4.class;
249: toload = TestException5.class;
250: toload = TestException6.class;
251:
252: TestExtension1 myExtension1 = new TestExtension1();
253: ProseSystem.getAspectManager().insert(myExtension1);
254: //System.err.println("ExceptionCatchCutTest - test0001_createRequestsAndEvents -> " + myExtension1.c1.createRequest());
255:
256: //ProseSystem.getAspectManager().insert(new TestExtension2());
257:
258: //TestExtension3 myExtension3 = new TestExtension3();
259: //ProseSystem.getAspectManager().insert(myExtension3);
260: //System.err.println("ExceptionCatchCutTest - test0001_createRequestsAndEvents -> " + myExtension3.c3.createRequest());
261:
262: //System.out.println("before -> " + catchCounter);
263:
264: excCatch1.throwMethod1();
265: excCatch1.throwMethod2();
266: excCatch2.throwMethod3();
267: excCatch2.throwMethod4();
268: excCatch2.throwMethod42();
269: excCatch3.throwMethod5();
270: excCatch3.throwMethod6();
271: excCatch3.throwMethod7();
272:
273: //System.out.println("after -> " + catchCounter);
274:
275: assertEquals("6 ExceptionCatchEvents reported ", 8,
276: catchCounter);
277: }
278:
279: /**
280: * Test suite.
281: * @return test instance
282: */
283: public static Test suite() {
284: return new TestSuite(ExceptionCatchCutTest.class);
285: }
286: }
287:
288: //======================================================================
289: //
290: //$Log$
291: //
|