KickJava   Java API By Example, From Geeks To Geeks.

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


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.FrameworkTestCase;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.hivemind.ApplicationRuntimeException;
25 import org.apache.hivemind.Location;
26 import org.apache.hivemind.definition.ModuleDefinition;
27 import org.apache.hivemind.definition.Visibility;
28 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl;
29 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl;
30 import org.apache.hivemind.internal.Module;
31 import org.apache.hivemind.internal.ServiceModel;
32 import org.apache.hivemind.internal.ServiceModelFactory;
33 import org.easymock.MockControl;
34
35 /**
36  * Test for {@link org.apache.hivemind.impl.ServicePointImpl}. Much of the testing is done using
37  * integration style tests, this just fills in some important gaps.
38  *
39  * @author Howard M. Lewis Ship
40  * @since 1.1
41  */

42 public class TestServicePoint extends FrameworkTestCase
43 {
44     private Module newModule()
45     {
46         ModuleImpl result = new ModuleImpl();
47         result.setModuleId("test");
48         result.setClassResolver(getClassResolver());
49         result.setPackageName("");
50         result.setRegistry(new RegistryInfrastructureImpl(new StrictErrorHandler(), Locale
51                 .getDefault()));
52         return result;
53     }
54
55     public void testUnknownInterfaceClass()
56     {
57         Location l = newLocation();
58         Module module = newModule();
59
60         replayControls();
61
62         ModuleDefinition moduleDefinition = createModuleDefinition("module");
63         ServicePointDefinitionImpl definition = new ServicePointDefinitionImpl(moduleDefinition, "zip.zap", l, Visibility.PUBLIC, "foo.bar.Baz");
64         ServicePointImpl sp = new ServicePointImpl(module, definition);
65
66         try
67         {
68             sp.getServiceInterface();
69             unreachable();
70         }
71         catch (ApplicationRuntimeException ex)
72         {
73             assertEquals("Unable to find interface foo.bar.Baz (for service test.zip.zap).", ex
74                     .getMessage());
75             assertSame(l, ex.getLocation());
76         }
77
78         verifyControls();
79     }
80
81     public void testResultNotAssignableToServiceInterface()
82     {
83         Location l = newLocation();
84
85         MockControl modulec = newControl(Module.class);
86         Module module = (Module) modulec.getMock();
87
88         ModuleDefinition moduleDef = createModuleDefinition("foo");
89         ServicePointDefinitionImpl definition = new ServicePointDefinitionImpl(moduleDef, "bar", l, Visibility.PUBLIC, "java.util.List");
90         definition.addImplementation(new ImplementationDefinitionImpl(moduleDef, l, null, "fred", true));
91         ServicePointImpl sp = new ServicePointImpl(module, definition);
92
93         Object JavaDoc service = new ArrayList JavaDoc();
94
95         MockControl factoryc = newControl(ServiceModelFactory.class);
96         ServiceModelFactory factory = (ServiceModelFactory) factoryc.getMock();
97
98         MockControl modelc = newControl(ServiceModel.class);
99         ServiceModel model = (ServiceModel) modelc.getMock();
100
101         module.getServiceModelFactory("fred");
102         modulec.setReturnValue(factory);
103
104         factory.createServiceModelForService(sp);
105         factoryc.setReturnValue(model);
106
107         model.getService();
108         modelc.setReturnValue(service);
109
110         module.getModuleId();
111         modulec.setReturnValue("test");
112         
113         module.resolveType("java.util.List");
114         modulec.setReturnValue(List JavaDoc.class);
115
116         replayControls();
117
118         try
119         {
120             sp.getService(Map JavaDoc.class);
121             unreachable();
122         }
123         catch (ApplicationRuntimeException ex)
124         {
125             assertEquals(
126                     "Service test.bar does not implement the requested interface (java.util.Map). The declared service interface type is java.util.List.",
127                     ex.getMessage());
128             assertSame(l, ex.getLocation());
129         }
130     }
131 }
Popular Tags