KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > TestObjectProviders


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.service.impl;
16
17 import hivemind.test.services.StringHolder;
18 import hivemind.test.services.impl.StringHolderImpl;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.Location;
25 import org.apache.hivemind.impl.ModuleImpl;
26 import org.apache.hivemind.internal.Module;
27 import org.apache.hivemind.test.HiveMindTestCase;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for several implementations of {@link org.apache.hivemind.service.ObjectProvider}.
32  *
33  * @author Howard Lewis Ship
34  */

35 public class TestObjectProviders extends HiveMindTestCase
36 {
37     private Module newModule()
38     {
39         ModuleImpl result = new ModuleImpl();
40         result.setClassResolver(getClassResolver());
41
42         return result;
43     }
44
45     public void testServiceObjectProvider()
46     {
47         ServiceObjectProvider p = new ServiceObjectProvider();
48
49         String JavaDoc expected = "EXPECTED RESULT";
50
51         MockControl mc = newControl(Module.class);
52         Module m = (Module) mc.getMock();
53
54         m.getService("fred", Object JavaDoc.class);
55         mc.setReturnValue(expected);
56
57         replayControls();
58
59         Object JavaDoc actual = p.provideObject(m, Location.class, "fred", null);
60
61         assertSame(expected, actual);
62
63         assertNull(p.provideObject(m, Location.class, "", null));
64
65         verifyControls();
66     }
67
68     public void testConfigurationObjectProvider()
69     {
70         ConfigurationObjectProvider p = new ConfigurationObjectProvider();
71
72         List JavaDoc expectedList = new ArrayList JavaDoc();
73
74         MockControl mc = newControl(Module.class);
75         Module m = (Module) mc.getMock();
76
77         m.getConfiguration("barney");
78         mc.setReturnValue(expectedList);
79
80         replayControls();
81
82         Object JavaDoc actual = p.provideObject(m, List JavaDoc.class, "barney", null);
83
84         assertSame(expectedList, actual);
85
86         verifyControls();
87     }
88
89     public void testInstanceProvider()
90     {
91         ObjectInstanceObjectProvider p = new ObjectInstanceObjectProvider();
92
93         Module m = newModule();
94
95         Object JavaDoc actual = p.provideObject(m, List JavaDoc.class, "java.util.ArrayList", null);
96
97         assertTrue(actual.getClass().equals(ArrayList JavaDoc.class));
98
99     }
100
101     public void testInstanceProviderFailure()
102     {
103         ObjectInstanceObjectProvider p = new ObjectInstanceObjectProvider();
104         Module m = newModule();
105
106         try
107         {
108             p.provideObject(m, List JavaDoc.class, "java.util.List", null);
109             unreachable();
110         }
111         catch (ApplicationRuntimeException ex)
112         {
113             assertExceptionSubstring(ex, "Unable to instantiate instance of class java.util.List");
114         }
115     }
116
117     public void testServicePropertyObjectProvider()
118     {
119         MockControl mc = newControl(Module.class);
120         Module m = (Module) mc.getMock();
121
122         StringHolder h = new StringHolderImpl();
123
124         h.setValue("abracadabra");
125
126         m.getService("MyService", Object JavaDoc.class);
127         mc.setReturnValue(h);
128
129         replayControls();
130
131         ServicePropertyObjectProvider p = new ServicePropertyObjectProvider();
132
133         Object JavaDoc result = p.provideObject(m, String JavaDoc.class, "MyService:value", null);
134
135         assertEquals(h.getValue(), result);
136
137         verifyControls();
138     }
139
140     public void testServicePropertyObjectProviderWithInvalidLocator()
141     {
142         ServicePropertyObjectProvider p = new ServicePropertyObjectProvider();
143         Location l = newLocation();
144
145         try
146         {
147             p.provideObject(null, null, "MyService", l);
148             unreachable();
149         }
150         catch (ApplicationRuntimeException ex)
151         {
152             assertExceptionSubstring(ex, ServiceMessages.invalidServicePropertyLocator("MyService"));
153             assertSame(l, ex.getLocation());
154         }
155     }
156
157     public void testClassProvider()
158     {
159         MockControl control = newControl(Module.class);
160         Module module = (Module) control.getMock();
161
162         module.resolveType("List");
163         control.setReturnValue(List JavaDoc.class);
164
165         replayControls();
166
167         assertSame(List JavaDoc.class, new ClassObjectProvider().provideObject(
168                 module,
169                 Class JavaDoc.class,
170                 "List",
171                 null));
172
173         verifyControls();
174     }
175
176     // TODO: Integration test that proves the XML is valid.
177
}
Popular Tags