1 15 package org.apache.hivemind.service.impl; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import java.lang.reflect.Constructor ; 20 import java.util.Collections ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.hivemind.Registry; 24 import org.apache.hivemind.definition.ImplementationConstructor; 25 import org.apache.hivemind.definition.ImplementationDefinition; 26 import org.apache.hivemind.definition.RegistryDefinition; 27 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 28 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl; 29 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl; 30 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl; 31 import org.apache.hivemind.impl.InterceptorStackImpl; 32 import org.apache.hivemind.internal.ServiceModel; 33 import org.apache.hivemind.internal.ServicePoint; 34 import org.apache.hivemind.service.ClassFactory; 35 import org.easymock.MockControl; 36 37 43 public class TestLoggingInterceptorFactory extends FrameworkTestCase 44 { 45 49 public void testLoggingOverProxy() throws Exception 50 { 51 ClassFactory cf = new ClassFactoryImpl(); 52 53 Runnable r = (Runnable ) newMock(Runnable .class); 54 MockControl logControl = newControl(Log.class); 55 Log log = (Log) logControl.getMock(); 56 57 LoggingInterceptorClassFactory f = new LoggingInterceptorClassFactory(cf); 58 59 MockControl spControl = newControl(ServicePoint.class); 60 ServicePoint sp = (ServicePoint) spControl.getMock(); 61 62 64 sp.getServiceInterface(); 65 spControl.setReturnValue(Runnable .class); 66 67 sp.getExtensionPointId(); 68 spControl.setReturnValue("foo.bar"); 69 70 replayControls(); 71 72 InterceptorStackImpl is = new InterceptorStackImpl(log, sp, r); 74 75 Class interceptorClass = f.constructInterceptorClass(is, Collections.EMPTY_LIST); 76 Constructor c = interceptorClass.getConstructors()[0]; 77 78 Object interceptor = c.newInstance(new Object [] { is.getServiceLog(), is.peek() }); 79 is.push(interceptor); 80 81 Runnable ri = (Runnable ) is.peek(); 82 83 verifyControls(); 84 85 87 log.isDebugEnabled(); 88 logControl.setReturnValue(true); 89 90 log.debug("BEGIN run()"); 91 log.debug("END run()"); 92 93 r.run(); 94 95 replayControls(); 96 97 ri.run(); 98 99 verifyControls(); 100 } 101 102 public void testJavassistProxies() throws Exception { 103 104 Registry reg = createRegistry(new JavassistBeanInterfaceFactory(newLocation(), "module")); 105 final BeanInterface bean = ( BeanInterface )reg.getService( "hivemind.tests.serviceByInterface.BeanInterface", BeanInterface.class ); 106 bean.interfaceMethod(); 107 } 108 109 public void testCglibProxies() throws Exception { 110 Registry reg = createRegistry(new CglibBeanInterfaceFactory(newLocation(), "module")); 111 final BeanInterface bean = ( BeanInterface )reg.getService( "hivemind.tests.serviceByInterface.BeanInterface", BeanInterface.class ); 112 bean.interfaceMethod(); 113 } 114 115 public void testJdkProxies() throws Exception { 116 Registry reg = createRegistry(new JdkBeanInterfaceFactory(newLocation(), "module")); 117 final BeanInterface bean = ( BeanInterface )reg.getService( "hivemind.tests.serviceByInterface.BeanInterface", BeanInterface.class ); 118 bean.interfaceMethod(); 119 } 120 121 122 126 private Registry createRegistry(ImplementationConstructor constructor) 127 { 128 RegistryDefinition definition = new RegistryDefinitionImpl(); 129 130 ModuleDefinitionImpl module = createModuleDefinition("hivemind.tests.serviceByInterface"); 131 definition.addModule(module); 132 133 ServicePointDefinitionImpl sp1 = createServicePointDefinition(module, "BeanInterface", BeanInterface.class); 134 ImplementationDefinition impl = new ImplementationDefinitionImpl(module, newLocation(), 135 constructor, ServiceModel.SINGLETON, true); 136 sp1.addImplementation(impl); 137 module.addServicePoint(sp1); 138 Registry reg = buildFrameworkRegistry(module); 139 return reg; 140 } 141 142 } | Popular Tags |