1 15 package org.apache.hivemind.lib.strategy; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.Collections ; 19 import java.util.List ; 20 21 import org.apache.hivemind.ErrorLog; 22 import org.apache.hivemind.Location; 23 import org.apache.hivemind.Registry; 24 import org.apache.hivemind.ServiceImplementationFactoryParameters; 25 import org.apache.hivemind.lib.util.StrategyRegistry; 26 import org.apache.hivemind.service.ClassFab; 27 import org.apache.hivemind.service.ClassFabUtils; 28 import org.apache.hivemind.service.ClassFactory; 29 import org.apache.hivemind.service.MethodFab; 30 import org.apache.hivemind.service.MethodSignature; 31 import org.apache.hivemind.test.AggregateArgumentsMatcher; 32 import org.apache.hivemind.test.ArgumentMatcher; 33 import org.apache.hivemind.test.ArrayMatcher; 34 import org.apache.hivemind.test.TypeMatcher; 35 import org.apache.hivemind.xml.XmlTestCase; 36 import org.easymock.MockControl; 37 38 45 public class TestStrategyFactory extends XmlTestCase 46 { 47 private List buildContributions(Class registerClass, Object adapter, Location location) 48 { 49 StrategyContribution c = new StrategyContribution(); 50 51 c.setRegisterClass(registerClass); 52 c.setStrategy(adapter); 53 c.setLocation(location); 54 55 return Collections.singletonList(c); 56 } 57 58 private StrategyParameter buildParameter(Class registerClass, Object adapter, 59 Location contributionLocation, Location parameterLocation) 60 { 61 StrategyParameter result = new StrategyParameter(); 62 63 result.setContributions(buildContributions(registerClass, adapter, contributionLocation)); 64 result.setLocation(parameterLocation); 65 66 return result; 67 } 68 69 private StrategyParameter buildParameter(Class registerClass, Object adapter) 70 { 71 return buildParameter(registerClass, adapter, null, null); 72 } 73 74 public void testBuildRegistry() 75 { 76 StrategyRegistry ar = (StrategyRegistry) newMock(StrategyRegistry.class); 77 ToStringStrategy adapter = (ToStringStrategy) newMock(ToStringStrategy.class); 78 79 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 80 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 81 .getMock(); 82 83 fp.getServiceInterface(); 84 fpc.setReturnValue(ToStringStrategy.class); 85 86 StrategyParameter p = buildParameter(Number .class, adapter); 87 88 fp.getFirstParameter(); 89 fpc.setReturnValue(p); 90 91 ar.register(Number .class, adapter); 92 93 replayControls(); 94 95 new StrategyFactory().buildRegistry(fp, ar); 96 97 verifyControls(); 98 } 99 100 public void testBuildRegistryWrongAdapterType() 101 { 102 Location l = newLocation(); 103 104 StrategyRegistry ar = (StrategyRegistry) newMock(StrategyRegistry.class); 105 ToStringStrategy adapter = (ToStringStrategy) newMock(ToStringStrategy.class); 106 107 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 108 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 109 .getMock(); 110 111 MockControl logc = newControl(ErrorLog.class); 112 ErrorLog log = (ErrorLog) logc.getMock(); 113 114 fp.getServiceInterface(); 115 fpc.setReturnValue(Runnable .class); 116 117 StrategyParameter p = buildParameter(Number .class, adapter, l, null); 118 119 fp.getFirstParameter(); 120 fpc.setReturnValue(p); 121 122 fp.getErrorLog(); 123 fpc.setReturnValue(log); 124 125 log.error( 126 StrategyMessages.strategyWrongInterface(adapter, Number .class, Runnable .class), 127 l, 128 new ClassCastException ()); 129 logc.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[] 130 { null, null, new TypeMatcher() })); 131 132 replayControls(); 133 134 new StrategyFactory().buildRegistry(fp, ar); 135 136 verifyControls(); 137 } 138 139 public void testBuildImplementationClass() 140 { 141 MockControl factoryControl = newControl(ClassFactory.class); 142 ClassFactory factory = (ClassFactory) factoryControl.getMock(); 143 144 MockControl cfc = newControl(ClassFab.class); 145 ClassFab cf = (ClassFab) cfc.getMock(); 146 147 MethodFab mf = (MethodFab) newMock(MethodFab.class); 148 149 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 150 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 151 .getMock(); 152 153 fp.getServiceInterface(); 154 fpc.setReturnValue(ToStringStrategy.class); 155 156 factory.newClass("NewClass", Object .class); 157 factoryControl.setReturnValue(cf); 158 159 cf.addInterface(ToStringStrategy.class); 160 cf.addField("_registry", StrategyRegistry.class); 161 162 cf.addConstructor(new Class [] 163 { StrategyRegistry.class }, null, "_registry = $1;"); 164 cfc.setMatcher(new AggregateArgumentsMatcher(new ArrayMatcher())); 165 166 cf 167 .addMethod( 168 Modifier.PRIVATE, 169 new MethodSignature(ToStringStrategy.class, "_getStrategy", new Class [] 170 { Object .class }, null), 171 "return (org.apache.hivemind.lib.strategy.ToStringStrategy) _registry.getStrategy($1.getClass());"); 172 cfc.setReturnValue(mf); 173 174 cf.addMethod(Modifier.PUBLIC, new MethodSignature(String .class, "toString", new Class [] 175 { Object .class }, null), "return ($r) _getStrategy($1).toString($$);"); 176 cfc.setReturnValue(mf); 177 178 fp.getServiceId(); 179 fpc.setReturnValue("foo.Bar"); 180 181 ClassFabUtils.addToStringMethod(cf, StrategyMessages.toString( 182 "foo.Bar", 183 ToStringStrategy.class)); 184 cfc.setReturnValue(mf); 185 186 cf.createClass(); 187 cfc.setReturnValue(String .class); 188 189 replayControls(); 190 191 StrategyFactory f = new StrategyFactory(); 192 f.setClassFactory(factory); 193 194 f.buildImplementationClass(fp, "NewClass"); 195 196 verifyControls(); 197 } 198 199 public void testBuildImplementationClassImproperMethod() 200 { 201 Location l = newLocation(); 202 203 MockControl factoryControl = newControl(ClassFactory.class); 204 ClassFactory factory = (ClassFactory) factoryControl.getMock(); 205 206 MockControl cfc = newControl(ClassFab.class); 207 ClassFab cf = (ClassFab) cfc.getMock(); 208 209 MethodFab mf = (MethodFab) newMock(MethodFab.class); 210 211 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 212 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 213 .getMock(); 214 215 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 216 217 fp.getServiceInterface(); 218 fpc.setReturnValue(Runnable .class); 219 220 factory.newClass("NewClass", Object .class); 221 factoryControl.setReturnValue(cf); 222 223 cf.addInterface(Runnable .class); 224 cf.addField("_registry", StrategyRegistry.class); 225 226 cf.addConstructor(new Class [] 227 { StrategyRegistry.class }, null, "_registry = $1;"); 228 cfc.setMatcher(new AggregateArgumentsMatcher(new ArrayMatcher())); 229 230 cf.addMethod( 231 Modifier.PRIVATE, 232 new MethodSignature(Runnable .class, "_getStrategy", new Class [] 233 { Object .class }, null), 234 "return (java.lang.Runnable) _registry.getStrategy($1.getClass());"); 235 cfc.setReturnValue(mf); 236 237 MethodSignature sig = new MethodSignature(void.class, "run", null, null); 238 239 cf.addMethod(Modifier.PUBLIC, sig, "{ }"); 240 cfc.setReturnValue(mf); 241 242 fp.getErrorLog(); 243 fpc.setReturnValue(log); 244 245 fp.getFirstParameter(); 246 fpc.setReturnValue(l); 249 250 log.error(StrategyMessages.improperServiceMethod(sig), l, null); 251 252 fp.getServiceId(); 253 fpc.setReturnValue("foo.Bar"); 254 255 ClassFabUtils.addToStringMethod(cf, StrategyMessages.toString("foo.Bar", Runnable .class)); 256 cfc.setReturnValue(mf); 257 258 cf.createClass(); 259 260 cfc.setReturnValue(String .class); 261 262 replayControls(); 263 264 StrategyFactory f = new StrategyFactory(); 265 f.setClassFactory(factory); 266 267 f.buildImplementationClass(fp, "NewClass"); 268 269 verifyControls(); 270 } 271 272 public void testIntegration() throws Exception 273 { 274 Registry r = buildFrameworkRegistry("AdapterFactoryIntegration.xml"); 275 276 ToStringStrategy ts = (ToStringStrategy) r.getService(ToStringStrategy.class); 277 278 assertEquals("5150", ts.toString(new Integer (5150))); 279 } 280 } | Popular Tags |