KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > beforeafter > BeforeAfterThrowingTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.aop.beforeafter;
23
24 import org.jboss.test.aop.AOPTestWithSetup;
25
26 /**
27  *
28  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
29  * @version $Revision: 45977 $
30  */

31 public class BeforeAfterThrowingTestCase extends AOPTestWithSetup
32 {
33    public BeforeAfterThrowingTestCase(String JavaDoc arg0)
34    {
35       // FIXME BeforeAfterThrowingTestCase constructor
36
super(arg0);
37    }
38
39    public void testSimple() throws Exception JavaDoc
40    {
41       System.out.println("=== TESING SIMPLE");
42       POJO pojo = new POJO();
43       
44       System.out.println("* Calling method with error=false");
45       SimpleAspect.clear();
46       assertEquals(1, pojo.method(false));
47       assertTrue(SimpleAspect.before);
48       assertTrue(SimpleAspect.around);
49       assertTrue(SimpleAspect.after);
50       assertFalse(SimpleAspect.throwing);
51       
52       
53       System.out.println("* Calling method with error=true");
54       SimpleAspect.clear();
55       try
56       {
57          pojo.method(true);
58          assertFalse("Should not get here", true);
59       }
60       catch(TestException e)
61       {
62          
63       }
64       
65       assertTrue(SimpleAspect.before);
66       assertTrue(SimpleAspect.around);
67       assertFalse(SimpleAspect.after);
68       assertTrue(SimpleAspect.throwing);
69    }
70
71    public void testArgs() throws Exception JavaDoc
72    {
73       System.out.println("=== TESTING WITH ARGUMENTS");
74       
75
76       System.out.println("* Testing ctor(boolean, int, long, String) with exception");
77       ArgsAspect.clear();
78       SimpleAspect.clear();
79       try
80       {
81          POJO pojo = new POJO(true, 1, 1, "x");
82          throw new RuntimeException JavaDoc("TestException not thrown");
83       }
84       catch (TestException e)
85       {
86           assertEquals("before1", ArgsAspect.before);
87           assertNull(ArgsAspect.after);
88           assertEquals("throwing1", ArgsAspect.throwing);
89       }
90       
91       System.out.println("* Testing ctor(boolean, int, long, String)");
92       ArgsAspect.clear();
93       POJO pojo = new POJO(false, 2, 3, "y");
94       assertEquals("before1", ArgsAspect.before);
95       assertEquals("after2", ArgsAspect.after);
96       assertNull(ArgsAspect.throwing);
97       
98       
99       System.out.println("* Testing method()");
100       ArgsAspect.clear();
101       pojo.method();
102       assertNull(ArgsAspect.before);
103       assertNull(ArgsAspect.after);
104       assertNull(ArgsAspect.throwing);
105       
106       assertFalse(SimpleAspect.before);
107       assertFalse(SimpleAspect.around);
108       assertFalse(SimpleAspect.after);
109       assertFalse(SimpleAspect.throwing);
110       
111
112       System.out.println("* Testing method(boolean) with exception");
113       ArgsAspect.clear();
114       SimpleAspect.clear();
115       try
116       {
117          pojo.method(true);
118          throw new RuntimeException JavaDoc("TestException not thrown");
119       }
120       catch (TestException e)
121       {
122       }
123       assertNull(ArgsAspect.before);
124       assertNull(ArgsAspect.after);
125       assertEquals("throwing2", ArgsAspect.throwing);
126       assertTrue(SimpleAspect.before);
127       assertTrue(SimpleAspect.around);
128       assertFalse(SimpleAspect.after);
129       assertTrue(SimpleAspect.throwing);
130
131       System.out.println("* Testing method(boolean)");
132       ArgsAspect.clear();
133       SimpleAspect.clear();
134       pojo.method(false);
135       assertNull(ArgsAspect.before);
136       assertNull(ArgsAspect.after);
137       assertNull(ArgsAspect.throwing);
138       assertTrue(SimpleAspect.before);
139       assertTrue(SimpleAspect.around);
140       assertTrue(SimpleAspect.after);
141       assertFalse(SimpleAspect.throwing);
142
143
144       SimpleAspect.clear();
145       System.out.println("* Testing method(boolean, int, long, String) with exception");
146       ArgsAspect.clear();
147       try
148       {
149          pojo.method(true, 10, 100L, "abc");
150          throw new RuntimeException JavaDoc("TestException not thrown");
151       }
152       catch (TestException e)
153       {
154       }
155
156       System.out.println("* Testing method(boolean, int, long, String)");
157       ArgsAspect.clear();
158       pojo.method(false, 10, 100L, "abc");
159       
160
161       System.out.println("* Testing method(boolean, int, long, String, int) with exception");
162       ArgsAspect.clear();
163       try
164       {
165          POJO ret = pojo.method(true, 10, 100L, "abc", 25);
166          throw new RuntimeException JavaDoc("TestException not thrown");
167       }
168       catch (TestException e)
169       {
170       }
171       
172       assertEquals("before2", ArgsAspect.before);
173       assertNull(ArgsAspect.after);
174       assertEquals("throwing3", ArgsAspect.throwing);
175
176       System.out.println("* Testing method(boolean, int, long, String, int)");
177       ArgsAspect.clear();
178       POJO ret = pojo.method(false, 10, 100L, "abc", 25);
179       
180       assertNotNull(ret);
181       assertEquals("before2", ArgsAspect.before);
182       assertEquals("after1", ArgsAspect.after);
183       assertNull(ArgsAspect.throwing);
184       
185       assertFalse(SimpleAspect.before);
186       assertFalse(SimpleAspect.around);
187       assertFalse(SimpleAspect.after);
188       assertFalse(SimpleAspect.throwing);
189    }
190
191    public void testSimpleFields() throws Exception JavaDoc
192    {
193       System.out.println("=== TESTING Fields");
194       POJO pojo = new POJO();
195
196       ArgsAspect.clear();
197       System.out.println("* Writing i");
198       pojo.i = 5;
199       assertEquals("before3", ArgsAspect.before);
200       assertNull(ArgsAspect.throwing);
201       assertEquals("after3", ArgsAspect.after);
202       
203       ArgsAspect.clear();
204       System.out.println("* Reading i");
205       int i = pojo.i;
206       assertEquals("before4", ArgsAspect.before);
207       assertNull(ArgsAspect.throwing);
208       assertEquals("after3", ArgsAspect.after);
209    }
210       
211    public void testFieldsWithInheritance() throws Exception JavaDoc
212    {
213       System.out.println("=== TESTING Fields with inheritance");
214       POJO pojo = new POJO();
215
216       ArgsAspect.clear();
217       System.out.println("* Writing superValue");
218       pojo.superValue = new SuperValue(5);
219       assertEquals("before4", ArgsAspect.before);
220       assertNull(ArgsAspect.throwing);
221       assertEquals("after5", ArgsAspect.after);
222
223       ArgsAspect.clear();
224       System.out.println("* Reading superValue");
225       SuperValue superVal = pojo.superValue;
226       assertEquals("before4", ArgsAspect.before);
227       assertNull(ArgsAspect.throwing);
228       assertEquals("after5", ArgsAspect.after);
229       assertEquals(20, superVal.getValue()); //The after4 and after5 methods double the value
230

231       
232       ArgsAspect.clear();
233       System.out.println("* Writing subValue");
234       pojo.subValue = new SubValue(5);
235       assertEquals("before5", ArgsAspect.before);
236       assertNull(ArgsAspect.throwing);
237       assertEquals("after4", ArgsAspect.after);
238       
239       ArgsAspect.clear();
240       System.out.println("* Writing subValue");
241       SubValue subVal = pojo.subValue;
242       assertEquals("before4", ArgsAspect.before);
243       assertNull(ArgsAspect.throwing);
244       assertEquals("after4", ArgsAspect.after);
245       assertEquals(20, subVal.getValue());//The after4 methods double the value
246
}
247    
248    public void testMethodsWithInheritance()
249    {
250       System.out.println("=== TESTING METHODS WITH INHERITANCE OF RETURN AND PARAMETERS");
251
252       GeneralAspect.clear();
253       System.out.println(" * Testing constructor(SuperValue, int)");
254       SuperValue superValue = new SuperValue(5);
255       POJO pojo = new POJO(superValue, 5);
256       assertEquals("before", GeneralAspect.before);
257       assertNull(ArgsAspect.throwing);
258       assertEquals("after", GeneralAspect.after);
259       
260       GeneralAspect.clear();
261       System.out.println(" * Testing constructor(SubValue, int)");
262       SubValue subValue = new SubValue(6);
263       POJO pojo2 = new POJO(subValue, 6);
264       assertEquals("before", GeneralAspect.before);
265       assertNull(ArgsAspect.throwing);
266       assertEquals("after", GeneralAspect.after);
267       
268       GeneralAspect.clear();
269       System.out.println(" * Testing method(SubValue, int)");
270       subValue = pojo.method(subValue, 5);
271       assertEquals("before", GeneralAspect.before);
272       assertNull(ArgsAspect.throwing);
273       assertEquals("after", GeneralAspect.after);
274       
275       GeneralAspect.clear();
276       System.out.println(" * Testing method(SuperValue, int)");
277       superValue = pojo.method(superValue, 10);
278       assertEquals("before", GeneralAspect.before);
279       assertNull(ArgsAspect.throwing);
280       assertEquals("after", GeneralAspect.after);
281      
282       ArgsAspect.clear();
283       System.out.println(" * Testing method(SubValue, SubValue)");
284       SuperValue ret = pojo.method(new SubValue(5), new SubValue(6));
285       assertEquals("before6", ArgsAspect.before);
286       assertNull(ArgsAspect.throwing);
287       assertEquals("after6", ArgsAspect.after);
288       
289       ArgsAspect.clear();
290       System.out.println(" * Testing method(SuperValue, SubValue)");
291       pojo.method(new SuperValue(7), new SubValue(8));
292       assertEquals("before7", ArgsAspect.before);
293       assertNull(ArgsAspect.throwing);
294       assertEquals("after7", ArgsAspect.after);
295    }
296 }
297
Popular Tags