KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > TestEagerLoader


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 hivemind.test.services;
16
17 import hivemind.test.FrameworkTestCase;
18 import hivemind.test.services.impl.LoudRunner;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.Registry;
24 import org.apache.hivemind.definition.Contribution;
25 import org.apache.hivemind.definition.ContributionContext;
26 import org.apache.hivemind.definition.ServicePointDefinition;
27 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper;
28 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl;
29 import org.apache.hivemind.internal.ServiceModel;
30 import org.apache.hivemind.internal.ServicePoint;
31 import org.apache.hivemind.service.impl.EagerLoader;
32
33 /**
34  * Tests for the {@link org.apache.hivemind.service.impl.EagerLoader} service.
35  *
36  * @author Howard Lewis Ship
37  */

38 public class TestEagerLoader extends FrameworkTestCase
39 {
40     public void testEagerLoaderImpl()
41     {
42         EagerLoader el = new EagerLoader();
43         List JavaDoc l = new ArrayList JavaDoc();
44
45         ServicePoint sp = (ServicePoint) newMock(ServicePoint.class);
46
47         sp.forceServiceInstantiation();
48    
49         replayControls();
50
51         l.add(sp);
52
53         el.setServicePoints(l);
54
55         el.run();
56
57         verifyControls();
58     }
59
60     public void testEagerLoadSingleton() throws Exception JavaDoc
61     {
62         interceptLogging("hivemind.test.services.Loud");
63
64         createEagerLoadModule(ServiceModel.SINGLETON);
65
66         assertLoggedMessage("Instantiated.");
67     }
68
69     public void testEagerLoadPrimitive() throws Exception JavaDoc
70     {
71         interceptLogging("hivemind.test.services.Loud");
72
73         createEagerLoadModule(ServiceModel.PRIMITIVE);
74
75         assertLoggedMessage("Instantiated.");
76     }
77
78     public void testEagerLoadThreaded() throws Exception JavaDoc
79     {
80         interceptLogging("hivemind.test.services.Loud");
81
82         createEagerLoadModule(ServiceModel.THREADED);
83
84         assertLoggedMessage("Instantiated.");
85     }
86
87     public void testEagerLoadPooled() throws Exception JavaDoc
88     {
89         interceptLogging("hivemind.test.services.Loud");
90
91         createEagerLoadModule(ServiceModel.POOLED);
92
93         assertLoggedMessage("Instantiated.");
94     }
95     
96     /**
97      * Creates a Registry with one module, that defines a Service "Loud" added to the EagerLoad
98      * configuration.
99      */

100     private Registry createEagerLoadModule(final String JavaDoc serviceModel)
101     {
102         ModuleDefinitionImpl module = createModuleDefinition("hivemind.test.services");
103         ModuleDefinitionHelper helper = new ModuleDefinitionHelper(module);
104
105         ServicePointDefinition sp1 = helper.addServicePoint("Loud", Runnable JavaDoc.class.getName());
106         helper.addSimpleServiceImplementation(sp1, LoudRunner.class.getName(), serviceModel);
107
108         // Add service point "Loud" to EagerLoad configuration point
109
helper.addContributionDefinition("hivemind.EagerLoad", new Contribution()
110         {
111             public void contribute(ContributionContext context)
112             {
113                 List JavaDoc contribution = new ArrayList JavaDoc();
114                 contribution.add(context.getDefiningModule().getServicePoint("hivemind.test.services.Loud"));
115                 context.mergeContribution(contribution);
116             }
117         });
118
119         return buildFrameworkRegistry(module);
120     }
121  
122 }
123
Popular Tags