KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > beans > TestBeanMap


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.beans;
17
18 import net.sf.cglib.proxy.*;
19 import net.sf.cglib.core.Constants;
20 import net.sf.cglib.core.ReflectUtils;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.*;
23 import junit.framework.*;
24
25 public class TestBeanMap extends net.sf.cglib.CodeGenTestCase {
26     public static class TestBean {
27         private String JavaDoc foo;
28         private String JavaDoc bar = "x";
29         private String JavaDoc baz;
30         private int quud;
31         private int quick = 42;
32         private int quip;
33
34         public String JavaDoc getFoo() {
35             return foo;
36         }
37         
38         public void setFoo(String JavaDoc value) {
39             foo = value;
40         }
41
42         public String JavaDoc getBar() {
43             return bar;
44         }
45         
46         public void setBaz(String JavaDoc value) {
47             baz = value;
48         }
49
50         public int getQuud() {
51             return quud;
52         }
53         
54         public void setQuud(int value) {
55             quud = value;
56         }
57
58         public int getQuick() {
59             return quick;
60         }
61         
62         public void setQuip(int value) {
63             quip = value;
64         }
65     }
66
67     public void testBeanMap() {
68         TestBean bean = new TestBean();
69         BeanMap map = BeanMap.create(bean);
70         assertTrue(map.size() == 6);
71         assertTrue(map.get("foo") == null);
72         map.put("foo", "FOO");
73         assertTrue("FOO".equals(map.get("foo")));
74         assertTrue(bean.getFoo().equals("FOO"));
75         assertTrue("x".equals(map.get("bar")));
76         assertTrue(((Integer JavaDoc)map.get("quick")).intValue() == 42);
77         map.put("quud", new Integer JavaDoc(13));
78         assertTrue(bean.getQuud() == 13);
79
80         assertTrue(map.getPropertyType("foo").equals(String JavaDoc.class));
81         assertTrue(map.getPropertyType("quud").equals(Integer.TYPE));
82         assertTrue(map.getPropertyType("kdkkj") == null);
83     }
84
85     public void testEntrySet() {
86         TestBean bean = new TestBean();
87         BeanMap map = BeanMap.create(bean);
88         assertTrue(map.entrySet().size() == map.size());
89     }
90
91     public void testNoUnderlyingBean() {
92         BeanMap.Generator gen = new BeanMap.Generator();
93         gen.setBeanClass(TestBean.class);
94         BeanMap map = gen.create();
95
96         TestBean bean = new TestBean();
97         assertTrue(bean.getFoo() == null);
98         assertTrue(map.put(bean, "foo", "FOO") == null);
99         assertTrue(bean.getFoo().equals("FOO"));
100         assertTrue(map.get(bean, "foo").equals("FOO"));
101     }
102
103     public void testMixinMapIntoBean() {
104         Object JavaDoc bean = new TestBean();
105         bean = mixinMapIntoBean(bean);
106         ((TestBean)bean).setFoo("hello");
107         assertTrue(bean instanceof Map);
108         assertTrue(((Map)bean).get("foo").equals("hello"));
109     }
110
111     public void testRequire() {
112         BeanMap.Generator gen = new BeanMap.Generator();
113         gen.setBeanClass(TestBean.class);
114         gen.setRequire(BeanMap.REQUIRE_GETTER);
115         BeanMap map = gen.create();
116         assertTrue(map.containsKey("foo"));
117         assertTrue(map.containsKey("bar"));
118         assertTrue(!map.containsKey("baz"));
119     }
120
121     public static Object JavaDoc mixinMapIntoBean(final Object JavaDoc bean) {
122         Enhancer e = new Enhancer();
123         e.setSuperclass(bean.getClass());
124         e.setInterfaces(new Class JavaDoc[]{ Map.class });
125         final Map map = BeanMap.create(bean);
126         e.setCallbackFilter(new CallbackFilter() {
127             public int accept(Method JavaDoc method) {
128                 return method.getDeclaringClass().equals(Map.class) ? 1 : 0;
129             }
130         });
131         e.setCallbacks(new Callback[]{
132             new Dispatcher() {
133                 public Object JavaDoc loadObject() {
134                     return bean;
135                 }
136             },
137             new Dispatcher() {
138                 public Object JavaDoc loadObject() {
139                     return map;
140                 }
141             }
142         });
143         return e.create();
144     }
145
146     // TODO: test different package
147
// TODO: test change bean instance
148
// TODO: test toString
149

150     public TestBeanMap(String JavaDoc testName) {
151         super(testName);
152     }
153     
154     public static void main(String JavaDoc[] args) {
155         junit.textui.TestRunner.run(suite());
156     }
157     
158     public static Test suite() {
159         return new TestSuite(TestBeanMap.class);
160     }
161     
162     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
163         //tested in enhancer test unit
164
}
165     
166     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
167     }
168     
169 }
170
Popular Tags