1 15 package hivemind.test.lib; 16 17 import java.util.List ; 18 19 import org.apache.hivemind.Registry; 20 import org.apache.hivemind.ServiceImplementationFactoryParameters; 21 import org.apache.hivemind.lib.SpringBeanFactoryHolder; 22 import org.apache.hivemind.lib.impl.SpringBeanParameter; 23 import org.apache.hivemind.lib.impl.SpringLookupFactory; 24 import org.apache.hivemind.xml.XmlTestCase; 25 import org.springframework.beans.factory.BeanFactory; 26 import org.springframework.beans.factory.xml.XmlBeanFactory; 27 import org.springframework.core.io.ClassPathResource; 28 29 35 public class TestSpringLookupFactory extends XmlTestCase 36 { 37 private SpringBeanParameter buildParameter(String beanName, BeanFactory f) 38 { 39 SpringBeanParameter p = new SpringBeanParameter(); 40 41 p.setName(beanName); 42 43 if (f != null) 44 p.setBeanFactory(f); 45 46 return p; 47 } 48 49 public void testDefaultFactory() 50 { 51 SpringLookupFactory lf = new SpringLookupFactory(); 52 53 BeanFactory beanFactory = (BeanFactory) newMock(BeanFactory.class); 54 55 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) newMock(ServiceImplementationFactoryParameters.class); 56 57 lf.setDefaultBeanFactory(beanFactory); 58 59 SpringBeanParameter param = buildParameter("fred", null); 60 61 Object fred = new Object (); 62 63 beanFactory.getBean("fred", List .class); 64 setReturnValue(beanFactory, fred); 65 66 fp.getFirstParameter(); 67 setReturnValue(fp, param); 68 69 fp.getServiceInterface(); 70 setReturnValue(fp, List .class); 71 72 replayControls(); 73 74 Object actual = lf.createCoreServiceImplementation(fp); 75 76 assertSame(fred, actual); 77 78 verifyControls(); 79 } 80 81 public void testBeanSpecificFactory() 82 { 83 SpringLookupFactory lf = new SpringLookupFactory(); 84 BeanFactory beanFactory = (BeanFactory) newMock(BeanFactory.class); 85 86 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) newMock(ServiceImplementationFactoryParameters.class); 87 88 SpringBeanParameter param = buildParameter("fred", beanFactory); 89 90 Object fred = new Object (); 91 92 beanFactory.getBean("fred", List .class); 93 setReturnValue(beanFactory, fred); 94 95 fp.getFirstParameter(); 96 setReturnValue(fp, param); 97 98 fp.getServiceInterface(); 99 setReturnValue(fp, List .class); 100 101 replayControls(); 102 103 Object actual = lf.createCoreServiceImplementation(fp); 104 105 assertSame(fred, actual); 106 107 verifyControls(); 108 } 109 110 public void testSpringIntegration() throws Exception 111 { 112 114 ClassPathResource springBeansResource = new ClassPathResource("SpringBeans.xml", 115 TestSpringLookupFactory.class); 116 117 BeanFactory beanFactory = new XmlBeanFactory(springBeansResource); 118 119 Registry r = buildFrameworkRegistry("SpringIntegration.xml"); 120 121 SpringBeanFactoryHolder h = (SpringBeanFactoryHolder) r.getService( 122 "hivemind.lib.DefaultSpringBeanFactoryHolder", 123 SpringBeanFactoryHolder.class); 124 125 h.setBeanFactory(beanFactory); 126 127 SimpleService a = (SimpleService) r.getService( 128 "hivemind.test.lib.Adder", 129 SimpleService.class); 130 131 assertEquals(17, a.add(9, 8)); 132 } 133 } | Popular Tags |