KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > interceptors2 > unit > InterceptorsTestCase


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.ejb3.test.interceptors2.unit;
23
24 import java.util.ArrayList JavaDoc;
25
26 import javax.management.MBeanServerConnection JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import junit.framework.Test;
31
32 import org.jboss.ejb3.mdb.ProducerManager;
33 import org.jboss.ejb3.mdb.ProducerObject;
34 import org.jboss.ejb3.test.interceptors2.AnnotatedClassInterceptor3;
35 import org.jboss.ejb3.test.interceptors2.AnnotatedMethodInterceptor4;
36 import org.jboss.ejb3.test.interceptors2.AnnotatedOnlySLSB;
37 import org.jboss.ejb3.test.interceptors2.AnnotatedOnlySLSBRemote;
38 import org.jboss.ejb3.test.interceptors2.AnnotatedClassInterceptor;
39 import org.jboss.ejb3.test.interceptors2.DefaultInterceptor;
40 import org.jboss.ejb3.test.interceptors2.DefaultOnlyProducer;
41 import org.jboss.ejb3.test.interceptors2.DefaultOnlySLSBRemote;
42 import org.jboss.ejb3.test.interceptors2.DefaultOnlyServiceRemote;
43 import org.jboss.ejb3.test.interceptors2.InheritingSFSB;
44 import org.jboss.ejb3.test.interceptors2.InheritingSFSBRemote;
45 import org.jboss.ejb3.test.interceptors2.Interception;
46 import org.jboss.ejb3.test.interceptors2.AnnotatedMethodInterceptor;
47 import org.jboss.ejb3.test.interceptors2.MethodOnlyInterceptedSLSBRemote;
48 import org.jboss.ejb3.test.interceptors2.MixedClassInterceptor;
49 import org.jboss.ejb3.test.interceptors2.MixedConfigSFSB;
50 import org.jboss.ejb3.test.interceptors2.MixedConfigSFSBRemote;
51 import org.jboss.ejb3.test.interceptors2.MixedMethodInterceptor;
52 import org.jboss.ejb3.test.interceptors2.MixedProducer;
53 import org.jboss.ejb3.test.interceptors2.MixedServiceRemote;
54 import org.jboss.ejb3.test.interceptors2.NoInterceptorsSLSBRemote;
55 import org.jboss.ejb3.test.interceptors2.OrderedSLSBRemote;
56 import org.jboss.ejb3.test.interceptors2.StatusRemote;
57 import org.jboss.ejb3.test.interceptors2.XMLClassInterceptor;
58 import org.jboss.ejb3.test.interceptors2.XMLClassInterceptor2;
59 import org.jboss.ejb3.test.interceptors2.XMLClassInterceptor3;
60 import org.jboss.ejb3.test.interceptors2.XMLMethodInterceptor;
61 import org.jboss.ejb3.test.interceptors2.XMLOnlySLSB;
62 import org.jboss.ejb3.test.interceptors2.XMLOnlySLSBRemote;
63 import org.jboss.test.JBossTestCase;
64
65 /**
66  *
67  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
68  * @version $Revision: 58110 $
69  */

