You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:58:03 UTC
svn commit: r815148 -
/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
Author: bayard
Date: Tue Sep 15 05:58:03 2009
New Revision: 815148
URL: http://svn.apache.org/viewvc?rev=815148&view=rev
Log:
Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions:
------------------------------------------------------------------------
r570378 | skestle | 2007-08-28 04:03:40 -0700 (Tue, 28 Aug 2007) | 1 line
Generified InstantiateFactory
------------------------------------------------------------------------
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java?rev=815148&r1=815147&r2=815148&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestFactoryUtils.java Tue Sep 15 05:58:03 2009
@@ -26,11 +26,8 @@
import java.util.Date;
import java.util.TimeZone;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
import org.apache.commons.collections.functors.ConstantFactory;
+import org.junit.Test;
/**
* Tests the org.apache.commons.collections.FactoryUtils class.
@@ -50,21 +47,6 @@
}
/**
- * Main.
- * @param args
- */
- public static void main(String[] args) {
- TestRunner.run(suite());
- }
-
- /**
- * Return class as a test suite.
- */
- public static Test suite() {
- return new TestSuite(TestFactoryUtils.class);
- }
-
- /**
* Set up instance variables required by this test case.
*/
public void setUp() {
@@ -98,7 +80,7 @@
//------------------------------------------------------------------
public void testNullFactory() {
- Factory factory = FactoryUtils.nullFactory();
+ Factory<Object> factory = FactoryUtils.nullFactory();
assertNotNull(factory);
Object created = factory.create();
assertNull(created);
@@ -108,7 +90,7 @@
//------------------------------------------------------------------
public void testConstantFactoryNull() {
- Factory factory = FactoryUtils.constantFactory(null);
+ Factory<Object> factory = FactoryUtils.constantFactory(null);
assertNotNull(factory);
Object created = factory.create();
assertNull(created);
@@ -116,9 +98,9 @@
public void testConstantFactoryConstant() {
Integer constant = new Integer(9);
- Factory factory = FactoryUtils.constantFactory(constant);
+ Factory<Integer> factory = FactoryUtils.constantFactory(constant);
assertNotNull(factory);
- Object created = factory.create();
+ Integer created = factory.create();
assertSame(constant, created);
}
@@ -131,9 +113,9 @@
public void testPrototypeFactoryPublicCloneMethod() throws Exception {
Date proto = new Date();
- Factory factory = FactoryUtils.prototypeFactory(proto);
+ Factory<Date> factory = FactoryUtils.prototypeFactory(proto);
assertNotNull(factory);
- Object created = factory.create();
+ Date created = factory.create();
assertTrue(proto != created);
assertEquals(proto, created);
@@ -143,13 +125,13 @@
out.writeObject(factory);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- Object dest = in.readObject();
+ in.readObject();
in.close();
}
public void testPrototypeFactoryPublicCopyConstructor() throws Exception {
Mock1 proto = new Mock1(6);
- Factory factory = FactoryUtils.prototypeFactory(proto);
+ Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto);
assertNotNull(factory);
Object created = factory.create();
assertTrue(proto != created);
@@ -163,21 +145,21 @@
} catch (NotSerializableException ex) {
out.close();
}
- factory = FactoryUtils.prototypeFactory(new Mock2("S"));
+ factory = FactoryUtils.<Object>prototypeFactory(new Mock2("S"));
buffer = new ByteArrayOutputStream();
out = new ObjectOutputStream(buffer);
out.writeObject(factory);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- Object dest = in.readObject();
+ in.readObject();
in.close();
}
public void testPrototypeFactoryPublicSerialization() throws Exception {
Integer proto = new Integer(9);
- Factory factory = FactoryUtils.prototypeFactory(proto);
+ Factory<Integer> factory = FactoryUtils.prototypeFactory(proto);
assertNotNull(factory);
- Object created = factory.create();
+ Integer created = factory.create();
assertTrue(proto != created);
assertEquals(proto, created);
@@ -187,17 +169,16 @@
out.writeObject(factory);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- Object dest = in.readObject();
+ in.readObject();
in.close();
}
public void testPrototypeFactoryPublicSerializationError() {
Mock2 proto = new Mock2(new Object());
- Factory factory = FactoryUtils.prototypeFactory(proto);
+ Factory<Object> factory = FactoryUtils.<Object>prototypeFactory(proto);
assertNotNull(factory);
try {
- Object created = factory.create();
-
+ factory.create();
} catch (FunctorException ex) {
assertTrue(ex.getCause() instanceof IOException);
return;
@@ -208,8 +189,7 @@
public void testPrototypeFactoryPublicBad() {
Object proto = new Object();
try {
- Factory factory = FactoryUtils.prototypeFactory(proto);
-
+ FactoryUtils.prototypeFactory(proto);
} catch (IllegalArgumentException ex) {
return;
}
@@ -234,6 +214,7 @@
}
}
+ @SuppressWarnings("serial")
public static class Mock2 implements Serializable {
private final Object iVal;
public Mock2(Object val) {
@@ -263,54 +244,40 @@
// instantiateFactory
//------------------------------------------------------------------
- public void testInstantiateFactoryNull() {
- try {
- Factory factory = FactoryUtils.instantiateFactory(null);
-
- } catch (IllegalArgumentException ex) {
- return;
- }
- fail();
+ @Test(expected=IllegalArgumentException.class)
+ public void instantiateFactoryNull() {
+ FactoryUtils.instantiateFactory(null);
}
- public void testInstantiateFactorySimple() {
- Factory factory = FactoryUtils.instantiateFactory(Mock3.class);
+ @Test
+ public void instantiateFactorySimple() {
+ Factory<Mock3> factory = FactoryUtils.instantiateFactory(Mock3.class);
assertNotNull(factory);
- Object created = factory.create();
- assertEquals(0, ((Mock3) created).getValue());
+ Mock3 created = factory.create();
+ assertEquals(0, created.getValue());
created = factory.create();
- assertEquals(1, ((Mock3) created).getValue());
+ assertEquals(1, created.getValue());
}
- public void testInstantiateFactoryMismatch() {
- try {
- Factory factory = FactoryUtils.instantiateFactory(Date.class, null, new Object[] {null});
-
- } catch (IllegalArgumentException ex) {
- return;
- }
- fail();
+ @Test(expected=IllegalArgumentException.class)
+ public void instantiateFactoryMismatch() {
+ FactoryUtils.instantiateFactory(Date.class, null, new Object[] {null});
}
- public void testInstantiateFactoryNoConstructor() {
- try {
- Factory factory = FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class}, new Object[] {null});
-
- } catch (IllegalArgumentException ex) {
- return;
- }
- fail();
+ @Test(expected=IllegalArgumentException.class)
+ public void instantiateFactoryNoConstructor() {
+ FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class}, new Object[] {null});
}
- public void testInstantiateFactoryComplex() {
+ @Test
+ public void instantiateFactoryComplex() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
// 2nd Jan 1970
- Factory factory = FactoryUtils.instantiateFactory(Date.class,
+ Factory<Date> factory = FactoryUtils.instantiateFactory(Date.class,
new Class[] {Integer.TYPE, Integer.TYPE, Integer.TYPE},
new Object[] {new Integer(70), new Integer(0), new Integer(2)});
assertNotNull(factory);
- Object created = factory.create();
- assertTrue(created instanceof Date);
+ Date created = factory.create();
// long time of 1 day (== 2nd Jan 1970)
assertEquals(new Date(1000 * 60 * 60 * 24), created);
}