KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestFactoryUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
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 package org.apache.commons.collections;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.NotSerializableException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.TimeZone JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 import junit.textui.TestRunner;
31
32 import org.apache.commons.collections.functors.ConstantFactory;
33
34 /**
35  * Tests the org.apache.commons.collections.FactoryUtils class.
36  *
37  * @since Commons Collections 3.0
38  * @version $Revision: 1.11 $ $Date: 2004/02/18 01:20:35 $
39  *
40  * @author Stephen Colebourne
41  */

42 public class TestFactoryUtils extends junit.framework.TestCase {
43
44     /**
45      * Construct
46      */

47     public TestFactoryUtils(String JavaDoc name) {
48         super(name);
49     }
50
51     /**
52      * Main.
53      * @param args
54      */

55     public static void main(String JavaDoc[] args) {
56         TestRunner.run(suite());
57     }
58
59     /**
60      * Return class as a test suite.
61      */

62     public static Test suite() {
63         return new TestSuite(TestFactoryUtils.class);
64     }
65
66     /**
67      * Set up instance variables required by this test case.
68      */

69     public void setUp() {
70     }
71
72     /**
73      * Tear down instance variables required by this test case.
74      */

75     public void tearDown() {
76     }
77
78     // exceptionFactory
79
//------------------------------------------------------------------
80

81     public void testExceptionFactory() {
82         assertNotNull(FactoryUtils.exceptionFactory());
83         assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory());
84         try {
85             FactoryUtils.exceptionFactory().create();
86         } catch (FunctorException ex) {
87             try {
88                 FactoryUtils.exceptionFactory().create();
89             } catch (FunctorException ex2) {
90                 return;
91             }
92         }
93         fail();
94     }
95     
96     // nullFactory
97
//------------------------------------------------------------------
98

99     public void testNullFactory() {
100         Factory factory = FactoryUtils.nullFactory();
101         assertNotNull(factory);
102         Object JavaDoc created = factory.create();
103         assertNull(created);
104     }
105
106     // constantFactory
107
//------------------------------------------------------------------
108

109     public void testConstantFactoryNull() {
110         Factory factory = FactoryUtils.constantFactory(null);
111         assertNotNull(factory);
112         Object JavaDoc created = factory.create();
113         assertNull(created);
114     }
115
116     public void testConstantFactoryConstant() {
117         Integer JavaDoc constant = new Integer JavaDoc(9);
118         Factory factory = FactoryUtils.constantFactory(constant);
119         assertNotNull(factory);
120         Object JavaDoc created = factory.create();
121         assertSame(constant, created);
122     }
123
124     // prototypeFactory
125
//------------------------------------------------------------------
126

