commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/functors InstantiateFactory.java InvokerTransformer.java InstantiateTransformer.java PrototypeFactory.java MapTransformer.java
Date Thu, 27 Nov 2003 23:57:09 GMT
scolebourne    2003/11/27 15:57:09

  Modified:    collections/src/test/org/apache/commons/collections
                        TestFactoryUtils.java TestClosureUtils.java
                        TestTransformerUtils.java
               collections/src/java/org/apache/commons/collections/functors
                        InstantiateFactory.java InvokerTransformer.java
                        InstantiateTransformer.java PrototypeFactory.java
                        MapTransformer.java
  Log:
  Improve test coverage
  
  Revision  Changes    Path
  1.8       +54 -19    jakarta-commons/collections/src/test/org/apache/commons/collections/TestFactoryUtils.java
  
  Index: TestFactoryUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFactoryUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestFactoryUtils.java	23 Nov 2003 17:48:19 -0000	1.7
  +++ TestFactoryUtils.java	27 Nov 2003 23:57:09 -0000	1.8
  @@ -57,17 +57,23 @@
    */
   package org.apache.commons.collections;
   
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import java.io.NotSerializableException;
  +import java.io.ObjectInputStream;
  +import java.io.ObjectOutputStream;
   import java.io.Serializable;
   import java.util.Date;
   import java.util.TimeZone;
   
  -import org.apache.commons.collections.functors.FunctorException;
  -
   import junit.framework.Test;
   import junit.framework.TestSuite;
   import junit.textui.TestRunner;
   
  +import org.apache.commons.collections.functors.ConstantFactory;
  +import org.apache.commons.collections.functors.FunctorException;
  +
   /**
    * Tests the org.apache.commons.collections.FactoryUtils class.
    * 
  @@ -162,40 +168,69 @@
       //------------------------------------------------------------------
       
       public void testPrototypeFactoryNull() {
  -        try {
  -            Factory factory = FactoryUtils.prototypeFactory(null);
  -            
  -        } catch (IllegalArgumentException ex) {
  -            return;
  -        }
  -        fail();
  +        assertSame(ConstantFactory.NULL_INSTANCE, FactoryUtils.prototypeFactory(null));
       }
   
  -    public void testPrototypeFactoryPublicCloneMethod() {
  +    public void testPrototypeFactoryPublicCloneMethod() throws Exception {
           Date proto = new Date();
           Factory factory = FactoryUtils.prototypeFactory(proto);
           assertNotNull(factory);
           Object created = factory.create();
           assertTrue(proto != created);
           assertEquals(proto, created);
  +        
  +        // check serialisation works
  +        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  +        ObjectOutputStream out = new ObjectOutputStream(buffer);
  +        out.writeObject(factory);
  +        out.close();
  +        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  +        Object dest = in.readObject();
  +        in.close();
       }
   
  -    public void testPrototypeFactoryPublicCopyConstructor() {
  +    public void testPrototypeFactoryPublicCopyConstructor() throws Exception {
           Mock1 proto = new Mock1(6);
           Factory factory = FactoryUtils.prototypeFactory(proto);
           assertNotNull(factory);
           Object created = factory.create();
           assertTrue(proto != created);
           assertEquals(proto, created);
  +        
  +        // check serialisation works
  +        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  +        ObjectOutputStream out = new ObjectOutputStream(buffer);
  +        try {
  +            out.writeObject(factory);
  +        } catch (NotSerializableException ex) {
  +            out.close();
  +        }
  +        factory = FactoryUtils.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.close();
       }
   
  -    public void testPrototypeFactoryPublicSerialization() {
  +    public void testPrototypeFactoryPublicSerialization() throws Exception {
           Integer proto = new Integer(9);
           Factory factory = FactoryUtils.prototypeFactory(proto);
           assertNotNull(factory);
           Object created = factory.create();
           assertTrue(proto != created);
           assertEquals(proto, created);
  +        
  +        // check serialisation works
  +        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  +        ObjectOutputStream out = new ObjectOutputStream(buffer);
  +        out.writeObject(factory);
  +        out.close();
  +        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  +        Object dest = in.readObject();
  +        in.close();
       }
   
       public void testPrototypeFactoryPublicSerializationError() {
  @@ -270,7 +305,7 @@
       // instantiateFactory
       //------------------------------------------------------------------
       
  -    public void testReflectionFactoryNull() {
  +    public void testInstantiateFactoryNull() {
           try {
               Factory factory = FactoryUtils.instantiateFactory(null);
               
  @@ -280,7 +315,7 @@
           fail();
       }
   
  -    public void testReflectionFactorySimple() {
  +    public void testInstantiateFactorySimple() {
           Factory factory = FactoryUtils.instantiateFactory(Mock3.class);
           assertNotNull(factory);
           Object created = factory.create();
  @@ -289,7 +324,7 @@
           assertEquals(1, ((Mock3) created).getValue());
       }
   
  -    public void testReflectionFactoryMismatch() {
  +    public void testInstantiateFactoryMismatch() {
           try {
               Factory factory = FactoryUtils.instantiateFactory(Date.class, null, new Object[]
{null});
               
  @@ -299,7 +334,7 @@
           fail();
       }
   
  -    public void testReflectionFactoryNoConstructor() {
  +    public void testInstantiateFactoryNoConstructor() {
           try {
               Factory factory = FactoryUtils.instantiateFactory(Date.class, new Class[] {Long.class},
new Object[] {null});
               
  @@ -309,7 +344,7 @@
           fail();
       }
   
  -    public void testReflectionFactoryComplex() {
  +    public void testInstantiateFactoryComplex() {
           TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
           // 2nd Jan 1970
           Factory factory = FactoryUtils.instantiateFactory(Date.class,
  
  
  
  1.6       +48 -12    jakarta-commons/collections/src/test/org/apache/commons/collections/TestClosureUtils.java
  
  Index: TestClosureUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestClosureUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestClosureUtils.java	23 Nov 2003 17:01:36 -0000	1.5
  +++ TestClosureUtils.java	27 Nov 2003 23:57:09 -0000	1.6
  @@ -124,7 +124,14 @@
           public void execute(Object object) {
               count++;
           }
  -
  +    }
  +    static class MockTransformer implements Transformer {
  +        int count = 0;
  +        
  +        public Object transform(Object object) {
  +            count++;
  +            return object;
  +        }
       }
   
       // exceptionClosure
  @@ -189,12 +196,23 @@
           MockClosure cmd = new MockClosure();
           ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), cmd).execute(null);
           assertEquals(0, cmd.count);
  +        
  +        cmd = new MockClosure();
  +        ClosureUtils.whileClosure(PredicateUtils.uniquePredicate(), cmd).execute(null);
  +        assertEquals(1, cmd.count);
  +        
  +        try {
  +            ClosureUtils.whileClosure(null, ClosureUtils.nopClosure());
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
           try {
               ClosureUtils.whileClosure(null, null);
  -        } catch (IllegalArgumentException ex) {
  -            return;
  -        }
  -        fail();
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
   
       // doWhileClosure
  @@ -204,12 +222,15 @@
           MockClosure cmd = new MockClosure();
           ClosureUtils.doWhileClosure(cmd, PredicateUtils.falsePredicate()).execute(null);
           assertEquals(1, cmd.count);
  +        
  +        cmd = new MockClosure();
  +        ClosureUtils.doWhileClosure(cmd, PredicateUtils.uniquePredicate()).execute(null);
  +        assertEquals(2, cmd.count);
  +        
           try {
               ClosureUtils.doWhileClosure(null, null);
  -        } catch (IllegalArgumentException ex) {
  -            return;
  -        }
  -        fail();
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
   
       // chainedClosure
  @@ -361,7 +382,9 @@
               fail();
           } catch (IllegalArgumentException ex) {}
           try {
  -            ClosureUtils.switchClosure(new Predicate[2], new Closure[1]);
  +            ClosureUtils.switchClosure(
  +                    new Predicate[] {PredicateUtils.truePredicate()},
  +                    new Closure[] {a,b});
               fail();
           } catch (IllegalArgumentException ex) {}
       }
  @@ -408,5 +431,18 @@
           } catch (IllegalArgumentException ex) {}
       }
       
  +    // asClosure
  +    //------------------------------------------------------------------
  +
  +    public void testTransformerClosure() {
  +        MockTransformer mock = new MockTransformer();
  +        Closure closure = ClosureUtils.asClosure(mock);
  +        closure.execute(null);
  +        assertEquals(1, mock.count);
  +        closure.execute(null);
  +        assertEquals(2, mock.count);
  +        
  +        assertSame(ClosureUtils.nopClosure(), ClosureUtils.asClosure(null));
  +    }
       
   }
  
  
  
  1.7       +46 -3     jakarta-commons/collections/src/test/org/apache/commons/collections/TestTransformerUtils.java
  
  Index: TestTransformerUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestTransformerUtils.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestTransformerUtils.java	23 Nov 2003 23:25:33 -0000	1.6
  +++ TestTransformerUtils.java	27 Nov 2003 23:57:09 -0000	1.7
  @@ -60,6 +60,7 @@
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Collections;
  +import java.util.Date;
   import java.util.HashMap;
   import java.util.List;
   import java.util.Map;
  @@ -171,6 +172,7 @@
           assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cObject));
           assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cString));
           assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cInteger));
  +        assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.constantTransformer(null));
       }
   
       // cloneTransformer
  @@ -200,6 +202,7 @@
           assertEquals(new Integer(1), TransformerUtils.mapTransformer(map).transform(cObject));
           assertEquals(new Integer(2), TransformerUtils.mapTransformer(map).transform(cString));
           assertEquals(null, TransformerUtils.mapTransformer(map).transform(cInteger));
  +        assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.mapTransformer(null));
       }
   
       // commandTransformer
  @@ -350,7 +353,9 @@
               fail();
           } catch (IllegalArgumentException ex) {}
           try {
  -            TransformerUtils.switchTransformer(new Predicate[2], new Transformer[1]);
  +            TransformerUtils.switchTransformer(
  +                    new Predicate[] {PredicateUtils.truePredicate()},
  +                    new Transformer[] {a,b});
               fail();
           } catch (IllegalArgumentException ex) {}
       }
  @@ -425,6 +430,18 @@
                   "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).transform(new
Object());
               fail();
           } catch (FunctorException ex) {}
  +        try {
  +            TransformerUtils.invokerTransformer("badArgs", null, new Object[] { cString
});
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            TransformerUtils.invokerTransformer("badArgs", new Class[] {Object.class},
null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            TransformerUtils.invokerTransformer("badArgs", new Class[] {}, new Object[]
{ cString });
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
       
       // stringValueTransformer
  @@ -439,4 +456,30 @@
               TransformerUtils.stringValueTransformer().transform(new Integer(6)));
       }
       
  +    // instantiateFactory
  +    //------------------------------------------------------------------
  +    
  +    public void testInstantiateTransformerNull() {
  +        try {
  +            Transformer trans = TransformerUtils.instantiateTransformer(null, new Object[]
{"str"});
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            Transformer trans = TransformerUtils.instantiateTransformer(new Class[] {},
new Object[] {"str"});
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        
  +        Transformer trans = TransformerUtils.instantiateTransformer(new Class[] {Long.class},
new Object[] {null});
  +        try {
  +            trans.transform(String.class);
  +            fail();
  +        } catch (FunctorException ex) {}
  +        
  +        trans = TransformerUtils.instantiateTransformer();
  +        assertEquals("", trans.transform(String.class));
  +        
  +        trans = TransformerUtils.instantiateTransformer(new Class[] {Long.TYPE}, new Object[]
{new Long(1000L)});
  +        assertEquals(new Date(1000L), trans.transform(Date.class));
  +    }
  +
   }
  
  
  
  1.2       +4 -5      jakarta-commons/collections/src/java/org/apache/commons/collections/functors/InstantiateFactory.java
  
  Index: InstantiateFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/functors/InstantiateFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InstantiateFactory.java	23 Nov 2003 17:48:19 -0000	1.1
  +++ InstantiateFactory.java	27 Nov 2003 23:57:09 -0000	1.2
  @@ -103,13 +103,12 @@
           }
   
           if (paramTypes == null || paramTypes.length == 0) {
  -            paramTypes = null;
  -            args = null;
  +            return new InstantiateFactory(classToInstantiate);
           } else {
               paramTypes = (Class[]) paramTypes.clone();
               args = (Object[]) args.clone();
  +            return new InstantiateFactory(classToInstantiate, paramTypes, args);
           }
  -        return new InstantiateFactory(classToInstantiate, paramTypes, args);
       }
   
       /**
  
  
  
  1.2       +4 -5      jakarta-commons/collections/src/java/org/apache/commons/collections/functors/InvokerTransformer.java
  
  Index: InvokerTransformer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/functors/InvokerTransformer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InvokerTransformer.java	23 Nov 2003 23:25:33 -0000	1.1
  +++ InvokerTransformer.java	27 Nov 2003 23:57:09 -0000	1.2
  @@ -99,13 +99,12 @@
               throw new IllegalArgumentException("The parameter types must match the arguments");
           }
           if (paramTypes == null || paramTypes.length == 0) {
  -            paramTypes = null;
  -            args = null;
  +            return new InvokerTransformer(methodName);
           } else {
               paramTypes = (Class[]) paramTypes.clone();
               args = (Object[]) args.clone();
  +            return new InvokerTransformer(methodName, paramTypes, args);
           }
  -        return new InvokerTransformer(methodName, paramTypes, args);
       }
   
       /**
  
  
  
  1.2       +3 -3      jakarta-commons/collections/src/java/org/apache/commons/collections/functors/InstantiateTransformer.java
  
  Index: InstantiateTransformer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/functors/InstantiateTransformer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InstantiateTransformer.java	23 Nov 2003 23:25:33 -0000	1.1
  +++ InstantiateTransformer.java	27 Nov 2003 23:57:09 -0000	1.2
  @@ -142,7 +142,7 @@
               return con.newInstance(iArgs);
   
           } catch (NoSuchMethodException ex) {
  -            throw new IllegalArgumentException("InstantiateTransformer: The constructor
must exist and be public ");
  +            throw new FunctorException("InstantiateTransformer: The constructor must exist
and be public ");
           } catch (InstantiationException ex) {
               throw new FunctorException("InstantiateTransformer: InstantiationException",
ex);
           } catch (IllegalAccessException ex) {
  
  
  
  1.2       +8 -15     jakarta-commons/collections/src/java/org/apache/commons/collections/functors/PrototypeFactory.java
  
  Index: PrototypeFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/functors/PrototypeFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PrototypeFactory.java	23 Nov 2003 17:48:19 -0000	1.1
  +++ PrototypeFactory.java	27 Nov 2003 23:57:09 -0000	1.2
  @@ -97,11 +97,11 @@
        */
       public static Factory getInstance(Object prototype) {
           if (prototype == null) {
  -            throw new IllegalArgumentException("The prototype must not be null");
  +            return ConstantFactory.NULL_INSTANCE;
           }
           try {
  -            prototype.getClass().getMethod("clone", null);
  -            return new PrototypeCloneFactory(prototype);
  +            Method method = prototype.getClass().getMethod("clone", null);
  +            return new PrototypeCloneFactory(prototype, method);
   
           } catch (NoSuchMethodException ex) {
               try {
  @@ -145,16 +145,12 @@
           private transient Method iCloneMethod;
   
           /**
  -         * Constructor to store prototype
  +         * Constructor to store prototype.
            */
  -        private PrototypeCloneFactory(Object prototype) {
  +        private PrototypeCloneFactory(Object prototype, Method method) {
               super();
  -            if (prototype == null) {
  -                throw new IllegalArgumentException("PrototypeCloneFactory: The prototype
must not be null");
  -            }
               iPrototype = prototype;
  -
  -            findCloneMethod();
  +            iCloneMethod = method;
           }
   
           /**
  @@ -207,9 +203,6 @@
            */
           private PrototypeSerializationFactory(Serializable prototype) {
               super();
  -            if (prototype == null) {
  -                throw new IllegalArgumentException("PrototypeSerializationFactory: The
prototype must not be null");
  -            }
               iPrototype = prototype;
           }
   
  
  
  
  1.2       +9 -7      jakarta-commons/collections/src/java/org/apache/commons/collections/functors/MapTransformer.java
  
  Index: MapTransformer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/functors/MapTransformer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MapTransformer.java	23 Nov 2003 23:25:33 -0000	1.1
  +++ MapTransformer.java	27 Nov 2003 23:57:09 -0000	1.2
  @@ -63,8 +63,8 @@
   import org.apache.commons.collections.Transformer;
   
   /**
  - * Predicate implementation that returns true the first time an object is
  - * passed into the predicate.
  + * Transformer implementation that returns the value held in a specified map
  + * using the input parameter as a key.
    * 
    * @since Commons Collections 3.0
    * @version $Revision$ $Date$
  @@ -81,13 +81,15 @@
   
       /**
        * Factory to create the transformer.
  +     * <p>
  +     * If the map is null, a transformer that always returns null is returned.
        * 
  -     * @return the map, not cloned, not null
  -     * @throws IllegalArgumentException if the map is null
  +     * @param the map, not cloned
  +     * @return the transformer
        */
       public static Transformer getInstance(Map map) {
           if (map == null) {
  -            throw new IllegalArgumentException("The map must not be null");
  +            return ConstantTransformer.NULL_INSTANCE;
           }
           return new MapTransformer(map);
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message