001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.NotSerializableException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.Serializable;
025: import java.util.Date;
026: import java.util.TimeZone;
027:
028: import junit.framework.Test;
029: import junit.framework.TestSuite;
030: import junit.textui.TestRunner;
031:
032: import org.apache.commons.collections.functors.ConstantFactory;
033:
034: /**
035: * Tests the org.apache.commons.collections.FactoryUtils class.
036: *
037: * @since Commons Collections 3.0
038: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
039: *
040: * @author Stephen Colebourne
041: */
042: public class TestFactoryUtils extends junit.framework.TestCase {
043:
044: /**
045: * Construct
046: */
047: public TestFactoryUtils(String name) {
048: super (name);
049: }
050:
051: /**
052: * Main.
053: * @param args
054: */
055: public static void main(String[] args) {
056: TestRunner.run(suite());
057: }
058:
059: /**
060: * Return class as a test suite.
061: */
062: public static Test suite() {
063: return new TestSuite(TestFactoryUtils.class);
064: }
065:
066: /**
067: * Set up instance variables required by this test case.
068: */
069: public void setUp() {
070: }
071:
072: /**
073: * Tear down instance variables required by this test case.
074: */
075: public void tearDown() {
076: }
077:
078: // exceptionFactory
079: //------------------------------------------------------------------
080:
081: public void testExceptionFactory() {
082: assertNotNull(FactoryUtils.exceptionFactory());
083: assertSame(FactoryUtils.exceptionFactory(), FactoryUtils
084: .exceptionFactory());
085: try {
086: FactoryUtils.exceptionFactory().create();
087: } catch (FunctorException ex) {
088: try {
089: FactoryUtils.exceptionFactory().create();
090: } catch (FunctorException ex2) {
091: return;
092: }
093: }
094: fail();
095: }
096:
097: // nullFactory
098: //------------------------------------------------------------------
099:
100: public void testNullFactory() {
101: Factory factory = FactoryUtils.nullFactory();
102: assertNotNull(factory);
103: Object created = factory.create();
104: assertNull(created);
105: }
106:
107: // constantFactory
108: //------------------------------------------------------------------
109:
110: public void testConstantFactoryNull() {
111: Factory factory = FactoryUtils.constantFactory(null);
112: assertNotNull(factory);
113: Object created = factory.create();
114: assertNull(created);
115: }
116:
117: public void testConstantFactoryConstant() {
118: Integer constant = new Integer(9);
119: Factory factory = FactoryUtils.constantFactory(constant);
120: assertNotNull(factory);
121: Object created = factory.create();
122: assertSame(constant, created);
123: }
124:
125: // prototypeFactory
126: //------------------------------------------------------------------
127:
128: public void testPrototypeFactoryNull() {
129: assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils
130: .prototypeFactory(null));
131: }
132:
133: public void testPrototypeFactoryPublicCloneMethod()
134: throws Exception {
135: Date proto = new Date();
136: Factory factory = FactoryUtils.prototypeFactory(proto);
137: assertNotNull(factory);
138: Object created = factory.create();
139: assertTrue(proto != created);
140: assertEquals(proto, created);
141:
142: // check serialisation works
143: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
144: ObjectOutputStream out = new ObjectOutputStream(buffer);
145: out.writeObject(factory);
146: out.close();
147: ObjectInputStream in = new ObjectInputStream(
148: new ByteArrayInputStream(buffer.toByteArray()));
149: Object dest = in.readObject();
150: in.close();
151: }
152:
153: public void testPrototypeFactoryPublicCopyConstructor()
154: throws Exception {
155: Mock1 proto = new Mock1(6);
156: Factory factory = FactoryUtils.prototypeFactory(proto);
157: assertNotNull(factory);
158: Object created = factory.create();
159: assertTrue(proto != created);
160: assertEquals(proto, created);
161:
162: // check serialisation works
163: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
164: ObjectOutputStream out = new ObjectOutputStream(buffer);
165: try {
166: out.writeObject(factory);
167: } catch (NotSerializableException ex) {
168: out.close();
169: }
170: factory = FactoryUtils.prototypeFactory(new Mock2("S"));
171: buffer = new ByteArrayOutputStream();
172: out = new ObjectOutputStream(buffer);
173: out.writeObject(factory);
174: out.close();
175: ObjectInputStream in = new ObjectInputStream(
176: new ByteArrayInputStream(buffer.toByteArray()));
177: Object dest = in.readObject();
178: in.close();
179: }
180:
181: public void testPrototypeFactoryPublicSerialization()
182: throws Exception {
183: Integer proto = new Integer(9);
184: Factory factory = FactoryUtils.prototypeFactory(proto);
185: assertNotNull(factory);
186: Object created = factory.create();
187: assertTrue(proto != created);
188: assertEquals(proto, created);
189:
190: // check serialisation works
191: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
192: ObjectOutputStream out = new ObjectOutputStream(buffer);
193: out.writeObject(factory);
194: out.close();
195: ObjectInputStream in = new ObjectInputStream(
196: new ByteArrayInputStream(buffer.toByteArray()));
197: Object dest = in.readObject();
198: in.close();
199: }
200:
201: public void testPrototypeFactoryPublicSerializationError() {
202: Mock2 proto = new Mock2(new Object());
203: Factory factory = FactoryUtils.prototypeFactory(proto);
204: assertNotNull(factory);
205: try {
206: Object created = factory.create();
207:
208: } catch (FunctorException ex) {
209: assertTrue(ex.getCause() instanceof IOException);
210: return;
211: }
212: fail();
213: }
214:
215: public void testPrototypeFactoryPublicBad() {
216: Object proto = new Object();
217: try {
218: Factory factory = FactoryUtils.prototypeFactory(proto);
219:
220: } catch (IllegalArgumentException ex) {
221: return;
222: }
223: fail();
224: }
225:
226: public static class Mock1 {
227: private final int iVal;
228:
229: public Mock1(int val) {
230: iVal = val;
231: }
232:
233: public Mock1(Mock1 mock) {
234: iVal = mock.iVal;
235: }
236:
237: public boolean equals(Object obj) {
238: if (obj instanceof Mock1) {
239: if (iVal == ((Mock1) obj).iVal) {
240: return true;
241: }
242: }
243: return false;
244: }
245: }
246:
247: public static class Mock2 implements Serializable {
248: private final Object iVal;
249:
250: public Mock2(Object val) {
251: iVal = val;
252: }
253:
254: public boolean equals(Object obj) {
255: if (obj instanceof Mock2) {
256: if (iVal == ((Mock2) obj).iVal) {
257: return true;
258: }
259: }
260: return false;
261: }
262: }
263:
264: public static class Mock3 {
265: private static int cCounter = 0;
266: private final int iVal;
267:
268: public Mock3() {
269: iVal = cCounter++;
270: }
271:
272: public int getValue() {
273: return iVal;
274: }
275: }
276:
277: // instantiateFactory
278: //------------------------------------------------------------------
279:
280: public void testInstantiateFactoryNull() {
281: try {
282: Factory factory = FactoryUtils.instantiateFactory(null);
283:
284: } catch (IllegalArgumentException ex) {
285: return;
286: }
287: fail();
288: }
289:
290: public void testInstantiateFactorySimple() {
291: Factory factory = FactoryUtils.instantiateFactory(Mock3.class);
292: assertNotNull(factory);
293: Object created = factory.create();
294: assertEquals(0, ((Mock3) created).getValue());
295: created = factory.create();
296: assertEquals(1, ((Mock3) created).getValue());
297: }
298:
299: public void testInstantiateFactoryMismatch() {
300: try {
301: Factory factory = FactoryUtils.instantiateFactory(
302: Date.class, null, new Object[] { null });
303:
304: } catch (IllegalArgumentException ex) {
305: return;
306: }
307: fail();
308: }
309:
310: public void testInstantiateFactoryNoConstructor() {
311: try {
312: Factory factory = FactoryUtils.instantiateFactory(
313: Date.class, new Class[] { Long.class },
314: new Object[] { null });
315:
316: } catch (IllegalArgumentException ex) {
317: return;
318: }
319: fail();
320: }
321:
322: public void testInstantiateFactoryComplex() {
323: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
324: // 2nd Jan 1970
325: Factory factory = FactoryUtils
326: .instantiateFactory(Date.class, new Class[] {
327: Integer.TYPE, Integer.TYPE, Integer.TYPE },
328: new Object[] { new Integer(70), new Integer(0),
329: new Integer(2) });
330: assertNotNull(factory);
331: Object created = factory.create();
332: assertTrue(created instanceof Date);
333: // long time of 1 day (== 2nd Jan 1970)
334: assertEquals(new Date(1000 * 60 * 60 * 24), created);
335: }
336:
337: }
|