1 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 ; 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 foo; 28 private String bar = "x"; 29 private String baz; 30 private int quud; 31 private int quick = 42; 32 private int quip; 33 34 public String getFoo() { 35 return foo; 36 } 37 38 public void setFoo(String value) { 39 foo = value; 40 } 41 42 public String getBar() { 43 return bar; 44 } 45 46 public void setBaz(String 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 )map.get("quick")).intValue() == 42); 77 map.put("quud", new Integer (13)); 78 assertTrue(bean.getQuud() == 13); 79 80 assertTrue(map.getPropertyType("foo").equals(String .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 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 : 0; 129 } 130 }); 131 e.setCallbacks(new Callback[]{ 132 new Dispatcher() { 133 public Object loadObject() { 134 return bean; 135 } 136 }, 137 new Dispatcher() { 138 public Object loadObject() { 139 return map; 140 } 141 } 142 }); 143 return e.create(); 144 } 145 146 150 public TestBeanMap(String testName) { 151 super(testName); 152 } 153 154 public static void main(String [] 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 loader) throws Throwable { 163 } 165 166 public void testFailOnMemoryLeak() throws Throwable { 167 } 168 169 } 170 | Popular Tags |