KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > microcontainer > junit > AbstractProxyTest


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.aop.microcontainer.junit;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.jboss.aop.metadata.SimpleMetaData;
28 import org.jboss.aop.proxy.container.AOPProxyFactory;
29 import org.jboss.aop.proxy.container.AOPProxyFactoryMixin;
30 import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
31 import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
32 import org.jboss.test.AbstractTestCaseWithSetup;
33 import org.jboss.test.AbstractTestDelegate;
34
35 /**
36  * AbstractProxyTest.
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 45764 $
40  */

41 public abstract class AbstractProxyTest extends AbstractTestCaseWithSetup
42 {
43    /** The proxy factory */
44    protected AOPProxyFactory proxyFactory;
45
46    /**
47     * Get the test delegate
48     *
49     * @param clazz the test class
50     * @return the delegate
51     * @throws Exception for any error
52     */

53    public static AbstractTestDelegate getDelegate(Class JavaDoc clazz) throws Exception JavaDoc
54    {
55       String JavaDoc property = System.getProperty("jboss.mc.secure", "false");
56       boolean enableSecurity = Boolean.valueOf(property).booleanValue();
57       AbstractProxyTestDelegate delegate = new AbstractProxyTestDelegate(clazz);
58       delegate.enableSecurity = enableSecurity;
59       return delegate;
60    }
61
62    /**
63     * Create a new AbstractProxyTest.
64     *
65     * @param name the test name
66     */

67    public AbstractProxyTest(String JavaDoc name)
68    {
69       super(name);
70    }
71    
72    protected void setUp() throws Exception JavaDoc
73    {
74       super.setUp();
75       configureLogging();
76       proxyFactory = new GeneratedAOPProxyFactory();
77    }
78
79    /**
80     * Create a proxy
81     *
82     * @param target the target
83     * @return the proxy
84     * @throws Exception for any error
85     */

86    protected Object JavaDoc createProxy(Object JavaDoc target) throws Exception JavaDoc
87    {
88       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
89       params.setProxiedClass(target.getClass());
90       params.setTarget(target);
91       return proxyFactory.createAdvisedProxy(params);
92    }
93
94    /**
95     * Create a proxy
96     *
97     * @param target the target
98     * @param expected the expected class
99     * @return the proxy
100     * @throws Exception for any error
101     */

102    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc expected) throws Exception JavaDoc
103    {
104       Object JavaDoc proxy = createProxy(target);
105       assertNotNull(proxy);
106       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
107       return proxy;
108    }
109
110    /**
111     * Create a proxy
112     *
113     * @param target the target
114     * @param interfaces the interfaces
115     * @return the proxy
116     * @throws Exception for any error
117     */

118    protected Object JavaDoc createProxy(Object JavaDoc target, Class JavaDoc[] interfaces) throws Exception JavaDoc
119    {
120       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
121       params.setProxiedClass(target.getClass());
122       params.setInterfaces(interfaces);
123       params.setTarget(target);
124       return proxyFactory.createAdvisedProxy(params);
125    }
126
127    /**
128     * Create a proxy
129     *
130     * @param target the target
131     * @param mixins the mixins
132     * @return the proxy
133     * @throws Exception for any error
134     */

135    protected Object JavaDoc createProxy(Object JavaDoc target, AOPProxyFactoryMixin[] mixins) throws Exception JavaDoc
136    {
137       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
138       params.setProxiedClass(target.getClass());
139       params.setMixins(mixins);
140       params.setTarget(target);
141       return proxyFactory.createAdvisedProxy(params);
142    }
143
144    /**
145     * Create a proxy
146     *
147     * @param target the target
148     * @param interfaces the interfaces
149     * @param mixins the mixins
150     * @return the proxy
151     * @throws Exception for any error
152     */

153    protected Object JavaDoc createProxy(Object JavaDoc target, Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins) throws Exception JavaDoc
154    {
155       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
156       params.setProxiedClass(target.getClass());
157       params.setInterfaces(interfaces);
158       params.setMixins(mixins);
159       params.setTarget(target);
160       return proxyFactory.createAdvisedProxy(params);
161    }
162    /**
163     * Create a proxy
164     *
165     * @param target the target
166     * @param mixins the mixins
167     * @param expected the expected class
168     * @return the proxy
169     * @throws Exception for any error
170     */

171    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, AOPProxyFactoryMixin[] mixins, Class JavaDoc expected) throws Exception JavaDoc
172    {
173       Object JavaDoc proxy = createProxy(target, mixins);
174       assertNotNull(proxy);
175       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
176       return proxy;
177    }
178
179    /**
180     * Create a proxy
181     *
182     * @param target the target
183     * @param interfaces the interfaces
184     * @param mixins the mixins
185     * @param expected the expected class
186     * @return the proxy
187     * @throws Exception for any error
188     */

189    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, Class JavaDoc[] expected) throws Exception JavaDoc
190    {
191       Object JavaDoc proxy = createProxy(target, interfaces, mixins);
192       assertNotNull(proxy);
193       for (int i = 0 ; i < expected.length ; i++)
194       {
195          assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy));
196       }
197       return proxy;
198    }
199
200    /**
201     * Create a proxy
202     *
203     * @param target the target
204     * @param interfaces the interfaces
205     * @param expected the expected class
206     * @return the proxy
207     * @throws Exception for any error
208     */

209    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc[] interfaces, Class JavaDoc expected) throws Exception JavaDoc
210    {
211       Object JavaDoc proxy = createProxy(target, interfaces);
212       assertNotNull(proxy);
213       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
214       return proxy;
215    }
216
217    /**
218     * Create a proxy
219     *
220     * @param target the target
221     * @param interfaces the interfaces
222     * @param metadata the metadata
223     * @return the proxy
224     * @throws Exception for any error
225     */

