1 16 package org.apache.commons.collections; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.NotSerializableException ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.io.Serializable ; 25 import java.util.Date ; 26 import java.util.TimeZone ; 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 42 public class TestFactoryUtils extends junit.framework.TestCase { 43 44 47 public TestFactoryUtils(String name) { 48 super(name); 49 } 50 51 55 public static void main(String [] args) { 56 TestRunner.run(suite()); 57 } 58 59 62 public static Test suite() { 63 return new TestSuite(TestFactoryUtils.class); 64 } 65 66 69 public void setUp() { 70 } 71 72 75 public void tearDown() { 76 } 77 78 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 99 public void testNullFactory() { 100 Factory factory = FactoryUtils.nullFactory(); 101 assertNotNull(factory); 102 Object created = factory.create(); 103 assertNull(created); 104 } 105 106 109 public void testConstantFactoryNull() { 110 Factory factory = FactoryUtils.constantFactory(null); 111 assertNotNull(factory); 112 Object created = factory.create(); 113 assertNull(created); 114 } 115 116 public void testConstantFactoryConstant() { 117 Integer constant = new Integer (9); 118 Factory factory = FactoryUtils.constantFactory(constant); 119 assertNotNull(factory); 120 Object created = factory.create(); 121 assertSame(constant, created); 122 } 123 124 127 public void testPrototypeFactoryNull() { 128 assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils.prototypeFactory(null)); 129 } 130 131 public void testPrototypeFactoryPublicCloneMethod() throws Exception { 132 Date proto = new Date (); 133 Factory factory = FactoryUtils.prototypeFactory(proto); 134 assertNotNull(factory); 135 Object created = factory.create(); 136 assertTrue(proto != created); 137 assertEquals(proto, created); 138 139 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 141 ObjectOutputStream out = new ObjectOutputStream (buffer); 142 out.writeObject(factory); 143 out.close(); 144 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 145 Object dest = in.readObject(); 146 in.close(); 147 } 148 149 public void testPrototypeFactoryPublicCopyConstructor() throws Exception { 150 Mock1 proto = new Mock1(6); 151 Factory factory = FactoryUtils.prototypeFactory(proto); 152 assertNotNull(factory); 153 Object created = factory.create(); 154 assertTrue(proto != created); 155 assertEquals(proto, created); 156 157 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 159 ObjectOutputStream out = new ObjectOutputStream (buffer); 160 try { 161 out.writeObject(factory); 162 } catch (NotSerializableException ex) { 163 out.close(); 164 } 165 factory = FactoryUtils.prototypeFactory(new Mock2("S")); 166 buffer = new ByteArrayOutputStream (); 167 out = new ObjectOutputStream (buffer); 168 out.writeObject(factory); 169 out.close(); 170 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 171 Object dest = in.readObject(); 172 in.close(); 173 } 174 175 public void testPrototypeFactoryPublicSerialization() throws Exception { 176 Integer proto = new Integer (9); 177 Factory factory = FactoryUtils.prototypeFactory(proto); 178 assertNotNull(factory); 179 Object created = factory.create(); 180 assertTrue(proto != created); 181 assertEquals(proto, created); 182 183 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 185 ObjectOutputStream out = new ObjectOutputStream (buffer); 186 out.writeObject(factory); 187 out.close(); 188 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 189 Object dest = in.readObject(); 190 in.close(); 191 } 192 193 public void testPrototypeFactoryPublicSerializationError() { 194 Mock2 proto = new Mock2(new Object ()); 195 Factory factory = FactoryUtils.prototypeFactory(proto); 196 assertNotNull(factory); 197 try { 198 Object created = factory.create(); 199 200 } catch (FunctorException ex) { 201 assertTrue(ex.getCause() instanceof IOException ); 202 return; 203 } 204 fail(); 205 } 206 207 public void testPrototypeFactoryPublicBad() { 208 Object proto = new Object (); 209 try { 210 Factory factory = FactoryUtils.prototypeFactory(proto); 211 212 } catch (IllegalArgumentException 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 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 { 237 private final Object iVal; 238 public Mock2(Object val) { 239 iVal = val; 240 } 241 public boolean equals(Object 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 265 public void testInstantiateFactoryNull() { 266 try { 267 Factory factory = FactoryUtils.instantiateFactory(null); 268 269 } catch (IllegalArgumentException ex) { 270 return; 271 } 272 fail(); 273 } 274 275 public void testInstantiateFactorySimple() { 276 Factory factory = FactoryUtils.instantiateFactory(Mock3.class); 277 assertNotNull(factory); 278 Object 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 .class, null, new Object [] {null}); 287 288 } catch (IllegalArgumentException ex) { 289 return; 290 } 291 fail(); 292 } 293 294 public void testInstantiateFactoryNoConstructor() { 295 try { 296 Factory factory = FactoryUtils.instantiateFactory(Date .class, new Class [] {Long .class}, new Object [] {null}); 297 298 } catch (IllegalArgumentException ex) { 299 return; 300 } 301 fail(); 302 } 303 304 public void testInstantiateFactoryComplex() { 305 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 306 Factory factory = FactoryUtils.instantiateFactory(Date .class, 308 new Class [] {Integer.TYPE, Integer.TYPE, Integer.TYPE}, 309 new Object [] {new Integer (70), new Integer (0), new Integer (2)}); 310 assertNotNull(factory); 311 Object created = factory.create(); 312 assertTrue(created instanceof Date ); 313 assertEquals(new Date (1000 * 60 * 60 * 24), created); 315 } 316 317 } 318 | Popular Tags |