Source code: hivemind/test/lib/TestSpringLookupFactory.java
1 // Copyright 2004, 2005 The Apache Software Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
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.test.HiveMindTestCase;
25 import org.springframework.beans.factory.BeanFactory;
26 import org.springframework.beans.factory.xml.XmlBeanFactory;
27 import org.springframework.core.io.ClassPathResource;
28
29 /**
30 * Tests for the {@link org.apache.hivemind.lib.impl.SpringLookupFactory} service implementation
31 * factory.
32 *
33 * @author Howard Lewis Ship
34 */
35 public class TestSpringLookupFactory extends HiveMindTestCase
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 // Spring setup
113
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 }