1 15 package org.apache.hivemind.lib.impl; 16 17 import java.lang.reflect.InvocationHandler ; 18 import java.lang.reflect.Method ; 19 import java.lang.reflect.Proxy ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.HiveMind; 23 import org.apache.hivemind.Registry; 24 import org.apache.hivemind.ServiceImplementationFactoryParameters; 25 import org.apache.hivemind.internal.ServicePoint; 26 import org.apache.hivemind.service.impl.ClassFactoryImpl; 27 import org.apache.hivemind.xml.XmlTestCase; 28 import org.easymock.MockControl; 29 30 35 public class TestServicePropertyFactory extends XmlTestCase 36 { 37 private ServicePropertyFactory newFactory() 38 { 39 ServicePropertyFactory result = new ServicePropertyFactory(); 40 41 result.setClassFactory(new ClassFactoryImpl()); 42 43 return result; 44 } 45 46 private ServicePropertyFactoryParameter newParameters(WonkSource service, String propertyName) 47 { 48 ServicePropertyFactoryParameter p = new ServicePropertyFactoryParameter(); 49 50 final MockControl spControl = newControl( ServicePoint.class ); 51 final ServicePoint sp = ( ServicePoint )spControl.getMock(); 52 53 sp.getServiceInterface(); 54 spControl.setReturnValue( WonkSource.class ); 55 sp.getService( WonkSource.class ); 56 spControl.setReturnValue( service ); 57 p.setServicePoint(sp); 58 p.setPropertyName(propertyName); 59 p.setLocation(newLocation()); 60 61 return p; 62 } 63 64 public void testSuccess() 65 { 66 ServicePropertyFactory f = newFactory(); 67 68 MockControl wonkControl = newControl(Wonk.class); 69 Wonk wonk = (Wonk) wonkControl.getMock(); 70 71 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 72 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 73 .getMock(); 74 75 ServicePropertyFactoryParameter parameters = newParameters(new WonkHolder(wonk), "wonk"); 76 77 wonk.wonkVoid(); 78 wonk.wonkString("zebra"); 79 wonkControl.setReturnValue("stripes"); 80 81 fp.getFirstParameter(); 82 fpc.setReturnValue(parameters); 83 84 fp.getServiceInterface(); 85 fpc.setReturnValue(Wonk.class); 86 87 fp.getServiceId(); 88 fpc.setReturnValue("foo.bar"); 89 90 replayControls(); 91 92 Wonk proxy = (Wonk) f.createCoreServiceImplementation(fp); 93 94 proxy.wonkVoid(); 95 assertEquals("stripes", proxy.wonkString("zebra")); 96 97 assertEquals( 98 "<ServicePropertyProxy foo.bar(org.apache.hivemind.lib.impl.Wonk) for property 'wonk' of <WonkHolder>>", 99 proxy.toString()); 100 101 verifyControls(); 102 } 103 104 public void testWithJdkProxies() 105 { 106 ServicePropertyFactory f = newFactory(); 107 108 MockControl wonkControl = newControl(Wonk.class); 109 final Wonk wonk = (Wonk) wonkControl.getMock(); 110 111 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 112 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 113 .getMock(); 114 final WonkHolder delegate = new WonkHolder( wonk ); 115 final WonkSource jdkProxy = ( WonkSource )Proxy.newProxyInstance( WonkSource.class.getClassLoader(), new Class [] { WonkSource.class }, new InvocationHandler () 116 { 117 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 118 return method.invoke( delegate, args ); 119 } 120 121 } ); 122 ServicePropertyFactoryParameter parameters = newParameters(jdkProxy, "wonk"); 123 124 wonk.wonkVoid(); 125 wonk.wonkString("zebra"); 126 wonkControl.setReturnValue("stripes"); 127 128 fp.getFirstParameter(); 129 fpc.setReturnValue(parameters); 130 131 fp.getServiceInterface(); 132 fpc.setReturnValue(Wonk.class); 133 134 fp.getServiceId(); 135 fpc.setReturnValue("foo.bar"); 136 137 replayControls(); 138 139 Wonk proxy = (Wonk) f.createCoreServiceImplementation(fp); 140 141 proxy.wonkVoid(); 142 assertEquals("stripes", proxy.wonkString("zebra")); 143 144 assertEquals( 145 "<ServicePropertyProxy foo.bar(org.apache.hivemind.lib.impl.Wonk) for property 'wonk' of <WonkHolder>>", 146 proxy.toString()); 147 148 verifyControls(); 149 } 150 public void testPropertyNull() 151 { 152 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 153 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 154 .getMock(); 155 156 ServicePropertyFactory f = newFactory(); 157 158 ServicePropertyFactoryParameter parameters = newParameters(new WonkHolder(null), "wonk"); 159 160 fp.getFirstParameter(); 161 fpc.setReturnValue(parameters); 162 163 fp.getServiceInterface(); 164 fpc.setReturnValue(Wonk.class); 165 166 fp.getServiceId(); 167 fpc.setReturnValue("foo.bar"); 168 169 replayControls(); 170 171 Wonk proxy = (Wonk) f.createCoreServiceImplementation(fp); 172 173 try 174 { 175 proxy.wonkVoid(); 176 unreachable(); 177 } 178 catch (NullPointerException ex) 179 { 180 assertEquals("Property 'wonk' of <WonkHolder> is null.", ex.getMessage()); 181 } 182 183 verifyControls(); 184 } 185 186 public void testWriteOnlyProperty() 187 { 188 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 189 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 190 .getMock(); 191 192 ServicePropertyFactory f = newFactory(); 193 194 ServicePropertyFactoryParameter parameters = newParameters(new WonkHolder(null), "writeOnly"); 195 196 fp.getFirstParameter(); 197 fpc.setReturnValue(parameters); 198 199 replayControls(); 200 201 try 202 { 203 f.createCoreServiceImplementation(fp); 204 unreachable(); 205 } 206 catch (ApplicationRuntimeException ex) 207 { 208 assertEquals("Property 'writeOnly' of <WonkHolder> is not readable.", ex.getMessage()); 209 assertEquals(HiveMind.getLocation(parameters), ex.getLocation()); 210 } 211 212 verifyControls(); 213 } 214 215 public void testPropertyTypeMismatch() 216 { 217 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 218 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 219 .getMock(); 220 221 ServicePropertyFactory f = newFactory(); 222 223 ServicePropertyFactoryParameter parameters = newParameters(new WonkHolder(null), "class"); 224 225 fp.getFirstParameter(); 226 fpc.setReturnValue(parameters); 227 228 fp.getServiceInterface(); 229 fpc.setReturnValue(Wonk.class); 230 231 replayControls(); 232 233 try 234 { 235 f.createCoreServiceImplementation(fp); 236 unreachable(); 237 } 238 catch (ApplicationRuntimeException ex) 239 { 240 assertEquals( 241 "Property 'class' of <WonkHolder> is type java.lang.Class, which does not match the expected interface org.apache.hivemind.lib.impl.Wonk.", 242 ex.getMessage()); 243 assertEquals(HiveMind.getLocation(parameters), ex.getLocation()); 244 } 245 246 verifyControls(); 247 } 248 249 public void testIntegrated() throws Exception 250 { 251 Registry r = buildFrameworkRegistry("ServicePropertyFactory.xml"); 252 253 WonkSource source = (WonkSource) r.getService(WonkSource.class); 254 Wonk wonkService = (Wonk) r.getService(Wonk.class); 255 256 Wonk wonk = (Wonk) newMock(Wonk.class); 257 258 source.setWonk(wonk); 259 260 wonk.wonkVoid(); 261 262 replayControls(); 263 264 267 wonkService.wonkVoid(); 268 269 verifyControls(); 270 } 271 272 276 public void testIntegratedTwoModules() throws Exception 277 { 278 Registry r = buildFrameworkRegistry("ServicePropertyFactoryMaster.xml"); 279 280 WonkSource source = (WonkSource) r.getService(WonkSource.class); 281 Wonk wonkService = (Wonk) r.getService(Wonk.class); 282 283 Wonk wonk = (Wonk) newMock(Wonk.class); 284 285 source.setWonk(wonk); 286 287 wonk.wonkVoid(); 288 289 replayControls(); 290 291 294 wonkService.wonkVoid(); 295 296 verifyControls(); 297 } 298 } | Popular Tags |