1 15 package org.apache.hivemind.service.impl; 16 17 import java.lang.reflect.Modifier ; 18 19 import org.apache.hivemind.service.ClassFab; 20 import org.apache.hivemind.service.ClassFabUtils; 21 import org.apache.hivemind.service.MethodFab; 22 import org.apache.hivemind.service.MethodSignature; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 import org.easymock.MockControl; 25 26 32 public class TestClassFabUtils extends HiveMindTestCase { 33 34 35 public void testGetInstanceClass() { 36 final HiveMindClassPool pool = new HiveMindClassPool(); 37 final CtClassSource classSource = new CtClassSource(pool); 38 final ClassFab classFab = new ClassFabImpl(classSource, pool 39 .makeClass("Dummy")); 40 assertSame(ClassFabUtils.getInstanceClass(classFab, 41 new BeanInterfaceImpl(), BeanInterface.class), 42 BeanInterfaceImpl.class); 43 assertSame( ClassFabUtils.getInstanceClass(classFab,CglibBeanInterfaceFactory.createCglibBean(), BeanInterface.class ), BeanInterface.class ); 44 assertSame( ClassFabUtils.getInstanceClass(classFab,JavassistBeanInterfaceFactory.createJavassistBean(), BeanInterface.class ), BeanInterface.class ); 45 assertSame( ClassFabUtils.getInstanceClass(classFab,JdkBeanInterfaceFactory.createJdkBean(), BeanInterface.class ), BeanInterface.class ); 46 47 } 48 49 public static class BeanInterfaceImpl implements BeanInterface { 50 51 public String interfaceMethod() { 52 return "Hello, World!"; 53 } 54 55 } 56 57 58 public void testAddNoOpMethod() { 59 tryAddNoOpMethod(void.class, "{ }"); 60 tryAddNoOpMethod(String .class, "{ return null; }"); 61 tryAddNoOpMethod(boolean.class, "{ return false; }"); 62 tryAddNoOpMethod(char.class, "{ return 0; }"); 63 tryAddNoOpMethod(short.class, "{ return 0; }"); 64 tryAddNoOpMethod(int.class, "{ return 0; }"); 65 tryAddNoOpMethod(long.class, "{ return 0L; }"); 66 tryAddNoOpMethod(double.class, "{ return 0.0d; }"); 67 tryAddNoOpMethod(float.class, "{ return 0.0f; }"); 68 } 69 70 71 private void tryAddNoOpMethod(Class returnClass, String expectedBody) { 72 MethodSignature sig = new MethodSignature(returnClass, "run", null, 73 null); 74 75 MockControl control = newControl(ClassFab.class); 76 ClassFab cf = (ClassFab) control.getMock(); 77 MethodFab mf = (MethodFab) newMock(MethodFab.class); 78 79 cf.addMethod(Modifier.PUBLIC, sig, expectedBody); 80 control.setReturnValue(mf); 81 82 replayControls(); 83 84 ClassFabUtils.addNoOpMethod(cf, sig); 85 86 verifyControls(); 87 } 88 89 90 public void testGenerateClassName() throws Exception { 91 String name = ClassFabUtils.generateClassName(Runnable .class); 92 93 assertRegexp("\\$Runnable_([0-9|a-f])+", name); 94 } 95 } | Popular Tags |