1 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 ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.List ; 33 34 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 .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 .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 serviceIds = r.getServiceIds( ResultSet .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 (), new HashSet ( r.getServiceIds( ResultSet .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 .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 .class, Visibility.PUBLIC ) ); 116 r.addServicePoint( createServicePoint(moduleDefinition, module1, "bar", ResultSet .class, Visibility.PUBLIC ) ); 117 r.addServicePoint( createServicePoint(moduleDefinition, module1, "baz", ResultSet .class, Visibility.PRIVATE ) ); 118 r.addServicePoint( createServicePoint(moduleDefinition, module1, "string", String .class, Visibility.PUBLIC ) ); 119 assertEquals( new HashSet ( Arrays.asList( new String []{"module1.foo", "module1.bar"} ) ), new HashSet ( r.getServiceIds( ResultSet .class ) ) ); 120 assertEquals( new HashSet ( Arrays.asList( new String []{"module1.string"} ) ), new HashSet ( r.getServiceIds( String .class ) ) ); 121 List 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 id, Class 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 id, String 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 146 public void testDoubleStartup() 147 { 148 MockControl spc = newControl( ServicePoint.class ); 149 ServicePoint sp = ( ServicePoint ) spc.getMock(); 150 Runnable service = ( Runnable ) newMock( Runnable .class ); 151 152 sp.getExtensionPointId(); 154 spc.setReturnValue( "hivemind.Startup" ); 155 sp.getServiceInterfaceClassName(); 156 spc.setReturnValue( Runnable .class.getName() ); 157 sp.visibleToModule( null ); 158 spc.setReturnValue( true ); 159 sp.getService( Runnable .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 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 cp.getExtensionPointId(); 185 cpc.setReturnValue( "hivemind.ServiceModels" ); 186 cp.getConfigurationType(); 187 cpc.setReturnValue(List .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 |