001: /*
002: * Copyright 2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.beans;
017:
018: import net.sf.cglib.proxy.*;
019: import net.sf.cglib.core.Constants;
020: import net.sf.cglib.core.ReflectUtils;
021: import java.lang.reflect.Method;
022: import java.util.*;
023: import junit.framework.*;
024:
025: public class TestBeanMap extends net.sf.cglib.CodeGenTestCase {
026: public static class TestBean {
027: private String foo;
028: private String bar = "x";
029: private String baz;
030: private int quud;
031: private int quick = 42;
032: private int quip;
033:
034: public String getFoo() {
035: return foo;
036: }
037:
038: public void setFoo(String value) {
039: foo = value;
040: }
041:
042: public String getBar() {
043: return bar;
044: }
045:
046: public void setBaz(String value) {
047: baz = value;
048: }
049:
050: public int getQuud() {
051: return quud;
052: }
053:
054: public void setQuud(int value) {
055: quud = value;
056: }
057:
058: public int getQuick() {
059: return quick;
060: }
061:
062: public void setQuip(int value) {
063: quip = value;
064: }
065: }
066:
067: public void testBeanMap() {
068: TestBean bean = new TestBean();
069: BeanMap map = BeanMap.create(bean);
070: assertTrue(map.size() == 6);
071: assertTrue(map.get("foo") == null);
072: map.put("foo", "FOO");
073: assertTrue("FOO".equals(map.get("foo")));
074: assertTrue(bean.getFoo().equals("FOO"));
075: assertTrue("x".equals(map.get("bar")));
076: assertTrue(((Integer) map.get("quick")).intValue() == 42);
077: map.put("quud", new Integer(13));
078: assertTrue(bean.getQuud() == 13);
079:
080: assertTrue(map.getPropertyType("foo").equals(String.class));
081: assertTrue(map.getPropertyType("quud").equals(Integer.TYPE));
082: assertTrue(map.getPropertyType("kdkkj") == null);
083: }
084:
085: public void testEntrySet() {
086: TestBean bean = new TestBean();
087: BeanMap map = BeanMap.create(bean);
088: assertTrue(map.entrySet().size() == map.size());
089: }
090:
091: public void testNoUnderlyingBean() {
092: BeanMap.Generator gen = new BeanMap.Generator();
093: gen.setBeanClass(TestBean.class);
094: BeanMap map = gen.create();
095:
096: TestBean bean = new TestBean();
097: assertTrue(bean.getFoo() == null);
098: assertTrue(map.put(bean, "foo", "FOO") == null);
099: assertTrue(bean.getFoo().equals("FOO"));
100: assertTrue(map.get(bean, "foo").equals("FOO"));
101: }
102:
103: public void testMixinMapIntoBean() {
104: Object 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 mixinMapIntoBean(final Object bean) {
122: Enhancer e = new Enhancer();
123: e.setSuperclass(bean.getClass());
124: e.setInterfaces(new Class[] { Map.class });
125: final Map map = BeanMap.create(bean);
126: e.setCallbackFilter(new CallbackFilter() {
127: public int accept(Method method) {
128: return method.getDeclaringClass().equals(Map.class) ? 1
129: : 0;
130: }
131: });
132: e.setCallbacks(new Callback[] { new Dispatcher() {
133: public Object loadObject() {
134: return bean;
135: }
136: }, new Dispatcher() {
137: public Object loadObject() {
138: return map;
139: }
140: } });
141: return e.create();
142: }
143:
144: // TODO: test different package
145: // TODO: test change bean instance
146: // TODO: test toString
147:
148: public TestBeanMap(String testName) {
149: super (testName);
150: }
151:
152: public static void main(String[] args) {
153: junit.textui.TestRunner.run(suite());
154: }
155:
156: public static Test suite() {
157: return new TestSuite(TestBeanMap.class);
158: }
159:
160: public void perform(ClassLoader loader) throws Throwable {
161: //tested in enhancer test unit
162: }
163:
164: public void testFailOnMemoryLeak() throws Throwable {
165: }
166:
167: }
|