1 15 package hivemind.test.services; 16 17 import hivemind.test.FrameworkTestCase; 18 import hivemind.test.services.impl.RunnableImpl; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.Registry; 22 import org.apache.hivemind.definition.ImplementationConstructionContext; 23 import org.apache.hivemind.definition.ImplementationConstructor; 24 import org.apache.hivemind.definition.ImplementationDefinition; 25 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 26 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl; 27 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl; 28 import org.apache.hivemind.events.RegistryShutdownListener; 29 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor; 30 import org.apache.hivemind.internal.ServiceModel; 31 32 37 public class TestShutdown extends FrameworkTestCase 38 { 39 40 public void testShutdownSingleton() throws Exception 41 { 42 Registry r = buildFrameworkRegistry(new SimpleModule()); 43 SimpleService s = (SimpleService) r.getService( 44 "hivemind.test.services.Simple", 45 SimpleService.class); 46 47 assertEquals(11, s.add(4, 7)); 48 49 r.shutdown(); 50 51 try 52 { 53 s.add(9, 5); 54 unreachable(); 55 } 56 catch (ApplicationRuntimeException ex) 57 { 58 assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown."); 59 } 60 } 61 62 public void testRegistryShutdownUnrepeatable() throws Exception 63 { 64 Registry r = buildFrameworkRegistry(new SimpleModule()); 65 66 r.shutdown(); 67 68 try 69 { 70 r.getConfiguration("foo.bar"); 71 unreachable(); 72 } 73 catch (ApplicationRuntimeException ex) 74 { 75 assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown."); 76 } 77 78 try 79 { 80 r.shutdown(); 81 unreachable(); 82 } 83 catch (ApplicationRuntimeException ex) 84 { 85 assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown."); 86 } 87 } 88 89 public void testShutdownThreaded() throws Exception 90 { 91 Registry r = buildFrameworkRegistry(new StringHolderModule(ServiceModel.THREADED)); 92 93 StringHolder h = (StringHolder) r.getService( 94 "hivemind.test.services.StringHolder", 95 StringHolder.class); 96 97 assertNull(h.getValue()); 98 99 h.setValue("fred"); 100 101 assertEquals("fred", h.getValue()); 102 103 r.shutdown(); 104 105 try 106 { 107 h.getValue(); 108 unreachable(); 109 } 110 catch (ApplicationRuntimeException ex) 111 { 112 assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown."); 113 } 114 } 115 116 public void testSingletonCore() throws Exception 117 { 118 Registry r = createModuleWithShutdownListener(ServiceModel.SINGLETON); 119 120 Runnable s = (Runnable ) r.getService("module1.Listener", Runnable .class); 121 122 interceptLogging("hivemind.test.services.impl.RunnableImpl"); 123 124 s.run(); 125 126 assertLoggedMessage("run -- singleton"); 127 128 r.shutdown(); 129 130 assertLoggedMessage("registryDidShutdown -- singleton"); 131 } 132 133 public void testPrimitiveCore() throws Exception 134 { 135 Registry r = createModuleWithShutdownListener(ServiceModel.PRIMITIVE); 136 137 Runnable s = (Runnable ) r.getService("module1.Listener", Runnable .class); 138 139 interceptLogging("hivemind.test.services.impl.RunnableImpl"); 140 141 s.run(); 142 143 assertLoggedMessage("run -- primitive"); 144 145 r.shutdown(); 146 147 assertLoggedMessage("registryDidShutdown -- primitive"); 148 } 149 150 154 private Registry createModuleWithShutdownListener(final String serviceModel) 155 { 156 ModuleDefinitionImpl module = createModuleDefinition("module1"); 157 158 ServicePointDefinitionImpl sp1 = createServicePointDefinition(module, "Listener", Runnable .class); 159 160 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(newLocation()) { 161 162 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 163 { 164 RunnableImpl result = new RunnableImpl(); 165 result.setType(serviceModel); 166 return result; 167 }}; 168 169 ImplementationDefinition impl = new ImplementationDefinitionImpl(module, newLocation(), 170 constructor, serviceModel, true); 171 sp1.addImplementation(impl); 172 module.addServicePoint(sp1); 173 return buildFrameworkRegistry(module); 174 } 175 176 } | Popular Tags |