1 15 package org.apache.hivemind.impl; 16 17 import hivemind.test.services.SimpleService; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.List ; 22 23 import org.apache.hivemind.ErrorLog; 24 import org.apache.hivemind.Location; 25 import org.apache.hivemind.Registry; 26 import org.apache.hivemind.ServiceImplementationFactory; 27 import org.apache.hivemind.definition.ImplementationConstructionContext; 28 import org.apache.hivemind.definition.ModuleDefinition; 29 import org.apache.hivemind.definition.Occurances; 30 import org.apache.hivemind.internal.ImplementationConstructionContextImpl; 31 import org.apache.hivemind.internal.Module; 32 import org.apache.hivemind.internal.ServicePoint; 33 import org.apache.hivemind.schema.impl.SchemaImpl; 34 import org.apache.hivemind.xml.XmlTestCase; 35 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl; 36 import org.easymock.MockControl; 37 38 43 public class TestInvokeFactoryServiceConstructor extends XmlTestCase 44 { 45 public void testWrongNumberOfParameters() 46 { 47 MockControl moduleControl = newControl(Module.class); 48 Module module = (Module) moduleControl.getMock(); 49 50 MockControl factoryPointControl = newControl(ServicePoint.class); 51 ServicePoint factoryPoint = (ServicePoint) factoryPointControl.getMock(); 52 53 MockControl factoryControl = newControl(ServiceImplementationFactory.class); 54 ServiceImplementationFactory factory = (ServiceImplementationFactory) factoryControl 55 .getMock(); 56 57 MockControl pointControl = newControl(ServicePoint.class); 58 ServicePoint point = (ServicePoint) pointControl.getMock(); 59 60 SchemaImpl schema = new SchemaImpl("module"); 61 schema.setRootElementClassName(ArrayList .class.getName()); 62 63 ModuleDefinition md = createModuleDefinition("test"); 64 XmlServicePointDefinitionImpl xmlSpd = new XmlServicePointDefinitionImpl(md); 65 xmlSpd.setParametersCount(Occurances.REQUIRED); 66 xmlSpd.setParametersSchema(schema); 67 68 Location location = newLocation(); 69 InvokeFactoryServiceConstructor c = new InvokeFactoryServiceConstructor(location, "module"); 70 71 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 72 73 75 point.getErrorLog(); 76 pointControl.setReturnValue(log); 77 78 module.getServicePoint("foo.bar.Baz"); 79 moduleControl.setReturnValue(factoryPoint); 80 81 module.resolveType(ArrayList .class.getName()); 82 moduleControl.setReturnValue(ArrayList .class); 83 84 factoryPoint.getService(ServiceImplementationFactory.class); 85 factoryPointControl.setReturnValue(factory); 86 87 factoryPoint.getServicePointDefinition(); 88 factoryPointControl.setReturnValue(xmlSpd); 89 90 factoryPoint.getModule(); 91 factoryPointControl.setReturnValue(module); 92 93 String message = XmlImplMessages 94 .wrongNumberOfParameters("foo.bar.Baz", 0, Occurances.REQUIRED); 95 96 log.error(message, location, null); 97 98 factory.createCoreServiceImplementation(new ServiceImplementationFactoryParametersImpl( 99 point, module, Collections.EMPTY_LIST)); 100 factoryControl.setReturnValue("THE SERVICE"); 101 102 replayControls(); 103 104 c.setFactoryServiceId("foo.bar.Baz"); 105 c.setParameters(Collections.EMPTY_LIST); 106 107 ImplementationConstructionContext context = new ImplementationConstructionContextImpl(module, point); 108 assertEquals("THE SERVICE", c.constructCoreServiceImplementation(context)); 109 110 verifyControls(); 111 } 112 113 public void testInvokeFactoryServiceConstructorAccessors() 114 { 115 String moduleId = "module"; 116 List p = new ArrayList (); 117 InvokeFactoryServiceConstructor c = new InvokeFactoryServiceConstructor(newLocation(), moduleId); 118 119 c.setParameters(p); 120 121 assertSame(p, c.getParameters()); 122 } 123 124 public void testComplex() throws Exception 125 { 126 Registry r = buildFrameworkRegistry("ComplexModule.xml"); 127 128 SimpleService s = 129 (SimpleService) r.getService("hivemind.test.services.Simple", SimpleService.class); 130 CountFactory.reset(); 131 132 assertEquals( 133 "<SingletonProxy for hivemind.test.services.Simple(hivemind.test.services.SimpleService)>", 134 s.toString()); 135 136 assertEquals(7, s.add(4, 3)); 137 138 assertEquals(1, CountFactory.getCount()); 139 140 assertEquals(19, s.add(11, 8)); 141 142 assertEquals(2, CountFactory.getCount()); 143 } 144 145 } | Popular Tags |