KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > basic > CallingPOJO


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.basic;
23
24 import org.jboss.aop.Advised;
25
26 /**
27  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
28  * @version $Revision: 37406 $
29  */

30 public class CallingPOJO
31 {
32    POJO pojo;
33    NonadvisedPOJO nonpojo;
34    PrivateProtectedPOJO privateProtectedPojo;
35
36    public CallingPOJO()
37    {
38       CallerInterceptor.called = false;
39       pojo = new POJO();
40       if (!CallerInterceptor.called)
41       {
42          throw new RuntimeException JavaDoc("constructor caller interceptor didn't work from within constructor");
43       }
44       CallerInterceptor.called = false;
45       pojo.someMethod();
46       if (!CallerInterceptor.called)
47       {
48          throw new RuntimeException JavaDoc("caller interceptor didn't work");
49       }
50
51       CallerInterceptor.called = false;
52       POJO.staticMethod();
53       if (!CallerInterceptor.called)
54       {
55          throw new RuntimeException JavaDoc("caller interceptor didn't work");
56       }
57
58       CallerInterceptor.called = false;
59       nonpojo = new NonadvisedPOJO("helloworld");
60       if (!CallerInterceptor.called)
61       {
62          throw new RuntimeException JavaDoc("constructor caller interceptor didn't work");
63       }
64       CallerInterceptor.called = false;
65       nonpojo.remoteTest();
66       if (!CallerInterceptor.called)
67       {
68          throw new RuntimeException JavaDoc("caller interceptor didn't work");
69       }
70       if (nonpojo instanceof Advised)
71       {
72          throw new RuntimeException JavaDoc("nonpojo is Advised when it shouldn't be");
73       }
74       
75       //Call twice to make sure method names have been cached properly
76
privateProtectedPojo = new PrivateProtectedPOJO();
77       CallerInterceptor.called = false;
78       privateProtectedPojo = new PrivateProtectedPOJO();
79       if (!CallerInterceptor.called)
80       {
81          throw new RuntimeException JavaDoc("protected constructor caller interceptor didn't work");
82       }
83       
84       //Call twice to make sure method names have been cached properly
85
privateProtectedPojo.protectedMethod();
86       CallerInterceptor.called = false;
87       privateProtectedPojo.protectedMethod();
88       if (!CallerInterceptor.called)
89       {
90          throw new RuntimeException JavaDoc("protected method caller interceptor didn't work");
91       }
92
93       CallerInterceptor.called = false;
94       PrivateProtectedPOJO.protectedStaticMethod();
95       if (!CallerInterceptor.called)
96       {
97          throw new RuntimeException JavaDoc("protected static method caller interceptor didn't work");
98       }
99
100       CallerInterceptor.called = false ;
101       final InterfacePojo interfacePojo = new InterfacePojo() ;
102       final SuperInterface superInterface = interfacePojo ;
103       superInterface.superInterfaceMethod() ;
104       if (!CallerInterceptor.called)
105       {
106          throw new RuntimeException JavaDoc("interface method via interface caller interceptor didn't work") ;
107       }
108
109       CallerInterceptor.called = false ;
110       final SubInterface subInterface = interfacePojo ;
111       subInterface.superInterfaceMethod() ;
112       if (!CallerInterceptor.called)
113       {
114          throw new RuntimeException JavaDoc("interface method via sub interface caller interceptor didn't work") ;
115       }
116    }
117
118    /**
119     * This method should be a caller pointcut
120     */

121    public void callSomeMethod()
122    {
123       CallerInterceptor.called = false;
124       pojo = new POJO();
125       if (!CallerInterceptor.called)
126       {
127          throw new RuntimeException JavaDoc("constructor caller interceptor didn't work within method");
128       }
129       CallerInterceptor.called = false;
130       POJO.staticMethod();
131       if (!CallerInterceptor.called)
132       {
133          throw new RuntimeException JavaDoc("caller interceptor didn't work");
134       }
135       CallerInterceptor.called = false;
136       pojo.someMethod();
137       if (!CallerInterceptor.called)
138       {
139          throw new RuntimeException JavaDoc("caller interceptor didn't work");
140       }
141
142       //Call twice to make sure method names have been cached properly
143
privateProtectedPojo = new PrivateProtectedPOJO();
144       CallerInterceptor.called = false;
145       privateProtectedPojo = new PrivateProtectedPOJO();
146       if (!CallerInterceptor.called)
147       {
148          throw new RuntimeException JavaDoc("protected constructor caller interceptor didn't work");
149       }
150
151       //Call twice to make sure method names have been cached properly
152
privateProtectedPojo.protectedMethod();
153       CallerInterceptor.called = false;
154       privateProtectedPojo.protectedMethod();
155       if (!CallerInterceptor.called)
156       {
157          throw new RuntimeException JavaDoc("protected method caller interceptor didn't work");
158       }
159
160       CallerInterceptor.called = false;
161       PrivateProtectedPOJO.callProtectedStaticMethod();
162       if (!CallerInterceptor.called)
163       {
164          throw new RuntimeException JavaDoc("protected static method caller interceptor didn't work");
165       }
166
167       CallerInterceptor.called = false;
168       privateProtectedPojo.callProtectedMethod();
169       if (!CallerInterceptor.called)
170       {
171          throw new RuntimeException JavaDoc("protected method caller interceptor (2) didn't work");
172       }
173
174       CallerInterceptor.called = false;
175       privateProtectedPojo.callPrivateMethod();
176       if (!CallerInterceptor.called)
177       {
178          throw new RuntimeException JavaDoc("protected method caller interceptor (2) didn't work");
179       }
180
181       CallerInterceptor.called = false;
182       privateProtectedPojo = new PrivateProtectedPOJO("s", 1);//calls private constructor
183
if (!CallerInterceptor.called)
184       {
185          throw new RuntimeException JavaDoc("private constructor caller interceptor from constructor didn't work");
186       }
187
188       CallerInterceptor.called = false;
189       PrivateProtectedPOJO.callPrivateConstructor();
190       if (!CallerInterceptor.called)
191       {
192          throw new RuntimeException JavaDoc("private constructor caller interceptor from method didn't work");
193       }
194
195       CallerInterceptor.called = false ;
196       final InterfacePojo interfacePojo = new InterfacePojo() ;
197       final SuperInterface superInterface = interfacePojo ;
198       superInterface.superInterfaceMethod() ;
199       if (!CallerInterceptor.called)
200       {
201          throw new RuntimeException JavaDoc("interface method via interface caller interceptor didn't work") ;
202       }
203
204       CallerInterceptor.called = false ;
205       final SubInterface subInterface = interfacePojo ;
206       subInterface.superInterfaceMethod() ;
207       if (!CallerInterceptor.called)
208       {
209          throw new RuntimeException JavaDoc("interface method via sub interface caller interceptor didn't work") ;
210       }
211    }
212
213    /**
214     * This method should not be a caller pointcut
215     */

216    public void nocallSomeMethod()
217    {
218       CallerInterceptor.called = false;
219       pojo = new POJO();
220       if (CallerInterceptor.called)
221       {
222          throw new RuntimeException JavaDoc("constructor caller interceptor didn't work, interceptor was invoked when it shouldn't have been");
223       }
224       pojo.someMethod();
225       if (CallerInterceptor.called)
226       {
227          throw new RuntimeException JavaDoc("caller interceptor didn't work, caller interceptor was invoked when it shouldn't have been");
228       }
229    }
230
231    public void callUnadvised()
232    {
233       CallerInterceptor.called = false;
234       nonpojo = new NonadvisedPOJO("helloworld");
235       if (!CallerInterceptor.called)
236       {
237          throw new RuntimeException JavaDoc("consturctor caller interceptor didn't work");
238       }
239       CallerInterceptor.called = false;
240       nonpojo.remoteTest();
241       if (!CallerInterceptor.called)
242       {
243          throw new RuntimeException JavaDoc("caller interceptor didn't work");
244       }
245       if (nonpojo instanceof Advised)
246       {
247          throw new RuntimeException JavaDoc("nonpojo is Advised when it shouldn't be");
248       }
249    }
250    
251    public void callUnadvisedWithPointcutException()
252    {
253       try
254       {
255          CallerInterceptor.called = false;
256          nonpojo = new NonadvisedPOJO("helloworld");
257          if (!CallerInterceptor.called)
258          {
259             throw new RuntimeException JavaDoc("caller interceptor didn't work");
260          }
261
262          CallerInterceptor.called = false;
263          nonpojo.withException("x", 1);
264          if (!CallerInterceptor.called)
265          {
266             throw new RuntimeException JavaDoc("caller interceptor didn't work");
267          }
268       }
269       catch (ClassCastException JavaDoc e)
270       {
271          // TODO Auto-generated catch block
272
throw new RuntimeException JavaDoc(e);
273       }
274       catch (SomeException e)
275       {
276          // TODO Auto-generated catch block
277
throw new RuntimeException JavaDoc(e);
278       }
279       
280       
281    }
282
283 }
284
285
Popular Tags