KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > TestInvokeFactoryServiceConstructor


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;
16
17 import hivemind.test.services.SimpleService;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.ErrorLog;
24 import org.apache.hivemind.Location;
25 import org.apache.hivemind.Registry;
26 import org.apache.hivemind.ServiceImplementationFactory;
27 import org.apache.hivemind.definition.ImplementationConstructionContext;
28 import org.apache.hivemind.definition.ModuleDefinition;
29 import org.apache.hivemind.definition.Occurances;
30 import org.apache.hivemind.internal.ImplementationConstructionContextImpl;
31 import org.apache.hivemind.internal.Module;
32 import org.apache.hivemind.internal.ServicePoint;
33 import org.apache.hivemind.schema.impl.SchemaImpl;
34 import org.apache.hivemind.xml.XmlTestCase;
35 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl;
36 import org.easymock.MockControl;
37
38 /**
39  * Tests some error conditions related to invoking a service factory.
40  *
41  * @author Howard Lewis Ship
42  */

43 public class TestInvokeFactoryServiceConstructor extends XmlTestCase
44 {
45     public void testWrongNumberOfParameters()
46     {
47         MockControl moduleControl = newControl(Module.class);
48         Module module = (Module) moduleControl.getMock();
49
50         MockControl factoryPointControl = newControl(ServicePoint.class);
51         ServicePoint factoryPoint = (ServicePoint) factoryPointControl.getMock();
52
53         MockControl factoryControl = newControl(ServiceImplementationFactory.class);
54         ServiceImplementationFactory factory = (ServiceImplementationFactory) factoryControl
55                 .getMock();
56
57         MockControl pointControl = newControl(ServicePoint.class);
58         ServicePoint point = (ServicePoint) pointControl.getMock();
59         
60         SchemaImpl schema = new SchemaImpl("module");
61         schema.setRootElementClassName(ArrayList JavaDoc.class.getName());
62         
63         ModuleDefinition md = createModuleDefinition("test");
64         XmlServicePointDefinitionImpl xmlSpd = new XmlServicePointDefinitionImpl(md);
65         xmlSpd.setParametersCount(Occurances.REQUIRED);
66         xmlSpd.setParametersSchema(schema);
67         
68         Location location = newLocation();
69         InvokeFactoryServiceConstructor c = new InvokeFactoryServiceConstructor(location, "module");
70
71         ErrorLog log = (ErrorLog) newMock(ErrorLog.class);
72
73         // Training !
74

75         point.getErrorLog();
76         pointControl.setReturnValue(log);
77
78         module.getServicePoint("foo.bar.Baz");
79         moduleControl.setReturnValue(factoryPoint);
80         
81         module.resolveType(ArrayList JavaDoc.class.getName());
82         moduleControl.setReturnValue(ArrayList JavaDoc.class);
83
84         factoryPoint.getService(ServiceImplementationFactory.class);
85         factoryPointControl.setReturnValue(factory);
86
87         factoryPoint.getServicePointDefinition();
88         factoryPointControl.setReturnValue(xmlSpd);
89         
90         factoryPoint.getModule();
91         factoryPointControl.setReturnValue(module);
92
93         String JavaDoc message = XmlImplMessages
94                 .wrongNumberOfParameters("foo.bar.Baz", 0, Occurances.REQUIRED);
95
96         log.error(message, location, null);
97
98         factory.createCoreServiceImplementation(new ServiceImplementationFactoryParametersImpl(
99                 point, module, Collections.EMPTY_LIST));
100         factoryControl.setReturnValue("THE SERVICE");
101
102         replayControls();
103
104         c.setFactoryServiceId("foo.bar.Baz");
105         c.setParameters(Collections.EMPTY_LIST);
106
107         ImplementationConstructionContext context = new ImplementationConstructionContextImpl(module, point);
108         assertEquals("THE SERVICE", c.constructCoreServiceImplementation(context));
109
110         verifyControls();
111     }
112
113     public void testInvokeFactoryServiceConstructorAccessors()
114     {
115         String JavaDoc moduleId = "module";
116         List JavaDoc p = new ArrayList JavaDoc();
117         InvokeFactoryServiceConstructor c = new InvokeFactoryServiceConstructor(newLocation(), moduleId);
118
119         c.setParameters(p);
120
121         assertSame(p, c.getParameters());
122     }
123     
124     public void testComplex() throws Exception JavaDoc
125     {
126         Registry r = buildFrameworkRegistry("ComplexModule.xml");
127
128         SimpleService s =
129             (SimpleService) r.getService("hivemind.test.services.Simple", SimpleService.class);
130         CountFactory.reset();
131
132         assertEquals(
133             "<SingletonProxy for hivemind.test.services.Simple(hivemind.test.services.SimpleService)>",
134             s.toString());
135
136         assertEquals(7, s.add(4, 3));
137
138         assertEquals(1, CountFactory.getCount());
139
140         assertEquals(19, s.add(11, 8));
141
142         assertEquals(2, CountFactory.getCount());
143     }
144     
145 }
Popular Tags