KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > factory > TestBeanFactoryImpl


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 org.apache.hivemind.lib.factory;
16
17 import java.io.Serializable JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.ErrorLog;
25 import org.apache.hivemind.Registry;
26 import org.apache.hivemind.ServiceImplementationFactoryParameters;
27 import org.apache.hivemind.lib.BeanFactory;
28 import org.apache.hivemind.xml.XmlTestCase;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests for {@link org.apache.hivemind.lib.factory.BeanFactoryImpl} and
33  * {@link org.apache.hivemind.lib.factory.BeanFactoryBuilder}.
34  *
35  * @author Howard Lewis Ship
36  */

37 public class TestBeanFactoryImpl extends XmlTestCase
38 {
39     private BeanFactoryContribution build(String JavaDoc name, Class JavaDoc objectClass)
40     {
41         return build(name, objectClass, null);
42     }
43
44     private BeanFactoryContribution build(String JavaDoc name, Class JavaDoc objectClass, Boolean JavaDoc cacheable)
45     {
46         BeanFactoryContribution result = new BeanFactoryContribution();
47         result.setName(name);
48         result.setBeanClass(objectClass);
49         result.setCacheable(cacheable);
50
51         return result;
52     }
53
54     private void executeNonClassContribution(String JavaDoc name, Class JavaDoc objectClass, String JavaDoc message)
55     {
56         List JavaDoc l = Collections.singletonList(build(name, objectClass));
57
58         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
59
60         el.error(message, null, null);
61
62         replayControls();
63
64         BeanFactoryImpl f = new BeanFactoryImpl(el, Object JavaDoc.class, l, true);
65
66         try
67         {
68             f.get(name);
69             unreachable();
70         }
71         catch (ApplicationRuntimeException ex)
72         {
73             assertEquals(FactoryMessages.unknownContribution(name), ex.getMessage());
74         }
75
76         verifyControls();
77     }
78
79     public void testInterfaceContribution()
80     {
81         executeNonClassContribution(
82                 "serializable",
83                 Serializable JavaDoc.class,
84                 "Contribution 'serializable' is for java.io.Serializable which is inappropriate for an object factory. The contribution has been ignored.");
85     }
86
87     public void testArrayContribution()
88     {
89         executeNonClassContribution(
90                 "array",
91                 String JavaDoc[].class,
92                 "Contribution 'array' is for java.lang.String[] which is inappropriate for an object factory. The contribution has been ignored.");
93     }
94
95     public void testPrimitiveContribution()
96     {
97         executeNonClassContribution(
98                 "primitive",
99                 double.class,
100                 "Contribution 'primitive' is for double which is inappropriate for an object factory. The contribution has been ignored.");
101     }
102
103     public void testIncorrectType()
104     {
105         List JavaDoc l = Collections.singletonList(build("array-list", ArrayList JavaDoc.class));
106
107         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
108
109         el
110                 .error(
111                         "Contribution 'array-list' (class java.util.ArrayList) is not assignable to interface java.util.Map and has been ignored.",
112                         null,
113                         null);
114
115         replayControls();
116
117         BeanFactoryImpl f = new BeanFactoryImpl(el, Map JavaDoc.class, l, true);
118
119         try
120         {
121             f.get("array-list");
122             unreachable();
123         }
124         catch (ApplicationRuntimeException ex)
125         {
126             assertEquals(FactoryMessages.unknownContribution("array-list"), ex.getMessage());
127         }
128
129         verifyControls();
130     }
131
132     public void testTranslator()
133     {
134         List JavaDoc l = Collections.singletonList(build("string", String JavaDoc.class));
135
136         BeanFactoryImpl f = new BeanFactoryImpl(null, Object JavaDoc.class, l, true);
137
138         String JavaDoc s = (String JavaDoc) f.get("string,locator");
139
140         assertEquals("locator", s);
141     }
142
143     public void testPlain()
144     {
145         List JavaDoc l = Collections.singletonList(build("string", String JavaDoc.class));
146
147         BeanFactoryImpl f = new BeanFactoryImpl(null, Object JavaDoc.class, l, true);
148
149         String JavaDoc s1 = (String JavaDoc) f.get("string");
150         String JavaDoc s2 = (String JavaDoc) f.get("string");
151
152         assertSame(s1, s2);
153     }
154
155     public void testNonCache()
156     {
157         List JavaDoc l = Collections.singletonList(build("buffer", StringBuffer JavaDoc.class, Boolean.FALSE));
158
159         BeanFactoryImpl f = new BeanFactoryImpl(null, Object JavaDoc.class, l, true);
160
161         StringBuffer JavaDoc s1 = (StringBuffer JavaDoc) f.get("buffer");
162         StringBuffer JavaDoc s2 = (StringBuffer JavaDoc) f.get("buffer");
163
164         assertNotSame(s1, s2);
165     }
166
167     public void testConstructFailure()
168     {
169         List JavaDoc l = Collections.singletonList(build("integer", Integer JavaDoc.class));
170
171         BeanFactoryImpl f = new BeanFactoryImpl(null, Number JavaDoc.class, l, true);
172
173         try
174         {
175             f.get("integer");
176             unreachable();
177         }
178         catch (ApplicationRuntimeException ex)
179         {
180             assertEquals(
181                     "Unable to instantiate instance of class java.lang.Integer: java.lang.Integer",
182                     ex.getMessage());
183         }
184
185     }
186
187     public void testBuilder()
188     {
189         BeanFactoryParameter p = new BeanFactoryParameter();
190
191         List JavaDoc l = Collections.singletonList(build("integer", Integer JavaDoc.class));
192
193         p.setContributionsList(l);
194
195         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
196         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
197                 .getMock();
198
199         fp.getFirstParameter();
200         fpc.setReturnValue(p);
201
202         fp.getErrorLog();
203         fpc.setReturnValue(newMock(ErrorLog.class));
204
205         replayControls();
206
207         BeanFactoryBuilder b = new BeanFactoryBuilder();
208
209         BeanFactory f = (BeanFactory) b.createCoreServiceImplementation(fp);
210
211         Integer JavaDoc i = (Integer JavaDoc) f.get("integer,5");
212
213         assertEquals(new Integer JavaDoc(5), i);
214
215         verifyControls();
216     }
217
218     /**
219      * Test integration; i.e., a service and configuration in a descriptor.
220      */

221     public void testIntegration() throws Exception JavaDoc
222     {
223         Registry r = buildFrameworkRegistry("NumberFactory.xml");
224
225         BeanFactory f = (BeanFactory) r.getService(
226                 "hivemind.lib.test.NumberFactory",
227                 BeanFactory.class);
228
229         assertEquals(new Integer JavaDoc(27), f.get("int,27"));
230         assertEquals(new Double JavaDoc(-22.5), f.get("double,-22.5"));
231     }
232
233     public void testContains()
234     {
235         List JavaDoc l = Collections.singletonList(build("integer", Integer JavaDoc.class));
236
237         BeanFactoryImpl f = new BeanFactoryImpl(null, Integer JavaDoc.class, l, true);
238
239         boolean contains = f.contains("integer");
240
241         assertTrue(contains);
242     }
243
244     public void testContainsFailure()
245     {
246         List JavaDoc l = Collections.singletonList(build("integer", Integer JavaDoc.class));
247
248         BeanFactoryImpl f = new BeanFactoryImpl(null, Integer JavaDoc.class, l, true);
249
250         boolean contains = f.contains("not_found");
251
252         assertTrue(!contains);
253     }
254 }
Popular Tags