226    protected Object JavaDoc createProxy(Object JavaDoc target, Class JavaDoc[] interfaces, SimpleMetaData metaData) throws Exception JavaDoc
227    {
228       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
229       params.setProxiedClass(target.getClass());
230       params.setInterfaces(interfaces);
231       params.setSimpleMetaData(metaData);
232       params.setTarget(target);
233       return proxyFactory.createAdvisedProxy(params);
234    }
235
236    /**
237     * Create a proxy
238     *
239     * @param target the target
240     * @param interfaces the interfaces
241     * @param metadata the metadata
242     * @param expected the expected class
243     * @return the proxy
244     * @throws Exception for any error
245     */

246    protected Object JavaDoc assertCreateProxy(Object JavaDoc target, Class JavaDoc[] interfaces, SimpleMetaData metaData, Class JavaDoc expected) throws Exception JavaDoc
247    {
248       Object JavaDoc proxy = createProxy(target, interfaces, metaData);
249       assertNotNull(proxy);
250       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
251       return proxy;
252    }
253
254    /**
255     * Create a proxy
256     *
257     * @param interfaces the interfaces
258     * @param metaData the metadata
259     * @return the proxy
260     * @throws Exception for any error
261     */

262    protected Object JavaDoc createHollowProxy(Class JavaDoc[] interfaces, SimpleMetaData metaData) throws Exception JavaDoc
263    {
264       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
265       params.setInterfaces(interfaces);
266       params.setSimpleMetaData(metaData);
267       return proxyFactory.createAdvisedProxy(params);
268    }
269
270    /**
271     * Create a proxy
272     *
273     * @param mixins the mixins
274     * @param metaData the metadata
275     * @return the proxy
276     * @throws Exception for any error
277     */

278    protected Object JavaDoc createHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception JavaDoc
279    {
280       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
281       params.setMixins(mixins);
282       params.setSimpleMetaData(metaData);
283       return proxyFactory.createAdvisedProxy(params);
284    }
285
286    /**
287     * Create a proxy
288     *
289     * @param mixins the mixins
290     * @param metaData the metadata
291     * @return the proxy
292     * @throws Exception for any error
293     */

294    protected Object JavaDoc createHollowProxy(Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception JavaDoc
295    {
296       AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
297       params.setInterfaces(interfaces);
298       params.setMixins(mixins);
299       params.setSimpleMetaData(metaData);
300       return proxyFactory.createAdvisedProxy(params);
301    }
302
303    /**
304     * Create a proxy
305     *
306     * @param interfaces the interfaces
307     * @param metadata the metadata
308     * @param expected the expected class
309     * @return the proxy
310     * @throws Exception for any error
311     */

312    protected Object JavaDoc assertCreateHollowProxy(Class JavaDoc[] interfaces, SimpleMetaData metaData, Class JavaDoc expected) throws Exception JavaDoc
313    {
314       Object JavaDoc proxy = createHollowProxy(interfaces, metaData);
315       assertNotNull(proxy);
316       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
317       return proxy;
318    }
319    
320    /**
321     * Create a proxy
322     *
323     * @param interfaces the interfaces
324     * @param metadata the metadata
325     * @param expected the expected class
326     * @return the proxy
327     * @throws Exception for any error
328     */

329    protected Object JavaDoc assertCreateHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class JavaDoc expected) throws Exception JavaDoc
330    {
331       Object JavaDoc proxy = createHollowProxy(mixins, metaData);
332       assertNotNull(proxy);
333       assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy));
334       return proxy;
335    }
336
337    /**
338     * Create a proxy
339     *
340     * @param target the target
341     * @param interfaces the interfaces
342     * @param mixins the mixins
343     * @param expected the expected class
344     * @return the proxy
345     * @throws Exception for any error
346     */

347    protected Object JavaDoc assertCreateHollowProxy(Class JavaDoc[] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class JavaDoc[] expected) throws Exception JavaDoc
348    {
349       Object JavaDoc proxy = createHollowProxy(interfaces, mixins, metaData);
350       assertNotNull(proxy);
351       for (int i = 0 ; i < expected.length ; i++)
352       {
353          assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy));
354       }
355       return proxy;
356    }
357    /**
358     * Get the interfaces for an object
359     *
360     * @param object the object
361     * @return the set of interfaces
362     */

363    protected Set JavaDoc getInterfaces(Object JavaDoc object)
364    {
365       Set JavaDoc<Class JavaDoc> interfaces = new HashSet JavaDoc<Class JavaDoc>();
366       addInterfaces(interfaces, object.getClass());
367       return interfaces;
368    }
369    
370    /**
371     * Add interfaces
372     *
373     * @param interfaces the interfaces to add to
374     * @param clazz the class
375     */

376    protected void addInterfaces(Set JavaDoc<Class JavaDoc> interfaces, Class JavaDoc clazz)
377    {
378       Class JavaDoc[] intfs = clazz.getInterfaces();
379       for (int i = 0; i < intfs.length; ++i)
380          interfaces.add(intfs[i]);
381       Class JavaDoc superClass = clazz.getSuperclass();
382       if (superClass != null)
383          addInterfaces(interfaces, superClass);
384    }
385    
386    /**
387     * Get the delegate
388     *
389     * @return the delegate
390     */

391    protected AbstractProxyTestDelegate getMCDelegate()
392    {
393       return (AbstractProxyTestDelegate) getDelegate();
394    }
395
396 }
397
Popular Tags