KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > callerscope > CallerScopeTestCase


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.callerscope;
23
24 import org.jboss.aop.advice.Scope;
25 import org.jboss.test.aop.AOPTestWithSetup;
26 import org.jboss.util.collection.WeakIdentityHashMap;
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29 import junit.textui.TestRunner;
30
31 /**
32  *
33  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
34  * @version $Revision: 45977 $
35  */

36 public class CallerScopeTestCase extends AOPTestWithSetup
37 {
38    Caller caller = new Caller();
39    
40    public static void main(String JavaDoc[] args)
41    {
42       TestRunner.run(suite());
43    }
44
45    public static Test suite()
46    {
47       TestSuite suite = new TestSuite("DotInPointcutNameTester");
48       suite.addTestSuite(CallerScopeTestCase.class);
49       return suite;
50    }
51
52    public CallerScopeTestCase(String JavaDoc name)
53    {
54       super(name);
55    }
56    
57    public void testWeakIdentityHashMap()
58    {
59       System.out.println("*** testWeakIdentityHashMap()");
60       
61       //NOTE: - if we allocated the strings as:
62
// String a = "a";
63
// String b = "a";
64
// String c = "a";
65
// String d = "a";
66
//
67
// we would end up with only one entry in the map due to the fact that when
68
// allocated as above all the following expressions would return true
69
// (since "a" lives in the constant pool of the class, so they all use the
70
// same reference):
71
// a==b
72
// b==c
73
// c==d
74

75       String JavaDoc a = new String JavaDoc("a");
76       String JavaDoc b = new String JavaDoc("a");
77       String JavaDoc c = new String JavaDoc("a");
78       String JavaDoc d = new String JavaDoc("a");
79       Long JavaDoc l1 = new Long JavaDoc(1);
80       Long JavaDoc l2 = new Long JavaDoc(2);
81       Long JavaDoc l3 = new Long JavaDoc(3);
82       Long JavaDoc l4 = new Long JavaDoc(4);
83
84       WeakIdentityHashMap map = new WeakIdentityHashMap();
85       map.put(a, l1);
86       map.put(b, l2);
87       map.put(c, l3);
88       map.put(d, l4);
89       
90       assertEquals(4, map.size());
91       System.gc();
92       assertEquals(4, map.size());
93       
94       assertEquals(l1, map.get(a));
95       assertEquals(l2, map.get(b));
96       assertEquals(l3, map.get(c));
97       assertEquals(l4, map.get(d));
98       
99       c = null;
100       d = null;
101       System.gc();
102       assertEquals(2, map.size());
103
104       assertEquals(l1, map.get(a));
105       assertEquals(l2, map.get(b));
106
107       System.out.println("*** testWeakIdentityHashMap() - successful");
108       
109    }
110    
111    public void testPerVm()throws Exception JavaDoc
112    {
113       System.out.println("*** testPerVm");
114       clearInterceptions();
115       POJO pojo = new POJO();
116       checkInterceptions(Scope.PER_VM);
117       
118       clearInterceptions();
119       pojo.perVmMethod();
120       checkInterceptions(Scope.PER_VM);
121       System.out.println();
122    }
123
124    public void testPerClass()throws Exception JavaDoc
125    {
126       System.out.println("*** testPerClass");
127       clearInterceptions();
128       POJO pojo = new POJO(1);
129       checkInterceptions(Scope.PER_CLASS);
130
131       clearInterceptions();
132       pojo.perClassMethod();
133       checkInterceptions(Scope.PER_CLASS);
134       System.out.println();
135    }
136
137    public void testPerInstance()throws Exception JavaDoc
138    {
139       System.out.println("*** testPerInstance");
140       clearInterceptions();
141       POJO pojo = new POJO();
142       checkInterceptions(Scope.PER_VM);
143       
144       clearInterceptions();
145       pojo.perInstanceMethod();
146       checkInterceptions(Scope.PER_INSTANCE);
147       
148       caller.testPerInstance(pojo);
149       System.out.println();
150    }
151    
152
153    public void testPerJoinpoint()throws Exception JavaDoc
154    {
155       System.out.println("*** testPerJoinpoint");
156       clearInterceptions();
157       POJO pojo = new POJO();
158       checkInterceptions(Scope.PER_VM);
159
160       clearInterceptions();
161       pojo.perJoinpointMethod();
162       checkInterceptions(Scope.PER_JOINPOINT);
163       System.out.println();
164    }
165
166    public void testPerClassJoinpoint()throws Exception JavaDoc
167    {
168       System.out.println("*** testPerClassJoinpoint");
169       clearInterceptions();
170       POJO pojo = new POJO(false);
171       checkInterceptions(Scope.PER_CLASS_JOINPOINT);
172
173       clearInterceptions();
174       pojo.perClassJoinpointMethod();
175       checkInterceptions(Scope.PER_CLASS_JOINPOINT);
176       System.out.println();
177    }
178    
179    static void clearInterceptions()
180    {
181       PerVmAspect.intercepted = false;
182       PerVmInterceptor.intercepted = false;
183       PerClassAspect.intercepted = false;
184       PerClassInterceptor.intercepted = false;
185       PerInstanceAspect.intercepted = false;
186       PerInstanceInterceptor.intercepted = false;
187       PerJoinpointAspect.intercepted = false;
188       PerJoinpointInterceptor.intercepted = false;
189       PerClassJoinpointAspect.intercepted = false;
190       PerClassJoinpointInterceptor.intercepted = false;
191    }
192    
193    
194    static void checkInterceptions(Scope scope)
195    {
196       if (scope == Scope.PER_VM)
197       {
198          checkInterceptions(true, true, false, false, false, false, false, false, false, false);
199       }
200       else if (scope == Scope.PER_CLASS)
201       {
202          checkInterceptions(false, false, true, true, false, false, false, false, false, false);
203       }
204       else if (scope == Scope.PER_INSTANCE)
205       {
206          checkInterceptions(false, false, false, false, true, true, false, false, false, false);
207       }
208       else if (scope == Scope.PER_JOINPOINT)
209       {
210          checkInterceptions(false, false, false, false, false, false, true, true, false, false);
211       }
212       else if (scope == Scope.PER_CLASS_JOINPOINT)
213       {
214          checkInterceptions(false, false, false, false, false, false, false, false, true, true);
215       }
216    }
217    
218    static void checkInterceptions(
219          boolean perVmAspect,
220          boolean perVmInterceptor,
221          boolean perClassAspect,
222          boolean perClassInterceptor,
223          boolean perInstanceAspect,
224          boolean perInstanceInterceptor,
225          boolean perJoinpointAspect,
226          boolean perJoinpointInterceptor,
227          boolean perClassJoinpointAspect,
228          boolean perClassJoinpointInterceptor)
229    {
230       assertEquals("Wrong intercepted value for PerVMAspect", perVmAspect, PerVmAspect.intercepted);
231       assertEquals("Wrong intercepted value for PerVmInterceptor", perVmInterceptor, PerVmInterceptor.intercepted);
232       assertEquals("Wrong intercepted value for PerClassAspect", perClassAspect, PerClassAspect.intercepted);
233       assertEquals("Wrong intercepted value for PerClassInterceptor", perClassInterceptor, PerClassInterceptor.intercepted);
234       assertEquals("Wrong intercepted value for PerInstanceAspect", perInstanceAspect, PerInstanceAspect.intercepted);
235       assertEquals("Wrong intercepted value for PerInstanceInterceptor", perInstanceInterceptor, PerInstanceInterceptor.intercepted);
236       assertEquals("Wrong intercepted value for PerJoinpointAspect", perJoinpointAspect, PerJoinpointAspect.intercepted);
237       assertEquals("Wrong intercepted value for PerJoinpointInterceptor", perJoinpointInterceptor, PerJoinpointInterceptor.intercepted);
238       assertEquals("Wrong intercepted value for PerClassJoinpointAspect", perClassJoinpointAspect, PerClassJoinpointAspect.intercepted);
239       assertEquals("Wrong intercepted value for PerClassJoinpointInterceptor", perClassJoinpointInterceptor, PerClassJoinpointInterceptor.intercepted);
240    }
241    
242 }
243
Popular Tags