KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringHolder;
18 import hivemind.test.services.impl.StringHolderImpl;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.ErrorLog;
23 import org.apache.hivemind.Locatable;
24 import org.apache.hivemind.internal.RegistryInfrastructure;
25 import org.apache.hivemind.test.HiveMindTestCase;
26 import org.easymock.MockControl;
27
28 /**
29  * Some additional tests for {@link org.apache.hivemind.impl.ModuleImpl}.
30  *
31  * @author Howard Lewis Ship
32  */

33 public class TestModule extends HiveMindTestCase
34 {
35
36     public void testGetServiceByInterface()
37     {
38         MockControl rc = newControl(RegistryInfrastructure.class);
39         RegistryInfrastructure r = (RegistryInfrastructure) rc.getMock();
40
41         ModuleImpl m = new ModuleImpl();
42         m.setRegistry(r);
43
44         StringHolder h = new StringHolderImpl();
45
46         r.getService(StringHolder.class, m);
47         rc.setReturnValue(h);
48
49         replayControls();
50
51         Object JavaDoc result = m.getService(StringHolder.class);
52
53         assertEquals(h, result);
54
55         verifyControls();
56     }
57
58     public void testResolveType()
59     {
60         ModuleImpl module = new ModuleImpl();
61         module.setPackageName("org.apache.hivemind");
62         module.setClassResolver(getClassResolver());
63
64         assertSame(Locatable.class, module.resolveType("org.apache.hivemind.Locatable"));
65         assertSame(ErrorLog.class, module.resolveType("ErrorLog"));
66         assertSame(BaseLocatable.class, module.resolveType("impl.BaseLocatable"));
67     }
68
69     /**
70      * @since 1.1.1
71      */

72     public void testResolveTypeCache()
73     {
74         Class JavaDoc expected = getClass();
75
76         ClassResolver resolver = newClassResolver();
77
78         trainCheckForClass(resolver, "FooBar", expected);
79
80         replayControls();
81
82         ModuleImpl module = new ModuleImpl();
83         module.setPackageName("org.apache.hivemind");
84         module.setClassResolver(resolver);
85
86         assertSame(expected, module.resolveType("FooBar"));
87
88         // And this time it comes out of the cache
89

90         assertSame(expected, module.resolveType("FooBar"));
91
92         verifyControls();
93     }
94
95     private void trainCheckForClass(ClassResolver resolver, String JavaDoc type, Class JavaDoc returnClass)
96     {
97         resolver.checkForClass(type);
98         setReturnValue(resolver, returnClass);
99     }
100
101     private ClassResolver newClassResolver()
102     {
103         return (ClassResolver) newMock(ClassResolver.class);
104     }
105
106     public void testResolveTypeFailure()
107     {
108         ModuleImpl module = new ModuleImpl();
109         module.setPackageName("org.apache.hivemind.order");
110         module.setClassResolver(getClassResolver());
111
112         try
113         {
114             module.resolveType("Qbert");
115             unreachable();
116         }
117         catch (ApplicationRuntimeException ex)
118         {
119             assertEquals(
120                     "Unable to convert type 'Qbert' to a Java class, either as is, or in package org.apache.hivemind.order.",
121                     ex.getMessage());
122         }
123     }
124 }
Popular Tags