1 16 package net.sf.cglib.proxy; 17 18 import net.sf.cglib.CodeGenTestCase; 19 import java.beans.*; 20 import java.lang.reflect.Method ; 21 import java.util.*; 22 import junit.framework.*; 23 24 28 public class TestMixin extends CodeGenTestCase { 29 public void testSimple() throws Exception { 30 Object obj = Mixin.create(new Class []{ DI1.class, DI2.class }, 31 new Object []{ 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 { 37 Object obj = Mixin.create(new Object []{ 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 { 43 Object obj = Mixin.create(new Object []{ 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 { 49 Object obj = Mixin.create(new Object []{ 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 { 55 Object obj = Mixin.create(new Object []{ 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 { 62 Object obj = Mixin.createBean(new Object []{ new DBean1(), new DBean2() }); 63 Set getters = getGetters(obj.getClass()); 64 assertTrue(getters.size() == 3); assertTrue(getters.contains("name")); 66 assertTrue(getters.contains("age")); 67 assertTrue(!(obj instanceof DI1)); 68 } 69 70 public void testEverything() throws Exception { 71 Mixin.Generator gen = new Mixin.Generator(); 72 gen.setStyle(Mixin.STYLE_EVERYTHING); 73 gen.setDelegates(new Object []{ new DBean1(), new DBean2() }); 74 Object obj = gen.create(); 75 Set getters = getGetters(obj.getClass()); 76 assertTrue(getters.size() == 3); assertTrue(obj instanceof DI1); 78 assertTrue(new DBean1().herby().equals(((DI1)obj).herby())); 79 } 80 81 public void testNullDelegates() throws Exception { 82 Mixin.Generator gen = new Mixin.Generator(); 83 gen.setStyle(Mixin.STYLE_BEANS); 84 gen.setClasses(new Class []{ DBean1.class, DBean2.class }); 85 Mixin mixin = gen.create(); 86 Object obj = mixin.newInstance(new Object []{ new DBean1(), new DBean2() }); 87 } 88 89 private static Set getGetters(Class beanClass) throws Exception { 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 beanClass, String property) throws Exception { 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 testName) { 113 super(testName); 114 } 115 116 public static void main(String [] 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 loader) throws Throwable { 125 Mixin.createBean(loader, new Object []{ new DBean1(), new DBean2() }); 126 } 127 128 public void testFailOnMemoryLeak() throws Throwable { 129 if(leaks()){ 130 fail("Memory Leak in Mixin"); 131 } 132 } 133 134 } 135 | Popular Tags |