From f51f23917803a1c1e16be489f3b513e3fdb147d9 Mon Sep 17 00:00:00 2001 From: Stephen Kestle Date: Tue, 28 Aug 2007 11:03:40 +0000 Subject: [PATCH] Generified InstantiateFactory git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/collections_jdk5_branch@570378 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/FactoryUtils.java | 4 +- .../functors/InstantiateFactory.java | 23 +++--- .../commons/collections/TestFactoryUtils.java | 72 ++++++------------- 3 files changed, 33 insertions(+), 66 deletions(-) diff --git a/src/java/org/apache/commons/collections/FactoryUtils.java b/src/java/org/apache/commons/collections/FactoryUtils.java index 062c0cddd..a41004310 100644 --- a/src/java/org/apache/commons/collections/FactoryUtils.java +++ b/src/java/org/apache/commons/collections/FactoryUtils.java @@ -117,7 +117,7 @@ public class FactoryUtils { * @return the reflection factory * @throws IllegalArgumentException if the classToInstantiate is null */ - public static Factory instantiateFactory(Class classToInstantiate) { + public static Factory instantiateFactory(Class classToInstantiate) { return InstantiateFactory.getInstance(classToInstantiate, null, null); } @@ -135,7 +135,7 @@ public class FactoryUtils { * @throws IllegalArgumentException if the paramTypes and args don't match * @throws IllegalArgumentException if the constructor doesn't exist */ - public static Factory instantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args) { + public static Factory instantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args) { return InstantiateFactory.getInstance(classToInstantiate, paramTypes, args); } diff --git a/src/java/org/apache/commons/collections/functors/InstantiateFactory.java b/src/java/org/apache/commons/collections/functors/InstantiateFactory.java index e1d99fbb9..f6e11df7c 100644 --- a/src/java/org/apache/commons/collections/functors/InstantiateFactory.java +++ b/src/java/org/apache/commons/collections/functors/InstantiateFactory.java @@ -31,19 +31,19 @@ import org.apache.commons.collections.FunctorException; * * @author Stephen Colebourne */ -public class InstantiateFactory implements Factory, Serializable { +public class InstantiateFactory implements Factory, Serializable { /** The serial version */ private static final long serialVersionUID = -7732226881069447957L; /** The class to create */ - private final Class iClassToInstantiate; + private final Class iClassToInstantiate; /** The constructor parameter types */ private final Class[] iParamTypes; /** The constructor arguments */ private final Object[] iArgs; /** The constructor */ - private transient Constructor iConstructor = null; + private transient Constructor iConstructor = null; /** * Factory method that performs validation. @@ -53,7 +53,7 @@ public class InstantiateFactory implements Factory, Serializable { * @param args the constructor arguments * @return a new instantiate factory */ - public static Factory getInstance(Class classToInstantiate, Class[] paramTypes, Object[] args) { + public static Factory getInstance(Class classToInstantiate, Class[] paramTypes, Object[] args) { if (classToInstantiate == null) { throw new IllegalArgumentException("Class to instantiate must not be null"); } @@ -64,12 +64,11 @@ public class InstantiateFactory implements Factory, Serializable { } if (paramTypes == null || paramTypes.length == 0) { - return new InstantiateFactory(classToInstantiate); - } else { - paramTypes = (Class[]) paramTypes.clone(); - args = (Object[]) args.clone(); - return new InstantiateFactory(classToInstantiate, paramTypes, args); + return new InstantiateFactory(classToInstantiate); } + paramTypes = paramTypes.clone(); + args = args.clone(); + return new InstantiateFactory(classToInstantiate, paramTypes, args); } /** @@ -78,7 +77,7 @@ public class InstantiateFactory implements Factory, Serializable { * * @param classToInstantiate the class to instantiate */ - public InstantiateFactory(Class classToInstantiate) { + public InstantiateFactory(Class classToInstantiate) { super(); iClassToInstantiate = classToInstantiate; iParamTypes = null; @@ -94,7 +93,7 @@ public class InstantiateFactory implements Factory, Serializable { * @param paramTypes the constructor parameter types, not cloned * @param args the constructor arguments, not cloned */ - public InstantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args) { + public InstantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args) { super(); iClassToInstantiate = classToInstantiate; iParamTypes = paramTypes; @@ -119,7 +118,7 @@ public class InstantiateFactory implements Factory, Serializable { * * @return the new object */ - public Object create() { + public T create() { // needed for post-serialization if (iConstructor == null) { findConstructor(); diff --git a/src/test/org/apache/commons/collections/TestFactoryUtils.java b/src/test/org/apache/commons/collections/TestFactoryUtils.java index 1e5914185..3abbeaa6c 100644 --- a/src/test/org/apache/commons/collections/TestFactoryUtils.java +++ b/src/test/org/apache/commons/collections/TestFactoryUtils.java @@ -26,11 +26,8 @@ import java.io.Serializable; 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. @@ -49,21 +46,6 @@ public class TestFactoryUtils extends junit.framework.TestCase { super(name); } - /** - * 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. */ @@ -263,54 +245,40 @@ public class TestFactoryUtils extends junit.framework.TestCase { // 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 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 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); }