KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > TestShutdownCoordinator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.util;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Registry;
19 import org.apache.hivemind.ShutdownCoordinator;
20 import org.apache.hivemind.events.RegistryShutdownListener;
21 import org.apache.hivemind.impl.RegistryBuilder;
22 import org.apache.hivemind.impl.ShutdownCoordinatorImpl;
23
24 import hivemind.test.FrameworkTestCase;
25
26 /**
27  * Tests the {@link org.apache.hivemind.impl.ShutdownCoordinator}.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TestShutdownCoordinator extends FrameworkTestCase
32 {
33     private static class Fixture implements RegistryShutdownListener
34     {
35         private boolean _shutdown;
36
37         public boolean isShutdown()
38         {
39             return _shutdown;
40         }
41
42         public void registryDidShutdown()
43         {
44             _shutdown = true;
45         }
46
47     }
48
49     public void testShutdownCoordinator()
50     {
51         ShutdownCoordinator c = new ShutdownCoordinatorImpl();
52
53         Fixture f = new Fixture();
54
55         c.addRegistryShutdownListener(f);
56
57         c.shutdown();
58
59         assertEquals(true, f.isShutdown());
60
61         // For good riddens; test no failure if already down.
62

63         c.shutdown();
64     }
65
66     public void testShutdownCoordinatorService()
67     {
68         Registry r = RegistryBuilder.constructDefaultRegistry();
69
70         ShutdownCoordinator c =
71             (ShutdownCoordinator) r.getService(
72                 "hivemind.ShutdownCoordinator",
73                 ShutdownCoordinator.class);
74
75         Fixture f = new Fixture();
76
77         c.addRegistryShutdownListener(f);
78
79         c.shutdown();
80
81         assertEquals(true, f.isShutdown());
82     }
83
84     public void testShutdownFailure() throws Exception JavaDoc
85     {
86         ShutdownCoordinator c = new ShutdownCoordinatorImpl();
87
88         c.addRegistryShutdownListener(new RegistryShutdownListener()
89         {
90             public void registryDidShutdown()
91             {
92                 throw new ApplicationRuntimeException("I'm just not in the mood.");
93             }
94         });
95
96         interceptLogging();
97
98         c.shutdown();
99
100         assertLoggedMessagePattern("Unable to shutdown .*: I'm just not in the mood\\.");
101     }
102
103 }
104
Popular Tags