KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.definition.ModuleDefinition;
19 import org.apache.hivemind.definition.Visibility;
20 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl;
21 import org.apache.hivemind.internal.ConfigurationPoint;
22 import org.apache.hivemind.internal.RegistryInfrastructure;
23 import org.apache.hivemind.internal.ServicePoint;
24 import org.easymock.MockControl;
25
26 import hivemind.test.FrameworkTestCase;
27
28 import java.sql.ResultSet JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Additional tests for {@link RegistryInfrastructureImpl}. Much of the
36  * existing testing is, alas, integration (not unit) testing. Gradually hope to move more of that
37  * into this class.
38  *
39  * @author Howard Lewis Ship
40  * @since 1.1
41  */

42 public class TestRegistryInfrastructure extends FrameworkTestCase
43 {
44     public void testGetMissingExtensionPoint()
45     {
46         RegistryInfrastructure r = new RegistryInfrastructureImpl( null, null );
47         try
48         {
49             r.getServicePoint( "foo", null );
50             unreachable();
51         }
52         catch( ApplicationRuntimeException ex )
53         {
54             assertEquals( ImplMessages.noSuchServicePoint( "foo" ), ex.getMessage() );
55         }
56     }
57
58     public void testGetUnqualifiedServicePoint()
59     {
60         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl( null, null );
61         ModuleDefinition moduleDefinition = createModuleDefinition("module1");
62         final ModuleImpl module1 = new ModuleImpl();
63         module1.setModuleId( "module1" );
64         r.addServicePoint( createServicePoint(moduleDefinition, module1, "foo", ResultSet JavaDoc.class, Visibility.PUBLIC ) );
65         try
66         {
67             r.getServicePoint( "foo", null );
68             unreachable();
69         }
70         catch( ApplicationRuntimeException ex )
71         {
72             assertEquals( ImplMessages.unqualifiedServicePoint( "foo", "\"module1.foo\"" ), ex.getMessage() );
73         }
74         final ModuleImpl module2 = new ModuleImpl();
75         module2.setModuleId( "module2" );
76         r.addServicePoint( createServicePoint(moduleDefinition, module2, "foo", ResultSet JavaDoc.class, Visibility.PUBLIC ) );
77         try
78         {
79             r.getServicePoint( "foo", null );
80             unreachable();
81         }
82         catch( ApplicationRuntimeException ex )
83         {
84             assertEquals( ImplMessages.unqualifiedServicePoint( "foo", "\"module1.foo\", \"module2.foo\"" ), ex.getMessage() );
85         }
86     }
87
88     public void testGetServiceIdsNoServices()
89     {
90         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl( null, null );
91         final List JavaDoc serviceIds = r.getServiceIds( ResultSet JavaDoc.class );
92         assertNotNull( serviceIds );
93         assertTrue( serviceIds.isEmpty() );
94     }
95
96     public void testGetServiceIdsInterfaceNotFound()
97     {
98         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl( null, null );
99         final ModuleImpl module1 = new ModuleImpl();
100         ModuleDefinition moduleDefinition = createModuleDefinition("module1");
101         module1.setClassResolver( new DefaultClassResolver() );
102         module1.setModuleId( "module1" );
103         r.addServicePoint( createServicePoint(moduleDefinition, module1, "module1.foo", "com.evilsite.some.bogus.package.SomeBogusInterface.", Visibility.PUBLIC ) );
104         assertEquals( new HashSet JavaDoc(), new HashSet JavaDoc( r.getServiceIds( ResultSet JavaDoc.class ) ) );
105     }
106
107     public void testGetServiceIds()
108     {
109         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl( null, null );
110         ModuleDefinition moduleDefinition = createModuleDefinition("module1");
111         assertTrue( r.getServiceIds( ResultSet JavaDoc.class ).isEmpty() );
112         final ModuleImpl module1 = new ModuleImpl();
113         module1.setClassResolver( new DefaultClassResolver() );
114         module1.setModuleId( "module1" );
115         r.addServicePoint( createServicePoint(moduleDefinition, module1, "foo", ResultSet JavaDoc.class, Visibility.PUBLIC ) );
116         r.addServicePoint( createServicePoint(moduleDefinition, module1, "bar", ResultSet JavaDoc.class, Visibility.PUBLIC ) );
117         r.addServicePoint( createServicePoint(moduleDefinition, module1, "baz", ResultSet JavaDoc.class, Visibility.PRIVATE ) );
118         r.addServicePoint( createServicePoint(moduleDefinition, module1, "string", String JavaDoc.class, Visibility.PUBLIC ) );
119         assertEquals( new HashSet JavaDoc( Arrays.asList( new String JavaDoc[]{"module1.foo", "module1.bar"} ) ), new HashSet JavaDoc( r.getServiceIds( ResultSet JavaDoc.class ) ) );
120         assertEquals( new HashSet JavaDoc( Arrays.asList( new String JavaDoc[]{"module1.string"} ) ), new HashSet JavaDoc( r.getServiceIds( String JavaDoc.class ) ) );
121         List JavaDoc serviceIds = r.getServiceIds( null );
122         assertNotNull( serviceIds );
123         assertEquals( 0, serviceIds.size() );
124
125     }
126
127     private ServicePointImpl createServicePoint(ModuleDefinition moduleDefinition, final ModuleImpl module, String JavaDoc id, Class JavaDoc serviceInterface, Visibility visibility)
128     {
129         ServicePointDefinitionImpl definition = new ServicePointDefinitionImpl(moduleDefinition, id, newLocation(), visibility, serviceInterface.getName());
130         final ServicePointImpl servicePoint2 = new ServicePointImpl(module, definition);
131         return servicePoint2;
132     }
133
134     private ServicePointImpl createServicePoint(ModuleDefinition moduleDefinition, final ModuleImpl module, String JavaDoc id, String JavaDoc serviceInterfaceName, Visibility visibility )
135     {
136         ServicePointDefinitionImpl definition = new ServicePointDefinitionImpl(moduleDefinition, id, newLocation(), visibility, serviceInterfaceName);
137         final ServicePointImpl servicePoint2 = new ServicePointImpl(module, definition);
138         return servicePoint2;
139     }
140
141     /**
142      * Ensure that the Registry "locks down" after being started up.
143      *
144      * @since 1.1
145      */

146     public void testDoubleStartup()
147     {
148         MockControl spc = newControl( ServicePoint.class );
149         ServicePoint sp = ( ServicePoint ) spc.getMock();
150         Runnable JavaDoc service = ( Runnable JavaDoc ) newMock( Runnable JavaDoc.class );
151
152         // Training
153
sp.getExtensionPointId();
154         spc.setReturnValue( "hivemind.Startup" );
155         sp.getServiceInterfaceClassName();
156         spc.setReturnValue( Runnable JavaDoc.class.getName() );
157         sp.visibleToModule( null );
158         spc.setReturnValue( true );
159         sp.getService( Runnable JavaDoc.class );
160         spc.setReturnValue( service );
161         service.run();
162         replayControls();
163         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl( null, null );
164         r.addServicePoint( sp );
165         r.startup();
166         verifyControls();
167         try
168         {
169             r.startup();
170             unreachable();
171         }
172         catch( IllegalStateException JavaDoc ex )
173         {
174             assertEquals( ImplMessages.registryAlreadyStarted(), ex.getMessage() );
175         }
176     }
177
178     public void testUnknownServiceModelFactory()
179     {
180         MockControl cpc = newControl( ConfigurationPoint.class );
181         ConfigurationPoint cp = ( ConfigurationPoint ) cpc.getMock();
182
183         // Training
184
cp.getExtensionPointId();
185         cpc.setReturnValue( "hivemind.ServiceModels" );
186         cp.getConfigurationType();
187         cpc.setReturnValue(List JavaDoc.class);
188         cp.visibleToModule( null );
189         cpc.setReturnValue( true );
190         cp.getConfiguration();
191         cpc.setReturnValue( Collections.EMPTY_MAP );
192         replayControls();
193         RegistryInfrastructureImpl r = new RegistryInfrastructureImpl( null, null );
194         r.addConfigurationPoint( cp );
195         try
196         {
197             r.getServiceModelFactory( "unknown" );
198             unreachable();
199         }
200         catch( ApplicationRuntimeException ex )
201         {
202             assertEquals( ImplMessages.unknownServiceModel( "unknown" ), ex.getMessage() );
203         }
204         verifyControls();
205     }
206
207 }
Popular Tags