KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > servicemodel > TestRegistryShutdownListenerServices


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.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 /**
30  * @author James Carman
31  * @version 1.0
32  */

33 public class TestRegistryShutdownListenerServices extends FrameworkTestCase
34 {
35     private void executeShutdownListenerTest(String JavaDoc serviceModel, boolean manual) throws Exception JavaDoc
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 JavaDoc
46     {
47         executeShutdownListenerTest(ServiceModel.POOLED, true);
48         executeShutdownListenerTest(ServiceModel.POOLED, false);
49     }
50
51     public void testSingleton() throws Exception JavaDoc
52     {
53         executeShutdownListenerTest(ServiceModel.SINGLETON, true);
54         executeShutdownListenerTest(ServiceModel.SINGLETON, false);
55     }
56
57     public void testPrimitive() throws Exception JavaDoc
58     {
59         executeShutdownListenerTest(ServiceModel.PRIMITIVE, true);
60         executeShutdownListenerTest(ServiceModel.PRIMITIVE, false);
61     }
62     
63     public void testSingletonBeanRegistryShutdownListener() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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     /**
92      * Creates a Registry with one module, that defines a Service "Simple" with an interface and
93      * separate implementation class.
94      * @param manual If true, the service is manually added to the {@link ShutdownCoordinator}
95      */

96     private Registry createRegistryWithSimpleService(final String JavaDoc 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         // Define inline implementation constructor, that passes the ShutdownCoordinator service if manual is true
104
ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(module.getLocation())
105         {
106             public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
107             {
108                 Object JavaDoc 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     /**
125      * Creates a registry with one module, that defines a Service "RegistryShutdownBean" which
126      * is a pojo
127      */

128     private Registry createRegistryWithPojo(final String JavaDoc 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