70 public class InterceptorsTestCase
71 extends JBossTestCase
72 {
73    org.jboss.logging.Logger log = getLog();
74
75    public InterceptorsTestCase(String JavaDoc name)
76    {
77       super(name);
78    }
79
80    public void testAnnotatedOnlySLSB() throws Exception JavaDoc
81    {
82       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
83       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
84       AnnotatedOnlySLSBRemote slsb = (AnnotatedOnlySLSBRemote)ctx.lookup("AnnotatedOnlySLSB/remote");
85       
86       status.clear();
87       slsb.methodWithClassLevel();
88       ArrayList JavaDoc<Interception> interceptions = status.getInterceptions();
89       ArrayList JavaDoc<Interception> postConstructs = status.getPostConstructs();
90       compare(new Interception[] {
91             new Interception(AnnotatedClassInterceptor.class, "intercept3"),
92             new Interception(AnnotatedClassInterceptor.class, "intercept"),
93             new Interception(AnnotatedOnlySLSB.class, "intercept")}, interceptions);
94       checkLifecycle(status, true, false, false, false);
95       compareLifecycle(new Interception[] {
96             new Interception(AnnotatedClassInterceptor.class, "postConstruct3"),
97             new Interception(AnnotatedClassInterceptor.class, "postConstruct"),
98             new Interception(AnnotatedMethodInterceptor.class, "postConstruct"),
99             new Interception(AnnotatedOnlySLSB.class, "postConstruct")}, postConstructs);
100       
101
102       status.clear();
103       slsb.methodExcludingClassInterceptors();
104       interceptions = status.getInterceptions();
105       compare(new Interception[] {new Interception(AnnotatedOnlySLSB.class, "intercept")}, interceptions);
106       checkLifecycle(status, false, false, false, false);
107       
108       status.clear();
109       slsb.methodWithOwnInterceptors();
110       interceptions = status.getInterceptions();
111       compare(new Interception[] {
112             new Interception(AnnotatedClassInterceptor.class, "intercept3"),
113             new Interception(AnnotatedClassInterceptor.class, "intercept"),
114             new Interception(AnnotatedMethodInterceptor.class, "intercept4"),
115             new Interception(AnnotatedMethodInterceptor.class, "intercept2"),
116             new Interception(AnnotatedMethodInterceptor.class, "intercept"),
117             new Interception(AnnotatedOnlySLSB.class, "intercept")}, interceptions);
118       checkLifecycle(status, false, false, false, false);
119       
120       status.clear();
121       slsb.methodWithOwnInterceptorsExcludeClass();
122       interceptions = status.getInterceptions();
123       compare(new Interception[] {
124             new Interception(AnnotatedMethodInterceptor.class, "intercept4"),
125             new Interception(AnnotatedMethodInterceptor.class, "intercept2"),
126             new Interception(AnnotatedMethodInterceptor.class, "intercept"),
127             new Interception(AnnotatedOnlySLSB.class, "intercept")}, interceptions);
128       checkLifecycle(status, false, false, false, false);
129       
130    }
131
132    public void testXmlOnlySLSB() throws Exception JavaDoc
133    {
134       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
135       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
136       XMLOnlySLSBRemote slsb = (XMLOnlySLSBRemote)ctx.lookup("org.jboss.ejb3.test.interceptors2.XMLOnlySLSBRemote");
137       
138       status.clear();
139       slsb.methodWithClassLevel();
140       ArrayList JavaDoc<Interception> interceptions = status.getInterceptions();
141       ArrayList JavaDoc<Interception> postConstructs = status.getPostConstructs();
142       compare(new Interception[] {
143             new Interception(DefaultInterceptor.class, "intercept"),
144             new Interception(XMLClassInterceptor2.class, "intercept3"),
145             new Interception(XMLClassInterceptor2.class, "intercept2"),
146             new Interception(XMLOnlySLSB.class, "intercept")}, interceptions);
147       checkLifecycle(status, true, false, false, false);
148       compareLifecycle(new Interception[] {
149             new Interception(DefaultInterceptor.class, "postConstruct"),
150             new Interception(XMLClassInterceptor2.class, "postConstruct3"),
151             new Interception(XMLClassInterceptor2.class, "postConstruct2"),
152             new Interception(XMLMethodInterceptor.class, "postConstruct"),
153             new Interception(XMLOnlySLSB.class, "postConstruct")}, postConstructs);
154       
155       status.clear();
156       long l = 10;
157       slsb.overloadedMethod(l);
158       interceptions = status.getInterceptions();
159       compare(new Interception[] {
160             new Interception(XMLClassInterceptor2.class, "intercept3"),
161             new Interception(XMLClassInterceptor2.class, "intercept2"),
162             new Interception(XMLOnlySLSB.class, "intercept")}, interceptions);
163       checkLifecycle(status, false, false, false, false);
164
165       status.clear();
166       slsb.overloadedMethod(l, new String JavaDoc[][] {{"Hello"}, {"Whatever"}});
167       interceptions = status.getInterceptions();
168       compare(new Interception[] {
169             new Interception(DefaultInterceptor.class, "intercept"),
170             new Interception(XMLClassInterceptor2.class, "intercept3"),
171             new Interception(XMLClassInterceptor2.class, "intercept2"),
172             new Interception(XMLMethodInterceptor.class, "intercept"),
173             new Interception(XMLOnlySLSB.class, "intercept")}, interceptions);
174       checkLifecycle(status, false, false, false, false);
175       
176       status.clear();
177       int i = 5;
178       slsb.overloadedMethod(i);
179       interceptions = status.getInterceptions();
180       compare(new Interception[] {
181             new Interception(DefaultInterceptor.class, "intercept"),
182             new Interception(XMLMethodInterceptor.class, "intercept"),
183             new Interception(XMLOnlySLSB.class, "intercept")}, interceptions);
184       checkLifecycle(status, false, false, false, false);
185       
186       status.clear();
187       slsb.overloadedMethod();
188       interceptions = status.getInterceptions();
189       compare(new Interception[] {
190             new Interception(XMLMethodInterceptor.class, "intercept"),
191             new Interception(XMLOnlySLSB.class, "intercept")}, interceptions);
192    }
193    
194    public void testDefaultOnlySLSB() throws Exception JavaDoc
195    {
196       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
197       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
198       DefaultOnlySLSBRemote slsb = (DefaultOnlySLSBRemote)ctx.lookup("DefaultOnlySLSB/remote");
199       
200       status.clear();
201       slsb.test();
202       ArrayList JavaDoc<Interception> interceptions = status.getInterceptions();
203       compare(new Interception[] {new Interception(DefaultInterceptor.class, "intercept")}, interceptions);
204       checkLifecycle(status, true, false, false, false);
205       compareLifecycle(new Interception[] {new Interception(DefaultInterceptor.class, "postConstruct")}, status.getPostConstructs());
206    }
207    
208    public void testNoInterceptorsSLSB() throws Exception JavaDoc
209    {
210       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
211       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
212       NoInterceptorsSLSBRemote slsb = (NoInterceptorsSLSBRemote)ctx.lookup("NoInterceptorsSLSB/remote");
213       
214       status.clear();
215       slsb.test();
216       ArrayList JavaDoc<Interception> interceptions = status.getInterceptions();
217       compare(new Interception[0], interceptions);
218       checkLifecycle(status, false, false, false, false);
219    }
220
221    public void testMixedConfigSFSB() throws Exception JavaDoc
222    {
223       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
224       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
225       
226       MixedConfigSFSBRemote sfsb1 = (MixedConfigSFSBRemote)ctx.lookup("MixedConfigSFSB/remote");
227       MixedConfigSFSBRemote sfsb2 = (MixedConfigSFSBRemote)ctx.lookup("MixedConfigSFSB/remote");
228       
229       final Interception[] expectedTestInterceptions =
230          new Interception[] {
231             new Interception(DefaultInterceptor.class, "intercept"),
232             new Interception(MixedClassInterceptor.class, "intercept"),
233             new Interception(XMLClassInterceptor.class, "intercept3"),
234             new Interception(XMLClassInterceptor.class, "intercept"),
235             new Interception(AnnotatedClassInterceptor.class, "intercept3"),
236             new Interception(AnnotatedClassInterceptor.class, "intercept"),
237             new Interception(MixedConfigSFSB.class, "intercept")};
238       
239       final Interception[] expectedTestWithMethodLevelInterceptions =
240          new Interception[] {
241             new Interception(DefaultInterceptor.class, "intercept"),
242             new Interception(MixedClassInterceptor.class, "intercept"),
243             new Interception(XMLClassInterceptor.class, "intercept3"),
244             new Interception(XMLClassInterceptor.class, "intercept"),
245             new Interception(AnnotatedClassInterceptor.class, "intercept3"),
246             new Interception(AnnotatedClassInterceptor.class, "intercept"),
247             new Interception(MixedMethodInterceptor.class, "intercept"),
248             new Interception(XMLMethodInterceptor.class, "intercept"),
249             new Interception(AnnotatedMethodInterceptor.class, "intercept4"),
250             new Interception(AnnotatedMethodInterceptor.class, "intercept2"),
251             new Interception(AnnotatedMethodInterceptor.class, "intercept"),
252             new Interception(MixedConfigSFSB.class, "intercept")};
253       
254       final Interception[] expectedPostConstructInterceptors =
255          new Interception[] {
256             new Interception(DefaultInterceptor.class, "postConstruct"),
257             new Interception(XMLClassInterceptor.class, "postConstruct3"),
258             new Interception(XMLClassInterceptor.class, "postConstruct"),
259             new Interception(AnnotatedClassInterceptor.class, "postConstruct3"),
260             new Interception(AnnotatedClassInterceptor.class, "postConstruct"),
261             new Interception(XMLMethodInterceptor.class, "postConstruct"),
262             new Interception(AnnotatedMethodInterceptor.class, "postConstruct")};
263       
264       final Interception[] expectedPostActivateInterceptors =
265          new Interception[] {
266             new Interception(DefaultInterceptor.class, "postActivate"),
267             new Interception(XMLClassInterceptor.class, "postActivate3"),
268             new Interception(XMLClassInterceptor.class, "postActivate"),
269             new Interception(AnnotatedClassInterceptor.class, "postActivate3"),
270             new Interception(AnnotatedClassInterceptor.class, "postActivate"),
271             new Interception(XMLMethodInterceptor.class, "postActivate"),
272             new Interception(AnnotatedMethodInterceptor.class, "postActivate")};
273       
274       final Interception[] expectedPrePassivateInterceptors =
275          new Interception[] {
276             new Interception(DefaultInterceptor.class, "prePassivate"),
277             new Interception(XMLClassInterceptor.class, "prePassivate3"),
278             new Interception(XMLClassInterceptor.class, "prePassivate"),
279             new Interception(AnnotatedClassInterceptor.class, "prePassivate3"),
280             new Interception(AnnotatedClassInterceptor.class, "prePassivate"),
281             new Interception(XMLMethodInterceptor.class, "prePassivate"),
282             new Interception(AnnotatedMethodInterceptor.class, "prePassivate")};
283       
284       final Interception[] expectedPreDestroyInterceptors =
285          new Interception[] {
286             new Interception(DefaultInterceptor.class, "preDestroy"),
287             new Interception(XMLClassInterceptor.class, "preDestroy3"),
288             new Interception(XMLClassInterceptor.class, "preDestroy"),
289             new Interception(AnnotatedClassInterceptor.class, "preDestroy3"),
290             new Interception(AnnotatedClassInterceptor.class, "preDestroy"),
291             new Interception(XMLMethodInterceptor.class, "preDestroy"),
292             new Interception(AnnotatedMethodInterceptor.class, "preDestroy")};
293       
294       status.clear();
295       sfsb1.test();
296       ArrayList JavaDoc<Interception> bean1test = status.getInterceptions();
297       ArrayList JavaDoc<Interception> bean1pc = status.getPostConstructs();
298       compare(expectedTestInterceptions, bean1test);
299       checkLifecycle(status, true, false, false, false);
300       compareLifecycle(expectedPostConstructInterceptors, bean1pc);
301       
302       status.clear();
303       sfsb1.testWithMethodLevel();
304       ArrayList JavaDoc<Interception> bean1testml = status.getInterceptions();
305       compare(expectedTestWithMethodLevelInterceptions, bean1testml);
306       checkLifecycle(status, false, false, false, false);
307       
308       //Cache size is 1, so sfsb1 should get passivated and sfsb2 constructed
309
status.clear();
310       sfsb2.test();
311       ArrayList JavaDoc<Interception> bean2test = status.getInterceptions();
312       ArrayList JavaDoc<Interception> bean2pc = status.getPostConstructs();
313       ArrayList JavaDoc<Interception> bean2pp = status.getPrePassivates();
314       compare(expectedTestInterceptions, bean2test);
315       checkLifecycle(status, true, false, true, false);
316       compareLifecycle(expectedPostConstructInterceptors, bean2pc);
317       compareLifecycle(expectedPrePassivateInterceptors, bean2pp);
318       
319       status.clear();
320       sfsb2.testWithMethodLevel();
321       ArrayList JavaDoc<Interception> bean2testml = status.getInterceptions();
322       compare(expectedTestWithMethodLevelInterceptions, bean2testml);
323       checkLifecycle(status, false, false, false, false);
324       
325       status.clear();
326       sfsb2.testWithMethodLevelB();
327       ArrayList JavaDoc<Interception> bean2testmlb = status.getInterceptions();
328       compare(expectedTestWithMethodLevelInterceptions, bean2testmlb);
329       checkLifecycle(status, false, false, false, false);
330       
331       //Cache size is 1, so sfsb2 should get passivated and sfsb1 activated
332
status.clear();
333       sfsb1.test();
334       ArrayList JavaDoc<Interception> bean1test_2 = status.getInterceptions();
335       ArrayList JavaDoc<Interception> bean1pp_2 = status.getPrePassivates();
336       ArrayList JavaDoc<Interception> bean1pa_2 = status.getPostActivates();
337       compare(expectedTestInterceptions, bean1test_2);
338       checkLifecycle(status, false, true, true, false);
339       compareLifecycle(expectedPrePassivateInterceptors, bean1pp_2);
340       compareLifecycle(expectedPostActivateInterceptors, bean1pa_2);
341       
342       status.clear();
343       sfsb1.testWithMethodLevel();
344       ArrayList JavaDoc<Interception> bean1testml_2 = status.getInterceptions();
345       compare(expectedTestWithMethodLevelInterceptions, bean1testml_2);
346       checkLifecycle(status, false, false, false, false);
347       
348       status.clear();
349       sfsb1.testWithMethodLevelB();
350       ArrayList JavaDoc<Interception> bean1testmlb_2 = status.getInterceptions();
351       compare(expectedTestWithMethodLevelInterceptions, bean1testmlb_2);
352       checkLifecycle(status, false, false, false, false);
353       
354       //interceptor instances used for bean 1 should all be the same, and different from those for bean 2
355
//Need to check that we can remove
356
status.clear();
357       sfsb1.kill();
358       ArrayList JavaDoc<Interception> preDestroys = status.getPreDestroys();
359       checkLifecycle(status, false, false, false, true);
360       compareLifecycle(expectedPreDestroyInterceptors, preDestroys);
361
362       checkInstances(bean1test, bean1test_2, true);
363       checkInstances(bean1testml, bean1testml_2, true);
364       checkInstances(bean1testml, bean1testmlb_2, true);
365       checkInstances(bean2testml, bean2testmlb, true);
366       checkInstances(bean1test, bean2test, false);
367       checkInstances(bean1testml, bean2testml, false);
368       
369       
370       
371    }
372    
373    public void testBeanHierarchy() throws Exception JavaDoc
374    {
375       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
376       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
377       InheritingSFSBRemote bean1 = (InheritingSFSBRemote)ctx.lookup("InheritingSFSB/remote");
378       InheritingSFSBRemote bean2 = (InheritingSFSBRemote)ctx.lookup("InheritingSFSB/remote");
379       
380       status.clear();
381       bean1.method();
382       ArrayList JavaDoc<Interception> interceptions = status.getInterceptions();
383       ArrayList JavaDoc<Interception> postConstructs = status.getPostConstructs();
384       checkLifecycle(status, true, false, false, false);
385       compare(
386             new Interception[] {
387                   new Interception(DefaultInterceptor.class, "intercept"),
388                   new Interception(InheritingSFSB.class, "intercept3"),
389                   new Interception(InheritingSFSB.class, "intercept")}, interceptions);
390       compareLifecycle(
391             new Interception[] {
392                   new Interception(DefaultInterceptor.class, "postConstruct"),
393                   new Interception(InheritingSFSB.class, "postConstruct3"),
394                   new Interception(InheritingSFSB.class, "postConstruct")}, postConstructs);
395
396       status.clear();
397       bean2.methodNoDefault();
398       interceptions = status.getInterceptions();
399       postConstructs = status.getPostConstructs();
400       ArrayList JavaDoc<Interception> prePassivates = status.getPrePassivates();
401       checkLifecycle(status, true, false, true, false);
402       compare(
403             new Interception[] {
404                   new Interception(InheritingSFSB.class, "intercept3"),
405                   new Interception(InheritingSFSB.class, "intercept")}, interceptions);
406       compareLifecycle(
407             new Interception[] {
408                   new Interception(DefaultInterceptor.class, "postConstruct"),
409                   new Interception(InheritingSFSB.class, "postConstruct3"),
410                   new Interception(InheritingSFSB.class, "postConstruct")}, postConstructs);
411       compareLifecycle(
412             new Interception[] {
413                   new Interception(DefaultInterceptor.class, "prePassivate"),
414                   new Interception(InheritingSFSB.class, "prePassivate3"),
415                   new Interception(InheritingSFSB.class, "prePassivate")}, prePassivates);
416       
417       status.clear();
418       bean1.methodNoDefault();
419       interceptions = status.getInterceptions();
420       prePassivates = status.getPrePassivates();
421       ArrayList JavaDoc<Interception> postActivates = status.getPostActivates();
422       checkLifecycle(status, false, true, true, false);
423       compare(
424             new Interception[] {
425                   new Interception(InheritingSFSB.class, "intercept3"),
426                   new Interception(InheritingSFSB.class, "intercept")}, interceptions);
427       compareLifecycle(
428             new Interception[] {
429                   new Interception(DefaultInterceptor.class, "postActivate"),
430                   new Interception(InheritingSFSB.class, "postActivate3"),
431                   new Interception(InheritingSFSB.class, "postActivate")}, postActivates);
432       compareLifecycle(
433             new Interception[] {
434                   new Interception(DefaultInterceptor.class, "prePassivate"),
435                   new Interception(InheritingSFSB.class, "prePassivate3"),
436                   new Interception(InheritingSFSB.class, "prePassivate")}, prePassivates);
437       
438       
439       //Need to check that we can remove
440
status.clear();
441       bean1.kill();
442       ArrayList JavaDoc<Interception> preDestroys = status.getPreDestroys();
443       checkLifecycle(status, false, false, false, true);
444       compareLifecycle(
445             new Interception[] {
446                   new Interception(DefaultInterceptor.class, "preDestroy"),
447                   new Interception(InheritingSFSB.class, "preDestroy3"),
448                   new Interception(InheritingSFSB.class, "preDestroy")}, preDestroys);
449    }
450    
451    public void testOverrideOrdering() throws Exception JavaDoc
452    {
453       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
454       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
455       OrderedSLSBRemote slsb = (OrderedSLSBRemote)ctx.lookup("OrderedSLSB/remote");
456       
457       status.clear();
458       slsb.methodWithClassLevel();
459       checkLifecycle(status, true, false, false, false);
460       ArrayList JavaDoc<Interception> actual = status.getInterceptions();
461       compare(
462             new Interception[] {
463                   new Interception( AnnotatedClassInterceptor3.class, "intercept3"),
464                   new Interception( XMLClassInterceptor3.class, "intercept3"),
465                   new Interception( DefaultInterceptor.class, "intercept")}, actual);
466       actual = status.getPostConstructs();
467       compareLifecycle(new Interception[] {
468             new Interception( AnnotatedClassInterceptor3.class, "postConstruct3"),
469             new Interception( XMLClassInterceptor3.class, "postConstruct3"),
470             new Interception( DefaultInterceptor.class, "postConstruct"),
471             new Interception( AnnotatedMethodInterceptor.class, "postConstruct"),
472             new Interception( XMLMethodInterceptor.class, "postConstruct")}, actual);
473
474       status.clear();
475       slsb.methodWithOwn("l", 5);
476       actual = status.getInterceptions();
477       compare(
478             new Interception[] {
479                   new Interception( XMLMethodInterceptor.class,"intercept"),
480                   new Interception( AnnotatedMethodInterceptor.class,"intercept4"),
481                   new Interception( AnnotatedMethodInterceptor.class,"intercept2"),
482                   new Interception( AnnotatedMethodInterceptor.class,"intercept")}, actual);
483       
484       status.clear();
485       slsb.overLoadedMethod("x");
486       actual = status.getInterceptions();
487       compare(
488             new Interception[] {
489                   new Interception( AnnotatedClassInterceptor3.class, "intercept3"),
490                   new Interception( DefaultInterceptor.class, "intercept"),
491                   new Interception( XMLClassInterceptor3.class, "intercept3"),
492                   new Interception( AnnotatedMethodInterceptor.class,"intercept4"),
493                   new Interception( AnnotatedMethodInterceptor.class,"intercept2"),
494                   new Interception( AnnotatedMethodInterceptor.class,"intercept"),
495                   new Interception( MixedMethodInterceptor.class,"intercept"),
496                   new Interception( XMLMethodInterceptor.class,"intercept")}, actual);
497       
498       status.clear();
499       slsb.overLoadedMethod(5);
500       actual = status.getInterceptions();
501       compare(
502             new Interception[] {
503                   new Interception( XMLClassInterceptor3.class, "intercept3"),
504                   new Interception( AnnotatedMethodInterceptor.class,"intercept4"),
505                   new Interception( AnnotatedMethodInterceptor.class,"intercept2"),
506                   new Interception( AnnotatedMethodInterceptor.class,"intercept"),
507                   new Interception( AnnotatedClassInterceptor3.class, "intercept3"),
508                   new Interception( DefaultInterceptor.class, "intercept"),
509                   new Interception( MixedMethodInterceptor.class,"intercept"),
510                   new Interception( XMLMethodInterceptor.class,"intercept"),
511             }, actual);
512       
513       status.clear();
514       slsb.overLoadedMethod();
515       actual = status.getInterceptions();
516       compare(
517             new Interception[] {
518                   new Interception( MixedMethodInterceptor.class,"intercept"),
519                   new Interception( DefaultInterceptor.class, "intercept")
520             }, actual);
521       
522       status.clear();
523       slsb.methodNotSpecifyingAll();
524       actual = status.getInterceptions();
525       compare(
526             new Interception[] {
527                   new Interception( XMLClassInterceptor3.class, "intercept3"),
528                   new Interception( AnnotatedMethodInterceptor.class,"intercept4"),
529                   new Interception( AnnotatedMethodInterceptor.class,"intercept2"),
530                   new Interception( AnnotatedMethodInterceptor.class,"intercept"),
531                   new Interception( AnnotatedClassInterceptor3.class, "intercept3"),
532                   new Interception( DefaultInterceptor.class, "intercept"),
533                   new Interception( XMLMethodInterceptor.class,"intercept"),
534                   new Interception( MixedMethodInterceptor.class,"intercept")}, actual);
535       
536    }
537    
538    public void testService() throws Exception JavaDoc
539    {
540       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
541       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
542       MixedServiceRemote srv = (MixedServiceRemote)ctx.lookup("MixedService/remote");
543       
544       status.clear();
545       srv.defaultMethod();
546       ArrayList JavaDoc<Interception> actual = status.getInterceptions();
547       compare(
548             new Interception[] {
549                   new Interception(DefaultInterceptor.class, "intercept"),
550                   new Interception(MixedClassInterceptor.class, "intercept"),
551                   new Interception(MixedMethodInterceptor.class, "intercept")}, actual);
552
553       
554       status.clear();
555       srv.remoteMethod();
556       actual = status.getInterceptions();
557       compare(
558             new Interception[] {
559                   new Interception(DefaultInterceptor.class, "intercept"),
560                   new Interception(MixedMethodInterceptor.class, "intercept")}, actual);
561       
562       status.clear();
563       MBeanServerConnection JavaDoc server = getServer();
564       ObjectName JavaDoc testerName = new ObjectName JavaDoc("test:service=mixed");
565       Object JavaDoc[] params = {};
566       String JavaDoc[] sig = {};
567       server.invoke(testerName, "managementMethod", params, sig);
568       actual = status.getInterceptions();
569       compare(
570             new Interception[] {
571                   new Interception(DefaultInterceptor.class, "intercept"),
572                   new Interception(MixedClassInterceptor.class, "intercept"),
573                   new Interception(MixedMethodInterceptor.class, "intercept")}, actual);
574    }
575    
576    public void testDefaultOnlyService() throws Exception JavaDoc
577    {
578       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
579       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
580       DefaultOnlyServiceRemote srv = (DefaultOnlyServiceRemote)ctx.lookup("DefaultOnlyService/remote");
581       
582       status.clear();
583       srv.method();
584       ArrayList JavaDoc<Interception> actual = status.getInterceptions();
585       compare(
586             new Interception[] {
587                   new Interception(DefaultInterceptor.class, "intercept")}, actual);
588       
589       status.clear();
590       MBeanServerConnection JavaDoc server = getServer();
591       ObjectName JavaDoc testerName = new ObjectName JavaDoc("test:service=DefaultOnly");
592       Object JavaDoc[] params = {};
593       String JavaDoc[] sig = {};
594       server.invoke(testerName, "method", params, sig);
595       actual = status.getInterceptions();
596       compare(
597             new Interception[] {
598                   new Interception(DefaultInterceptor.class, "intercept")}, actual);
599    }
600    
601    public void testMixedConsumer() throws Exception JavaDoc
602    {
603       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
604       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
605       MixedProducer prod = (MixedProducer)ctx.lookup("org.jboss.ejb3.test.interceptors2.MixedProducer");
606       ProducerManager manager = ((ProducerObject) prod).getProducerManager();
607       manager.connect();
608
609       status.clear();
610       prod.method();
611       Thread.sleep(1000); //Give receiving end enough time to pick up interceptions
612
ArrayList JavaDoc<Interception> actual = status.getInterceptions();
613       compare(
614             new Interception[] {
615                   new Interception(DefaultInterceptor.class, "intercept"),
616                   new Interception(MixedClassInterceptor.class, "intercept"),
617                   new Interception(XMLMethodInterceptor.class, "intercept")}, actual);
618       
619       status.clear();
620       prod.methodWithOwnOnly();
621       Thread.sleep(1000); //Give receiving end enough time to pick up interceptions
622
actual = status.getInterceptions();
623       compare(
624             new Interception[] {
625                   new Interception(MixedMethodInterceptor.class, "intercept")}, actual);
626    }
627    
628    public void testDefaultOnlyConsumer() throws Exception JavaDoc
629    {
630       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
631       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
632       DefaultOnlyProducer prod = (DefaultOnlyProducer)ctx.lookup("org.jboss.ejb3.test.interceptors2.DefaultOnlyProducer");
633       ProducerManager manager = ((ProducerObject) prod).getProducerManager();
634       manager.connect();
635
636       status.clear();
637       prod.method();
638       Thread.sleep(1000); //Give receiving end enough time to pick up interceptions
639
ArrayList JavaDoc<Interception> actual = status.getInterceptions();
640       compare(
641             new Interception[] {
642                   new Interception(DefaultInterceptor.class, "intercept")}, actual);
643    }
644    
645    public void testMethodOnlySLSB() throws Exception JavaDoc
646    {
647       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
648       StatusRemote status = (StatusRemote)ctx.lookup("StatusBean/remote");
649       MethodOnlyInterceptedSLSBRemote remote = (MethodOnlyInterceptedSLSBRemote)ctx.lookup("MethodOnlyInterceptedSLSB/remote");
650
651       status.clear();
652       remote.intercepted();
653       ArrayList JavaDoc<Interception> actual = status.getInterceptions();
654       compare(
655             new Interception[] {
656                   new Interception(DefaultInterceptor.class, "intercept"),
657                   new Interception(AnnotatedMethodInterceptor4.class, "intercept4")}, actual);
658       
659       status.clear();
660       remote.notintercepted();
661       actual = status.getInterceptions();
662       compare(
663             new Interception[] {
664                   new Interception(DefaultInterceptor.class, "intercept")}, actual);
665    }
666    
667    private void compare(Interception[] expected, ArrayList JavaDoc<Interception> interceptions)
668    {
669       assertEquals("Bad interceptions: " + interceptions, expected.length, interceptions.size());
670       
671       for (int i = 0 ; i < expected.length ; i++)
672       {
673          assertEquals(
674                "index " + i,
675                expected[i].getClassname() + "." + expected[i].getMethod(),
676                interceptions.get(i).getClassname() + "." + interceptions.get(i).getMethod());
677       }
678    }
679    
680    
681    private void checkLifecycle(StatusRemote status,
682          boolean expectPostConstruct,
683          boolean expectPostActivate,
684          boolean expectPrePassivate,
685          boolean expectPreDestroy)
686    {
687       if (expectPostConstruct)
688       {
689          assertNotSame(0, status.getPostConstructs().size());
690       }
691       else
692       {
693          assertEquals(0, status.getPostConstructs().size());
694       }
695
696       if (expectPostActivate)
697       {
698          assertNotSame(0, status.getPostActivates().size());
699       }
700       else
701       {
702          assertEquals(0, status.getPostActivates().size());
703       }
704
705       if (expectPrePassivate)
706       {
707          assertNotSame(0, status.getPrePassivates().size());
708       }
709       else
710       {
711          assertEquals(0, status.getPrePassivates().size());
712       }
713
714       if (expectPreDestroy)
715       {
716          assertNotSame(0, status.getPreDestroys().size());
717       }
718       else
719       {
720          assertEquals(0, status.getPreDestroys().size());
721       }
722 }
723    
724    private void compareLifecycle(Interception[] expected, ArrayList JavaDoc<Interception> interceptions)
725    {
726       assertEquals("\nExpected:\n" + toString(expected) + "\nHad interceptions\n" + interceptions, expected.length, interceptions.size());
727       
728       for (int i = 0 ; i < expected.length ; i++)
729       {
730          assertEquals(
731                "index " + i,
732                expected[i].getClassname() + "." + expected[i].getMethod(),
733                interceptions.get(i).getClassname() + "." + interceptions.get(i).getMethod());
734       }
735    }
736    
737    private void checkInstances(ArrayList JavaDoc<Interception> interceptionsA, ArrayList JavaDoc<Interception> interceptionsB, boolean same)
738    {
739       for (int i = 0 ; i < interceptionsA.size() ; i++)
740       {
741          if (interceptionsA.get(i).getInstance() >= 0 || interceptionsB.get(i).getInstance() >= 0)
742          if (same)
743          {
744             assertEquals("Instances should be the same (" + i + ")", interceptionsA.get(i).getInstance(), interceptionsB.get(i).getInstance());
745          }
746          else
747          {
748             assertNotSame("Instances should not be the same (" + i + ")", interceptionsA.get(i).getInstance(), interceptionsB.get(i).getInstance());
749          }
750       }
751    }
752    
753    private String JavaDoc toString(Object JavaDoc[] array)
754    {
755       StringBuffer JavaDoc buf = new StringBuffer JavaDoc("[");
756       
757       for (int i = 0 ; i < array.length ; i++)
758       {
759          if (i > 0) buf.append(", ");
760          
761          buf.append(array[i]);
762       }
763       
764       buf.append("]");
765       return buf.toString();
766    }
767    
768    public static Test suite() throws Exception JavaDoc
769    {
770       return getDeploySetup(InterceptorsTestCase.class, "interceptors2test-service.xml, interceptors2-test.jar");
771    }
772
773 }
Popular Tags