KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > scoped > ScopedTester


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.scoped;
23
24 /**
25  *
26  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
27  * @version $Revision$
28  */

29 public class ScopedTester implements ScopedTesterMBean
30 {
31    int expectedInterceptorValue;
32    int expectedAspectValue;
33    int metadataSuffix;
34    
35    String JavaDoc ctorPlainMetadata;
36    String JavaDoc methodPlainMetadata;
37    String JavaDoc customMetadata;
38    
39    public void setExpectedInterceptorValue(int i)
40    {
41       expectedInterceptorValue = i;
42    }
43
44    public void setExpectedAspectValue(int i)
45    {
46       expectedAspectValue = i;
47    }
48    
49    public int getExpectedInterceptorValue()
50    {
51       return expectedInterceptorValue;
52    }
53
54    public int getExpectedAspectValue()
55    {
56       return expectedAspectValue;
57    }
58
59    public void setMetadataSuffix(int i)
60    {
61       metadataSuffix = i;
62       ctorPlainMetadata = "ctor" + metadataSuffix;
63       methodPlainMetadata = "method" + metadataSuffix;
64       customMetadata = "custom" + metadataSuffix;
65    }
66    
67    public void testExpectedValues()
68    {
69       if (ScopedInterceptor.value != expectedInterceptorValue)
70       {
71          throw new RuntimeException JavaDoc("Expected Interceptor value " + expectedInterceptorValue + ", was " + ScopedInterceptor.value);
72       }
73       if (ScopedAspect.value != expectedAspectValue)
74       {
75          throw new RuntimeException JavaDoc("Expected Aspect value " + expectedAspectValue + ", was " + ScopedAspect.value);
76       }
77       
78    }
79    
80    public void testScoped() throws Exception JavaDoc
81    {
82       System.out.println("--------------------------- TESTING SCOPED ------------------");
83       System.out.println("MY CLASSLOADER " + getClass().getClassLoader());
84       System.out.println("SCOPED INTERCEPTOR CLASSLOADER " + ScopedInterceptor.class.getClassLoader());
85
86
87       System.out.println("------- CTOR");
88       ScopedAspect.intercepted = 0;
89       ScopedFactoryAspect.intercepted = 0;
90       ScopedFactoryAspect.metadata = null;
91       ScopedFactoryAspect.customMetadata = null;
92       
93       POJO pojo = new POJO();
94       if (ScopedAspect.intercepted != 1)
95       {
96          throw new RuntimeException JavaDoc("Expected ScopedAspect 1 for POJO constructor, was " + ScopedAspect.intercepted);
97       }
98       if (ScopedFactoryAspect.intercepted != 1)
99       {
100          throw new RuntimeException JavaDoc("Expected ScopedFactoryAspect 1 for POJO constructor, was " + ScopedFactoryAspect.intercepted);
101       }
102       if (!ctorPlainMetadata.equals(ScopedFactoryAspect.metadata))
103       {
104          throw new RuntimeException JavaDoc("Expected ctor metadata " + ctorPlainMetadata + ", was " + ScopedFactoryAspect.metadata);
105       }
106       if (!customMetadata.equals(ScopedFactoryAspect.customMetadata))
107       {
108          throw new RuntimeException JavaDoc("Expected ctor customm metadata " + customMetadata + ", was " + ScopedFactoryAspect.customMetadata);
109       }
110       
111       System.out.println("------- METHOD");
112       ScopedInterceptor.intercepted = 0;
113       ScopedAspect.intercepted = 0;
114       ScopedFactoryAspect.intercepted = 0;
115       ScopedFactoryAspect.metadata = null;
116       ScopedFactoryAspect.customMetadata = null;
117
118       pojo.method();
119       if (ScopedInterceptor.intercepted != 1)
120       {
121          throw new RuntimeException JavaDoc("Expected ScopedInterceptor 1 for POJO method, was " + ScopedInterceptor.intercepted);
122       }
123       if (ScopedAspect.intercepted != 1)
124       {
125          throw new RuntimeException JavaDoc("Expected ScopedAspect 1 for POJO method, was " + ScopedAspect.intercepted);
126       }
127       if (ScopedFactoryAspect.intercepted != 1)
128       {
129          throw new RuntimeException JavaDoc("Expected ScopedFactoryAspect 1 for POJO method, was " + ScopedFactoryAspect.intercepted);
130       }
131       if (!methodPlainMetadata.equals(ScopedFactoryAspect.metadata))
132       {
133          throw new RuntimeException JavaDoc("Expected method metadata '" + methodPlainMetadata + ", was " + ScopedFactoryAspect.metadata);
134       }
135       if (!customMetadata.equals(ScopedFactoryAspect.customMetadata))
136       {
137          throw new RuntimeException JavaDoc("Expected method customm metadata " + customMetadata + ", was " + ScopedFactoryAspect.customMetadata);
138       }
139    }
140    
141    public void testAnnotatedScopedAnnotationsDeployed()
142    {
143       AnnotatedInterceptor.invoked = false;
144       POJO pojo = new POJO();
145       if (!AnnotatedInterceptor.invoked) throw new RuntimeException JavaDoc("AnnotatedInterceptor should have been invoked");
146    }
147    
148    public void testAnnotatedScopedAnnotationsNotDeployed()
149    {
150       try
151       {
152          AnnotatedInterceptor.invoked = false;
153          throw new RuntimeException JavaDoc("AnnotatedInterceptor should not be available in this deployment");
154       }
155       catch (NoClassDefFoundError JavaDoc expected)
156       {
157       }
158    }
159    
160    public void testIntroduction1()
161    {
162       IntroducedPOJO pojo = new IntroducedPOJO();
163       Scope1Interface iface = (Scope1Interface)pojo;
164       
165       ScopedInterceptor.intercepted = 0;
166       iface.testMethod();
167       if (ScopedInterceptor.intercepted != 1)
168       {
169          throw new RuntimeException JavaDoc("Expected ScopedInterceptor 1 for introduction " + ScopedInterceptor.intercepted);
170       }
171       
172       try
173       {
174          Scope2Interface iface2 = (Scope2Interface)pojo;
175          throw new RuntimeException JavaDoc("Should not be implementing Scope2Interface");
176       }
177       catch(ClassCastException JavaDoc expected)
178       {
179       }
180    }
181    
182    public void testIntroduction2()
183    {
184       IntroducedPOJO pojo = new IntroducedPOJO();
185       Scope2Interface iface = (Scope2Interface)pojo;
186       
187       ScopedInterceptor.intercepted = 0;
188       iface.testMethod();
189       if (ScopedInterceptor.intercepted != 1)
190       {
191          throw new RuntimeException JavaDoc("Expected ScopedInterceptor 1 for introduction " + ScopedInterceptor.intercepted);
192       }
193       
194       try
195       {
196          Scope1Interface iface2 = (Scope1Interface)pojo;
197          throw new RuntimeException JavaDoc("Should not be implementing Scope1Interface");
198       }
199       catch(ClassCastException JavaDoc expected)
200       {
201       }
202    }
203    
204 }
205
Popular Tags