KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > ServiceLocatorFactoryBeanTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.config;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.beans.MutablePropertyValues;
22 import org.springframework.beans.PropertyValue;
23 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24 import org.springframework.context.support.StaticApplicationContext;
25 import org.springframework.core.NestedCheckedException;
26 import org.springframework.core.NestedRuntimeException;
27
28 /**
29  * @author Colin Sampaleanu
30  */

31 public class ServiceLocatorFactoryBeanTests extends TestCase {
32
33     public void testNoArgGetter() {
34         StaticApplicationContext ctx = new StaticApplicationContext();
35         ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
36         MutablePropertyValues mpv = new MutablePropertyValues();
37         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator.class));
38         ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
39         ctx.refresh();
40
41         TestServiceLocator factory = (TestServiceLocator) ctx.getBean("factory");
42         TestService testBean = factory.getTestService();
43     }
44
45     public void testErrorOnTooManyOrTooFew() throws Exception JavaDoc {
46         StaticApplicationContext ctx = new StaticApplicationContext();
47         ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
48         ctx.registerSingleton("testServiceInstance2", TestService.class, new MutablePropertyValues());
49         MutablePropertyValues mpv = new MutablePropertyValues();
50         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator.class));
51         ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
52         mpv = new MutablePropertyValues();
53         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator2.class));
54         ctx.registerSingleton("factory2", ServiceLocatorFactoryBean.class, mpv);
55         mpv = new MutablePropertyValues();
56         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestService2Locator.class));
57         ctx.registerSingleton("factory3", ServiceLocatorFactoryBean.class, mpv);
58         ctx.refresh();
59
60         TestServiceLocator factory = (TestServiceLocator) ctx.getBean("factory");
61         try {
62             TestService testService = factory.getTestService();
63             fail("should fail on more than one matching type");
64         }
65         catch (NoSuchBeanDefinitionException ex) {
66             // expected
67
}
68         TestServiceLocator2 factory2 = (TestServiceLocator2) ctx.getBean("factory2");
69         try {
70             TestService testService = factory2.getTestService(null);
71             fail("should fail on more than one matching type");
72         }
73         catch (NoSuchBeanDefinitionException ex) {
74             // expected
75
}
76         TestService2Locator factory3 = (TestService2Locator) ctx.getBean("factory3");
77         try {
78             TestService2 testService2 = factory3.getTestService();
79             fail("should fail on no matching types");
80         }
81         catch (NoSuchBeanDefinitionException ex) {
82             // expected
83
}
84     }
85
86     public void testErrorOnTooManyOrTooFewWithCustomServiceLocatorException() {
87         StaticApplicationContext ctx = new StaticApplicationContext();
88         ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
89         ctx.registerSingleton("testServiceInstance2", TestService.class, new MutablePropertyValues());
90         MutablePropertyValues mpv = new MutablePropertyValues();
91         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator.class));
92         mpv.addPropertyValue(new PropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException1.class));
93         ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
94         mpv = new MutablePropertyValues();
95         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator2.class));
96         mpv.addPropertyValue(new PropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException2.class));
97         ctx.registerSingleton("factory2", ServiceLocatorFactoryBean.class, mpv);
98         mpv = new MutablePropertyValues();
99         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestService2Locator.class));
100         mpv.addPropertyValue(new PropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException3.class));
101         ctx.registerSingleton("factory3", ServiceLocatorFactoryBean.class, mpv);
102         ctx.refresh();
103
104         TestServiceLocator factory = (TestServiceLocator) ctx.getBean("factory");
105         try {
106             TestService testService = factory.getTestService();
107             fail("should fail on more than one matching type");
108         }
109         catch (CustomServiceLocatorException1 ex) {
110             // expected
111
assertTrue(ex.getCause() instanceof NoSuchBeanDefinitionException);
112         }
113         TestServiceLocator2 factory2 = (TestServiceLocator2) ctx.getBean("factory2");
114         try {
115             TestService testService = factory2.getTestService(null);
116             fail("should fail on more than one matching type");
117         }
118         catch (CustomServiceLocatorException2 ex) {
119             // expected
120
assertTrue(ex.getCause() instanceof NoSuchBeanDefinitionException);
121         }
122         TestService2Locator factory3 = (TestService2Locator) ctx.getBean("factory3");
123         try {
124             TestService2 testService2 = factory3.getTestService();
125             fail("should fail on no matching types");
126         }
127         catch (CustomServiceLocatorException3 ex) {
128             // expected
129
}
130     }
131
132     public void testStringArgGetter() throws Exception JavaDoc {
133         StaticApplicationContext ctx = new StaticApplicationContext();
134         ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
135         MutablePropertyValues mpv = new MutablePropertyValues();
136         mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator2.class));
137         ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
138         ctx.refresh();
139
140         // test string-arg getter with null id
141
TestServiceLocator2 factory = (TestServiceLocator2) ctx.getBean("factory");
142         TestService testBean = factory.getTestService(null);
143         // now test with explicit id
144
testBean = factory.getTestService("testService");
145         // now verify failure on bad id
146
try {
147             testBean = factory.getTestService("bogusTestService");
148             fail("illegal operation allowed");
149         }
150         catch (NoSuchBeanDefinitionException ex) {
151             // expected
152
}
153     }
154
155     public void testCombinedLocatorInterface() {
156         StaticApplicationContext ctx = new StaticApplicationContext();
157         ctx.registerPrototype("testService", TestService.class, new MutablePropertyValues());
158         ctx.registerAlias("testService", "1");
159         MutablePropertyValues mpv = new MutablePropertyValues();
160         mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
161         ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
162         ctx.refresh();
163
164         TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory");
165         TestService testBean1 = factory.getTestService();
166         TestService testBean2 = factory.getTestService("testService");
167         TestService testBean3 = factory.getTestService(1);
168         TestService testBean4 = factory.someFactoryMethod();
169         assertNotSame(testBean1, testBean2);
170         assertNotSame(testBean1, testBean3);
171         assertNotSame(testBean1, testBean4);
172         assertNotSame(testBean2, testBean3);
173         assertNotSame(testBean2, testBean4);
174         assertNotSame(testBean3, testBean4);
175
176         assertTrue(factory.toString().indexOf("TestServiceLocator3") != -1);
177     }
178
179     public void testServiceMappings() {
180         StaticApplicationContext ctx = new StaticApplicationContext();
181         ctx.registerPrototype("testService1", TestService.class, new MutablePropertyValues());
182         ctx.registerPrototype("testService2", ExtendedTestService.class, new MutablePropertyValues());
183         MutablePropertyValues mpv = new MutablePropertyValues();
184         mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
185         mpv.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2");
186         ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
187         ctx.refresh();
188
189         TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory");
190         TestService testBean1 = factory.getTestService();
191         TestService testBean2 = factory.getTestService("testService1");
192         TestService testBean3 = factory.getTestService(1);
193         TestService testBean4 = factory.getTestService(2);
194         assertNotSame(testBean1, testBean2);
195         assertNotSame(testBean1, testBean3);
196         assertNotSame(testBean1, testBean4);
197         assertNotSame(testBean2, testBean3);
198         assertNotSame(testBean2, testBean4);
199         assertNotSame(testBean3, testBean4);
200         assertFalse(testBean1 instanceof ExtendedTestService);
201         assertFalse(testBean2 instanceof ExtendedTestService);
202         assertFalse(testBean3 instanceof ExtendedTestService);
203         assertTrue(testBean4 instanceof ExtendedTestService);
204     }
205
206
207     public static class TestService {
208     }
209
210
211     public static class ExtendedTestService extends TestService {
212     }
213
214
215     public static class TestService2 {
216     }
217
218
219     public static interface TestServiceLocator {
220
221         TestService getTestService();
222     }
223
224
225     public static interface TestServiceLocator2 {
226
227         TestService getTestService(String JavaDoc id) throws CustomServiceLocatorException2;
228     }
229
230
231     public static interface TestServiceLocator3 {
232
233         TestService getTestService();
234
235         TestService getTestService(String JavaDoc id);
236
237         TestService getTestService(int id);
238
239         TestService someFactoryMethod();
240     }
241
242
243     public static interface TestService2Locator {
244
245         TestService2 getTestService() throws CustomServiceLocatorException3;
246     }
247
248
249     public static class CustomServiceLocatorException1 extends NestedRuntimeException {
250
251         public CustomServiceLocatorException1(String JavaDoc message, Throwable JavaDoc cause) {
252             super(message, cause);
253         }
254     }
255
256
257     public static class CustomServiceLocatorException2 extends NestedCheckedException {
258
259         public CustomServiceLocatorException2(Throwable JavaDoc cause) {
260             super("", cause);
261         }
262     }
263
264
265     public static class CustomServiceLocatorException3 extends NestedCheckedException {
266
267         public CustomServiceLocatorException3(String JavaDoc message) {
268             super(message);
269         }
270     }
271
272 }
273
Popular Tags