1 15 package org.apache.hivemind.impl.servicemodel; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import org.apache.hivemind.Registry; 20 import org.apache.hivemind.ShutdownCoordinator; 21 import org.apache.hivemind.definition.ImplementationConstructionContext; 22 import org.apache.hivemind.definition.ImplementationConstructor; 23 import org.apache.hivemind.definition.ServicePointDefinition; 24 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper; 25 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 26 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor; 27 import org.apache.hivemind.internal.ServiceModel; 28 29 33 public class TestRegistryShutdownListenerServices extends FrameworkTestCase 34 { 35 private void executeShutdownListenerTest(String serviceModel, boolean manual) throws Exception 36 { 37 Registry registry = createRegistryWithSimpleService(serviceModel, manual); 38 Simple simple = (Simple) registry.getService("hivemind.lib.test.Simple", Simple.class); 39 final Counter counter = new Counter(); 40 simple.setCounter(counter); 41 registry.shutdown(); 42 assertEquals(1, counter.getValue()); 43 } 44 45 public void testPooledCalled() throws Exception 46 { 47 executeShutdownListenerTest(ServiceModel.POOLED, true); 48 executeShutdownListenerTest(ServiceModel.POOLED, false); 49 } 50 51 public void testSingleton() throws Exception 52 { 53 executeShutdownListenerTest(ServiceModel.SINGLETON, true); 54 executeShutdownListenerTest(ServiceModel.SINGLETON, false); 55 } 56 57 public void testPrimitive() throws Exception 58 { 59 executeShutdownListenerTest(ServiceModel.PRIMITIVE, true); 60 executeShutdownListenerTest(ServiceModel.PRIMITIVE, false); 61 } 62 63 public void testSingletonBeanRegistryShutdownListener() throws Exception 64 { 65 Registry registry = createRegistryWithPojo(ServiceModel.SINGLETON); 66 RegistryShutdownBean bean = ( RegistryShutdownBean )registry.getService( "hivemind.lib.test.RegistryShutdownBean", RegistryShutdownBean.class ); 67 bean.someMethod(); 68 } 69 70 public void testThreadedBeanRegistryShutdownListener() throws Exception 71 { 72 Registry registry = createRegistryWithPojo(ServiceModel.THREADED); 73 RegistryShutdownBean bean = ( RegistryShutdownBean )registry.getService( "hivemind.lib.test.RegistryShutdownBean", RegistryShutdownBean.class ); 74 bean.someMethod(); 75 } 76 77 public void testPooledBeanRegistryShutdownListener() throws Exception 78 { 79 Registry registry = createRegistryWithPojo(ServiceModel.POOLED); 80 RegistryShutdownBean bean = ( RegistryShutdownBean )registry.getService( "hivemind.lib.test.RegistryShutdownBean", RegistryShutdownBean.class ); 81 bean.someMethod(); 82 } 83 84 public void testPrimitiveBeanRegistryShutdownListener() throws Exception 85 { 86 Registry registry = createRegistryWithPojo(ServiceModel.PRIMITIVE); 87 RegistryShutdownBean bean = ( RegistryShutdownBean )registry.getService( "hivemind.lib.test.RegistryShutdownBean", RegistryShutdownBean.class ); 88 bean.someMethod(); 89 } 90 91 96 private Registry createRegistryWithSimpleService(final String serviceModel, final boolean manual) 97 { 98 ModuleDefinitionImpl module = createModuleDefinition("hivemind.lib.test"); 99 ModuleDefinitionHelper helper = new ModuleDefinitionHelper(module); 100 101 ServicePointDefinition sp1 = helper.addServicePoint("Simple", Simple.class.getName()); 102 103 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(module.getLocation()) 105 { 106 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 107 { 108 Object result; 109 if (manual) { 110 ShutdownCoordinator coordinator = (ShutdownCoordinator) context.getService(ShutdownCoordinator.class); 111 result = new SimpleImpl(coordinator); 112 } else { 113 result = new SimpleImpl(); 114 } 115 return result; 116 } 117 }; 118 119 helper.addServiceImplementation(sp1, constructor, serviceModel); 120 121 return buildFrameworkRegistry(module); 122 } 123 124 128 private Registry createRegistryWithPojo(final String serviceModel) 129 { 130 ModuleDefinitionImpl module = createModuleDefinition("hivemind.lib.test"); 131 ModuleDefinitionHelper helper = new ModuleDefinitionHelper(module); 132 133 ServicePointDefinition sp1 = helper.addServicePoint("RegistryShutdownBean", RegistryShutdownBean.class.getName()); 134 helper.addSimpleServiceImplementation(sp1, RegistryShutdownBean.class.getName(), serviceModel); 135 136 return buildFrameworkRegistry(module); 137 } 138 139 } 140 | Popular Tags |