KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > proxy > TestMixin


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.proxy;
17
18 import net.sf.cglib.CodeGenTestCase;
19 import java.beans.*;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.*;
22 import junit.framework.*;
23
24 /**
25  * @author Chris Nokleberg
26  * @version $Id: TestMixin.java,v 1.5 2004/06/24 21:15:16 herbyderby Exp $
27  */

28 public class TestMixin extends CodeGenTestCase {
29     public void testSimple() throws Exception JavaDoc {
30         Object JavaDoc obj = Mixin.create(new Class JavaDoc[]{ DI1.class, DI2.class },
31                                    new Object JavaDoc[]{ new D1(), new D2() });
32         assertTrue(((DI1)obj).herby().equals("D1"));
33         assertTrue(((DI2)obj).derby().equals("D2"));
34     }
35
36     public void testDetermineInterfaces() throws Exception JavaDoc {
37         Object JavaDoc obj = Mixin.create(new Object JavaDoc[]{ new D1(), new D2() });
38         assertTrue(((DI1)obj).herby().equals("D1"));
39         assertTrue(((DI2)obj).derby().equals("D2"));
40     }
41  
42     public void testOverride() throws Exception JavaDoc {
43         Object JavaDoc obj = Mixin.create(new Object JavaDoc[]{ new D1(), new D4() });
44         assertTrue(((DI1)obj).herby().equals("D1"));
45         assertTrue(((DI2)obj).derby().equals("D4"));
46     }
47
48     public void testNonOverride() throws Exception JavaDoc {
49         Object JavaDoc obj = Mixin.create(new Object JavaDoc[]{ new D4(), new D1() });
50         assertTrue(((DI1)obj).herby().equals("D4"));
51         assertTrue(((DI2)obj).derby().equals("D4"));
52     }
53
54     public void testSubclass() throws Exception JavaDoc {
55         Object JavaDoc obj = Mixin.create(new Object JavaDoc[]{ new D3(), new D1() });
56         assertTrue(((DI1)obj).herby().equals("D1"));
57         assertTrue(((DI2)obj).derby().equals("D2"));
58         assertTrue(((DI3)obj).extra().equals("D3"));
59     }
60
61     public void testBeans() throws Exception JavaDoc {
62         Object JavaDoc obj = Mixin.createBean(new Object JavaDoc[]{ new DBean1(), new DBean2() });
63         Set getters = getGetters(obj.getClass());
64         assertTrue(getters.size() == 3); // name, age, class
65
assertTrue(getters.contains("name"));
66         assertTrue(getters.contains("age"));
67         assertTrue(!(obj instanceof DI1));
68     }
69
70     public void testEverything() throws Exception JavaDoc {
71         Mixin.Generator gen = new Mixin.Generator();
72         gen.setStyle(Mixin.STYLE_EVERYTHING);
73         gen.setDelegates(new Object JavaDoc[]{ new DBean1(), new DBean2() });
74         Object JavaDoc obj = gen.create();
75         Set getters = getGetters(obj.getClass());
76         assertTrue(getters.size() == 3); // name, age, class
77
assertTrue(obj instanceof DI1);
78         assertTrue(new DBean1().herby().equals(((DI1)obj).herby()));
79     }
80
81     public void testNullDelegates() throws Exception JavaDoc {
82         Mixin.Generator gen = new Mixin.Generator();
83         gen.setStyle(Mixin.STYLE_BEANS);
84         gen.setClasses(new Class JavaDoc[]{ DBean1.class, DBean2.class });
85         Mixin mixin = gen.create();
86         Object JavaDoc obj = mixin.newInstance(new Object JavaDoc[]{ new DBean1(), new DBean2() });
87     }
88
89     private static Set getGetters(Class JavaDoc beanClass) throws Exception JavaDoc {
90         Set getters = new HashSet();
91         PropertyDescriptor[] descriptors =
92             Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
93         for (int i = 0; i < descriptors.length; i++) {
94             if (descriptors[i].getReadMethod() != null) {
95                 getters.add(descriptors[i].getName());
96             }
97         }
98         return getters;
99     }
100
101     private static PropertyDescriptor getProperty(Class JavaDoc beanClass, String JavaDoc property) throws Exception JavaDoc {
102         Set getters = new HashSet();
103         PropertyDescriptor[] descriptors =
104             Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
105         for (int i = 0; i < descriptors.length; i++) {
106             if (descriptors[i].getName().equals(property))
107                 return descriptors[i];
108         }
109         return null;
110     }
111
112     public TestMixin(String JavaDoc testName) {
113         super(testName);
114     }
115     
116     public static void main(String JavaDoc[] args) {
117         junit.textui.TestRunner.run(suite());
118     }
119     
120     public static Test suite() {
121         return new TestSuite(TestMixin.class);
122     }
123     
124     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
125         Mixin.createBean(loader, new Object JavaDoc[]{ new DBean1(), new DBean2() });
126     }
127     
128     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
129         if(leaks()){
130           fail("Memory Leak in Mixin");
131         }
132     }
133     
134 }
135
Popular Tags