127     public void testPrototypeFactoryNull() {
128         assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils.prototypeFactory(null));
129     }
130
131     public void testPrototypeFactoryPublicCloneMethod() throws Exception JavaDoc {
132         Date JavaDoc proto = new Date JavaDoc();
133         Factory factory = FactoryUtils.prototypeFactory(proto);
134         assertNotNull(factory);
135         Object JavaDoc created = factory.create();
136         assertTrue(proto != created);
137         assertEquals(proto, created);
138         
139         // check serialisation works
140
ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
141         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
142         out.writeObject(factory);
143         out.close();
144         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
145         Object JavaDoc dest = in.readObject();
146         in.close();
147     }
148
149     public void testPrototypeFactoryPublicCopyConstructor() throws Exception JavaDoc {
150         Mock1 proto = new Mock1(6);
151         Factory factory = FactoryUtils.prototypeFactory(proto);
152         assertNotNull(factory);
153         Object JavaDoc created = factory.create();
154         assertTrue(proto != created);
155         assertEquals(proto, created);
156         
157         // check serialisation works
158
ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
159         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
160         try {
161             out.writeObject(factory);
162         } catch (NotSerializableException JavaDoc ex) {
163             out.close();
164         }
165         factory = FactoryUtils.prototypeFactory(new Mock2("S"));
166         buffer = new ByteArrayOutputStream JavaDoc();
167         out = new ObjectOutputStream JavaDoc(buffer);
168         out.writeObject(factory);
169         out.close();
170         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
171         Object JavaDoc dest = in.readObject();
172         in.close();
173     }
174
175     public void testPrototypeFactoryPublicSerialization() throws Exception JavaDoc {
176         Integer JavaDoc proto = new Integer JavaDoc(9);
177         Factory factory = FactoryUtils.prototypeFactory(proto);
178         assertNotNull(factory);
179         Object JavaDoc created = factory.create();
180         assertTrue(proto != created);
181         assertEquals(proto, created);
182         
183         // check serialisation works
184
ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
185         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
186         out.writeObject(factory);
187         out.close();
188         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
189         Object JavaDoc dest = in.readObject();
190         in.close();
191     }
192
193     public void testPrototypeFactoryPublicSerializationError() {
194         Mock2 proto = new Mock2(new Object JavaDoc());
195         Factory factory = FactoryUtils.prototypeFactory(proto);
196         assertNotNull(factory);
197         try {
198             Object JavaDoc created = factory.create();
199             
200         } catch (FunctorException ex) {
201             assertTrue(ex.getCause() instanceof IOException JavaDoc);
202             return;
203         }
204         fail();
205     }
206
207     public void testPrototypeFactoryPublicBad() {
208         Object JavaDoc proto = new Object JavaDoc();
209         try {
210             Factory factory = FactoryUtils.prototypeFactory(proto);
211             
212         } catch (IllegalArgumentException JavaDoc ex) {
213             return;
214         }
215         fail();
216     }
217
218     public static class Mock1 {
219         private final int iVal;
220         public Mock1(int val) {
221             iVal = val;
222         }
223         public Mock1(Mock1 mock) {
224             iVal = mock.iVal;
225         }
226         public boolean equals(Object JavaDoc obj) {
227             if (obj instanceof Mock1) {
228                 if (iVal == ((Mock1) obj).iVal) {
229                     return true;
230                 }
231             }
232             return false;
233         }
234     }
235     
236     public static class Mock2 implements Serializable JavaDoc {
237         private final Object JavaDoc iVal;
238         public Mock2(Object JavaDoc val) {
239             iVal = val;
240         }
241         public boolean equals(Object JavaDoc obj) {
242             if (obj instanceof Mock2) {
243                 if (iVal == ((Mock2) obj).iVal) {
244                     return true;
245                 }
246             }
247             return false;
248         }
249     }
250     
251     public static class Mock3 {
252         private static int cCounter = 0;
253         private final int iVal;
254         public Mock3() {
255             iVal = cCounter++;
256         }
257         public int getValue() {
258             return iVal;
259         }
260     }
261     
262     // instantiateFactory
263
//------------------------------------------------------------------
264

265     public void testInstantiateFactoryNull() {
266         try {
267             Factory factory = FactoryUtils.instantiateFactory(null);
268             
269         } catch (IllegalArgumentException JavaDoc ex) {
270             return;
271         }
272         fail();
273     }
274
275     public void testInstantiateFactorySimple() {
276         Factory factory = FactoryUtils.instantiateFactory(Mock3.class);
277         assertNotNull(factory);
278         Object JavaDoc created = factory.create();
279         assertEquals(0, ((Mock3) created).getValue());
280         created = factory.create();
281         assertEquals(1, ((Mock3) created).getValue());
282     }
283
284     public void testInstantiateFactoryMismatch() {
285         try {
286             Factory factory = FactoryUtils.instantiateFactory(Date JavaDoc.class, null, new Object JavaDoc[] {null});
287             
288         } catch (IllegalArgumentException JavaDoc ex) {
289             return;
290         }
291         fail();
292     }
293
294     public void testInstantiateFactoryNoConstructor() {
295         try {
296             Factory factory = FactoryUtils.instantiateFactory(Date JavaDoc.class, new Class JavaDoc[] {Long JavaDoc.class}, new Object JavaDoc[] {null});
297             
298         } catch (IllegalArgumentException JavaDoc ex) {
299             return;
300         }
301         fail();
302     }
303
304     public void testInstantiateFactoryComplex() {
305         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
306         // 2nd Jan 1970
307
Factory factory = FactoryUtils.instantiateFactory(Date JavaDoc.class,
308             new Class JavaDoc[] {Integer.TYPE, Integer.TYPE, Integer.TYPE},
309             new Object JavaDoc[] {new Integer JavaDoc(70), new Integer JavaDoc(0), new Integer JavaDoc(2)});
310         assertNotNull(factory);
311         Object JavaDoc created = factory.create();
312         assertTrue(created instanceof Date JavaDoc);
313         // long time of 1 day (== 2nd Jan 1970)
314
assertEquals(new Date JavaDoc(1000 * 60 * 60 * 24), created);
315     }
316
317 }
318
Popular Tags