KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > javassist > test > DynProxyTestCase


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
23 package org.jboss.test.javassist.test;
24
25
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.AbstractCollection JavaDoc;
29
30 import javassist.util.proxy.ProxyFactory;
31 import junit.framework.TestCase;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.test.javassist.test.support.ThingMethodHandler;
35 import org.jboss.test.javassist.test.support.AThing;
36 import org.jboss.test.javassist.test.support.IThing;
37 import org.jboss.test.javassist.test.support.AbstractThing;
38 import org.jboss.test.javassist.test.support.MyCollectionHandler;
39
40 /**
41  * Tests of the javassit proxy framework
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 44647 $
45  */

46 public class DynProxyTestCase
47    extends TestCase
48 {
49    private static final Logger log = Logger.getLogger(DynProxyTestCase.class);
50
51    public DynProxyTestCase(String JavaDoc name)
52    {
53       super(name);
54    }
55
56    /**
57     * Test creating a proxy for an interface
58     * @throws Exception
59     */

60    public void testInterfaceProxy() throws Exception JavaDoc
61    {
62       log.info("+++ testInterfaceProxy");
63       ProxyFactory factory = new ProxyFactory();
64       AThing athing = new AThing();
65       ThingMethodHandler handler = new ThingMethodHandler(athing);
66       factory.setHandler(handler);
67       Class JavaDoc[] ifaces = {IThing.class};
68       factory.setInterfaces(ifaces);
69       Class JavaDoc[] sig = {};
70       Object JavaDoc[] args = {};
71       IThing proxy = (IThing) factory.create(sig, args);
72       proxy.method1();
73       assertEquals("method1Count", 1, athing.getMethod1Count());
74       proxy.method2("testInterfaceProxy");
75       assertEquals("method2Count", 1, athing.getMethod2Count());
76       proxy.method3(proxy);
77       assertEquals("method3Count", 1, athing.getMethod3Count());
78       assertEquals("method2Count", 2, athing.getMethod2Count());
79    }
80
81    /**
82     * Test creating a proxy for an abstract class
83     * @throws Exception
84     */

85    public void testAbstractProxy() throws Exception JavaDoc
86    {
87       log.info("+++ testAbstractProxy");
88       ProxyFactory factory = new ProxyFactory();
89       AThing athing = new AThing();
90       ThingMethodHandler handler = new ThingMethodHandler(athing);
91       factory.setHandler(handler);
92       factory.setSuperclass(AbstractThing.class);
93       Class JavaDoc[] sig = {};
94       Object JavaDoc[] args = {};
95       AbstractThing proxy = (AbstractThing) factory.create(sig, args);
96       proxy.method1();
97       assertEquals("method1Count", 1, athing.getMethod1Count());
98       proxy.method2("testInterfaceProxy");
99       assertEquals("method2Count", 1, athing.getMethod2Count());
100       proxy.method3(athing);
101       assertEquals("method3Count", 1, athing.getMethod3Count());
102       assertEquals("method2Count", 2, athing.getMethod2Count());
103    }
104
105    /**
106     * Test creating a proxy for an abstract class with the abstract
107     * superclass being from the jdk (java.util.AbstractCollection).
108     *
109     * @throws Exception
110     */

111    public void testAbstractJDKClassProxy() throws Exception JavaDoc
112    {
113       log.info("+++ testAbstractJDKClassProxy");
114       ProxyFactory factory = new ProxyFactory();
115       HashSet JavaDoc aset = new HashSet JavaDoc();
116       MyCollectionHandler handler = new MyCollectionHandler(aset);
117       factory.setHandler(handler);
118       factory.setSuperclass(java.util.AbstractCollection JavaDoc.class);
119       Class JavaDoc[] sig = {};
120       Object JavaDoc[] args = {};
121       AbstractCollection JavaDoc proxy = (AbstractCollection JavaDoc) factory.create(sig, args);
122       proxy.add("Add");
123       assertEquals("size", 1, aset.size());
124       proxy.remove("Add");
125       assertEquals("size", 0, aset.size());
126       assertEquals("isEmpty", true, proxy.isEmpty());
127    }
128 }
129
Popular Tags