001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package hivemind.test.services;
016:
017: import hivemind.test.FrameworkTestCase;
018:
019: import org.apache.hivemind.ApplicationRuntimeException;
020: import org.apache.hivemind.Registry;
021:
022: /**
023: * Tests shutdown on the registry and on deferred and threaded services.
024: *
025: * @author Howard Lewis Ship
026: */
027: public class TestShutdown extends FrameworkTestCase {
028:
029: public void testShutdownSingleton() throws Exception {
030: Registry r = buildFrameworkRegistry("SimpleModule.xml");
031: SimpleService s = (SimpleService) r.getService(
032: "hivemind.test.services.Simple", SimpleService.class);
033:
034: assertEquals(11, s.add(4, 7));
035:
036: r.shutdown();
037:
038: try {
039: s.add(9, 5);
040: unreachable();
041: } catch (ApplicationRuntimeException ex) {
042: assertExceptionSubstring(ex,
043: "The HiveMind Registry has been shutdown.");
044: }
045: }
046:
047: public void testRegistryShutdownUnrepeatable() throws Exception {
048: Registry r = buildFrameworkRegistry("SimpleModule.xml");
049:
050: r.shutdown();
051:
052: try {
053: r.getConfiguration("foo.bar");
054: unreachable();
055: } catch (ApplicationRuntimeException ex) {
056: assertExceptionSubstring(ex,
057: "The HiveMind Registry has been shutdown.");
058: }
059:
060: try {
061: r.shutdown();
062: unreachable();
063: } catch (ApplicationRuntimeException ex) {
064: assertExceptionSubstring(ex,
065: "The HiveMind Registry has been shutdown.");
066: }
067: }
068:
069: public void testShutdownThreaded() throws Exception {
070: Registry r = buildFrameworkRegistry("StringHolder.xml");
071:
072: StringHolder h = (StringHolder) r.getService(
073: "hivemind.test.services.StringHolder",
074: StringHolder.class);
075:
076: assertNull(h.getValue());
077:
078: h.setValue("fred");
079:
080: assertEquals("fred", h.getValue());
081:
082: r.shutdown();
083:
084: try {
085: h.getValue();
086: unreachable();
087: } catch (ApplicationRuntimeException ex) {
088: assertExceptionSubstring(ex,
089: "The HiveMind Registry has been shutdown.");
090: }
091: }
092:
093: public void testSingletonCore() throws Exception {
094: Registry r = buildFrameworkRegistry("Shutdown.xml");
095:
096: Runnable s = (Runnable) r.getService(
097: "hivemind.test.services.Singleton", Runnable.class);
098:
099: interceptLogging("hivemind.test.services.Singleton");
100:
101: s.run();
102:
103: assertLoggedMessage("run -- Singleton");
104:
105: r.shutdown();
106:
107: assertLoggedMessage("registryDidShutdown -- Singleton");
108: }
109:
110: public void testPrimitiveCore() throws Exception {
111: Registry r = buildFrameworkRegistry("Shutdown.xml");
112:
113: Runnable s = (Runnable) r.getService(
114: "hivemind.test.services.Primitive", Runnable.class);
115:
116: interceptLogging("hivemind.test.services.Primitive");
117:
118: s.run();
119:
120: assertLoggedMessage("run -- Primitive");
121:
122: r.shutdown();
123:
124: assertLoggedMessage("registryDidShutdown -- Primitive");
125: }
126:
127: }
|