bayard 2002/11/05 08:45:13 Added: lang/src/test/org/apache/commons/lang/functor FunctorTestSuite.java TestClosureUtils.java TestFactoryUtils.java TestPredicateUtils.java TestTransformerUtils.java Log: Transfer of Patterns code over to Lang Revision Changes Path 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/functor/FunctorTestSuite.java Index: FunctorTestSuite.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.commons.lang.functor; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.textui.TestRunner; /** * Test suite for the Pattern packages. * * @author <a href="mailto:scolebourne@;joda.org">Stephen Colebourne</a> * @version $Id: FunctorTestSuite.java,v 1.1 2002/11/05 16:45:13 bayard Exp $ */ public class FunctorTestSuite extends TestCase { /** * Construct a new instance. */ public FunctorTestSuite(String name) { super(name); } /** * Closure-line interface. */ public static void main(String[] args) { TestRunner.run(suite()); } /** * Get the suite of tests */ public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(TestClosureUtils.suite()); suite.addTest(TestFactoryUtils.suite()); suite.addTest(TestPredicateUtils.suite()); suite.addTest(TestTransformerUtils.suite()); return suite; } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/functor/TestClosureUtils.java Index: TestClosureUtils.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.commons.lang.functor; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.lang.functor.Predicate; import org.apache.commons.lang.functor.PredicateUtils; /** * Tests the org.apache.commons.lang.functor.ClosureUtils class. * * @author <a href="mailto:scolebourne@;joda.org">Stephen Colebourne</a> * @version $Id: TestClosureUtils.java,v 1.1 2002/11/05 16:45:13 bayard Exp $ */ public class TestClosureUtils extends junit.framework.TestCase { private static final Object cObject = new Object(); private static final Object cString = "Hello"; private static final Object cInteger = new Integer(6); /** * Construct */ public TestClosureUtils(String name) { super(name); } /** * Return class aa a test suite. */ public static Test suite() { return new TestSuite(TestClosureUtils.class); } /** * Set up instance variables required by this test case. */ public void setUp() { } /** * Tear down instance variables required by this test case. */ public void tearDown() { } static class MockClosure implements Closure { int count = 0; /** * @see org.apache.commons.lang.functor.Closure#execute(Object) */ public void execute(Object object) { count++; } } // exceptionClosure //------------------------------------------------------------------ public void testExceptionClosure() { assertNotNull(ClosureUtils.exceptionClosure()); assertSame(ClosureUtils.exceptionClosure(), ClosureUtils.exceptionClosure()); try { ClosureUtils.exceptionClosure().execute(null); } catch (ClosureException ex) { try { ClosureUtils.exceptionClosure().execute(cString); } catch (ClosureException ex2) { return; } } fail(); } // nopClosure //------------------------------------------------------------------ public void testNopClosure() { StringBuffer buf = new StringBuffer("Hello"); ClosureUtils.nopClosure().execute(null); assertEquals("Hello", buf.toString()); ClosureUtils.nopClosure().execute("Hello"); assertEquals("Hello", buf.toString()); } // invokeClosure //------------------------------------------------------------------ public void testInvokeClosure() { StringBuffer buf = new StringBuffer("Hello"); ClosureUtils.invokerClosure("reverse").execute(buf); assertEquals("olleH", buf.toString()); buf = new StringBuffer("Hello"); ClosureUtils.invokerClosure("setLength", new Class[] {Integer.TYPE}, new Object[] {new Integer(2)}).execute(buf); assertEquals("He", buf.toString()); } // forClosure //------------------------------------------------------------------ public void testForClosure() { MockClosure cmd = new MockClosure(); ClosureUtils.forClosure(5, cmd).execute(null); assertEquals(5, cmd.count); try { ClosureUtils.forClosure(-1, new MockClosure()); } catch (IllegalArgumentException ex) { return; } fail(); } // whileClosure //------------------------------------------------------------------ public void testWhileClosure() { MockClosure cmd = new MockClosure(); ClosureUtils.whileClosure(PredicateUtils.falsePredicate(), cmd).execute(null); assertEquals(0, cmd.count); try { ClosureUtils.whileClosure(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } // doWhileClosure //------------------------------------------------------------------ public void testDoWhileClosure() { MockClosure cmd = new MockClosure(); ClosureUtils.doWhileClosure(cmd, PredicateUtils.falsePredicate()).execute(null); assertEquals(1, cmd.count); try { ClosureUtils.doWhileClosure(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } // chainedClosure //------------------------------------------------------------------ public void testChainedClosure() { MockClosure a = new MockClosure(); MockClosure b = new MockClosure(); ClosureUtils.chainedClosure(a, b).execute(null); assertEquals(1, a.count); assertEquals(1, b.count); a = new MockClosure(); b = new MockClosure(); ClosureUtils.chainedClosure(new Closure[] {a, b, a}).execute(null); assertEquals(2, a.count); assertEquals(1, b.count); a = new MockClosure(); b = new MockClosure(); Collection coll = new ArrayList(); coll.add(b); coll.add(a); coll.add(b); ClosureUtils.chainedClosure(coll).execute(null); assertEquals(1, a.count); assertEquals(2, b.count); } public void testChainedClosureEx1a() { try { ClosureUtils.chainedClosure(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedClosureEx1b() { try { ClosureUtils.chainedClosure((Closure[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedClosureEx1c() { try { ClosureUtils.chainedClosure((Collection) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedClosureEx2() { try { ClosureUtils.chainedClosure(new Closure[0]); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedClosureEx3() { try { ClosureUtils.chainedClosure(new Closure[] {null, null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedClosureEx4() { try { ClosureUtils.chainedClosure(Collections.EMPTY_LIST); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedClosureEx5() { try { Collection coll = new ArrayList(); coll.add(null); coll.add(null); ClosureUtils.chainedClosure(coll); } catch (IllegalArgumentException ex) { return; } fail(); } // switchClosure //------------------------------------------------------------------ public void testSwitchClosure() { MockClosure a = new MockClosure(); MockClosure b = new MockClosure(); ClosureUtils.switchClosure(PredicateUtils.truePredicate(), a, b).execute(null); assertEquals(1, a.count); assertEquals(0, b.count); a = new MockClosure(); b = new MockClosure(); ClosureUtils.switchClosure(PredicateUtils.falsePredicate(), a, b).execute(null); assertEquals(0, a.count); assertEquals(1, b.count); a = new MockClosure(); b = new MockClosure(); ClosureUtils.switchClosure( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Closure[] {a, b}).execute("WELL"); assertEquals(0, a.count); assertEquals(0, b.count); a = new MockClosure(); b = new MockClosure(); ClosureUtils.switchClosure( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Closure[] {a, b}).execute("HELLO"); assertEquals(1, a.count); assertEquals(0, b.count); a = new MockClosure(); b = new MockClosure(); MockClosure c = new MockClosure(); ClosureUtils.switchClosure( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Closure[] {a, b}, c).execute("WELL"); assertEquals(0, a.count); assertEquals(0, b.count); assertEquals(1, c.count); a = new MockClosure(); b = new MockClosure(); Map map = new HashMap(); map.put(PredicateUtils.equalPredicate("HELLO"), a); map.put(PredicateUtils.equalPredicate("THERE"), b); ClosureUtils.switchClosure(map).execute(null); assertEquals(0, a.count); assertEquals(0, b.count); a = new MockClosure(); b = new MockClosure(); map = new HashMap(); map.put(PredicateUtils.equalPredicate("HELLO"), a); map.put(PredicateUtils.equalPredicate("THERE"), b); ClosureUtils.switchClosure(map).execute("THERE"); assertEquals(0, a.count); assertEquals(1, b.count); a = new MockClosure(); b = new MockClosure(); c = new MockClosure(); map = new HashMap(); map.put(PredicateUtils.equalPredicate("HELLO"), a); map.put(PredicateUtils.equalPredicate("THERE"), b); map.put(null, c); ClosureUtils.switchClosure(map).execute("WELL"); assertEquals(0, a.count); assertEquals(0, b.count); assertEquals(1, c.count); } public void testSwitchClosureEx1a() { try { ClosureUtils.switchClosure(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx1b() { try { ClosureUtils.switchClosure((Predicate[]) null, (Closure[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx1c() { try { ClosureUtils.switchClosure((Map) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx2() { try { ClosureUtils.switchClosure(new Predicate[0], new Closure[0]); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx3() { try { ClosureUtils.switchClosure(new Predicate[2], new Closure[2]); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx4() { try { ClosureUtils.switchClosure(Collections.EMPTY_MAP); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx5() { try { Map map = new HashMap(); map.put(null, null); map.put(null, null); ClosureUtils.switchClosure(map); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchClosureEx6() { try { ClosureUtils.switchClosure(new Predicate[2], new Closure[1]); } catch (IllegalArgumentException ex) { return; } fail(); } // switchMapClosure //------------------------------------------------------------------ public void testSwitchMapClosure() { MockClosure a = new MockClosure(); MockClosure b = new MockClosure(); Map map = new HashMap(); map.put("HELLO", a); map.put("THERE", b); ClosureUtils.switchMapClosure(map).execute(null); assertEquals(0, a.count); assertEquals(0, b.count); a = new MockClosure(); b = new MockClosure(); map = new HashMap(); map.put("HELLO", a); map.put("THERE", b); ClosureUtils.switchMapClosure(map).execute("THERE"); assertEquals(0, a.count); assertEquals(1, b.count); a = new MockClosure(); b = new MockClosure(); MockClosure c = new MockClosure(); map = new HashMap(); map.put("HELLO", a); map.put("THERE", b); map.put(null, c); ClosureUtils.switchMapClosure(map).execute("WELL"); assertEquals(0, a.count); assertEquals(0, b.count); assertEquals(1, c.count); } public void testSwitchMapClosureEx1() { try { ClosureUtils.switchMapClosure(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchMapClosureEx2() { try { ClosureUtils.switchMapClosure(Collections.EMPTY_MAP); } catch (IllegalArgumentException ex) { return; } fail(); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/functor/TestFactoryUtils.java Index: TestFactoryUtils.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.commons.lang.functor; import java.io.Serializable; import java.util.Date; import java.util.TimeZone; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.lang.SerializationException; /** * Tests the org.apache.commons.lang.functor.FactoryUtils class. * * @author <a href="mailto:scolebourne@;joda.org">Stephen Colebourne</a> * @version $Id: TestFactoryUtils.java,v 1.1 2002/11/05 16:45:13 bayard Exp $ */ public class TestFactoryUtils extends junit.framework.TestCase { /** * Construct */ public TestFactoryUtils(String name) { super(name); } /** * Return class aa a test suite. */ public static Test suite() { return new TestSuite(TestFactoryUtils.class); } /** * Set up instance variables required by this test case. */ public void setUp() { } /** * Tear down instance variables required by this test case. */ public void tearDown() { } // exceptionFactory //------------------------------------------------------------------ public void testExceptionFactory() { assertNotNull(FactoryUtils.exceptionFactory()); assertSame(FactoryUtils.exceptionFactory(), FactoryUtils.exceptionFactory()); try { FactoryUtils.exceptionFactory().create(); } catch (FactoryException ex) { try { FactoryUtils.exceptionFactory().create(); } catch (FactoryException ex2) { return; } } fail(); } // nullFactory //------------------------------------------------------------------ public void testNullFactory() { Factory factory = FactoryUtils.nullFactory(); assertNotNull(factory); Object created = factory.create(); assertNull(created); } // constantFactory //------------------------------------------------------------------ public void testConstantFactoryNull() { Factory factory = FactoryUtils.constantFactory(null); assertNotNull(factory); Object created = factory.create(); assertNull(created); } public void testConstantFactoryConstant() { Integer constant = new Integer(9); Factory factory = FactoryUtils.constantFactory(constant); assertNotNull(factory); Object created = factory.create(); assertSame(constant, created); } // prototypeFactory //------------------------------------------------------------------ public void testPrototypeFactoryNull() { try { Factory factory = FactoryUtils.prototypeFactory(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testPrototypeFactoryPublicCloneMethod() { Date proto = new Date(); Factory factory = FactoryUtils.prototypeFactory(proto); assertNotNull(factory); Object created = factory.create(); assertTrue(proto != created); assertEquals(proto, created); } public void testPrototypeFactoryPublicCopyConstructor() { Mock1 proto = new Mock1(6); Factory factory = FactoryUtils.prototypeFactory(proto); assertNotNull(factory); Object created = factory.create(); assertTrue(proto != created); assertEquals(proto, created); } public void testPrototypeFactoryPublicSerialization() { Integer proto = new Integer(9); Factory factory = FactoryUtils.prototypeFactory(proto); assertNotNull(factory); Object created = factory.create(); assertTrue(proto != created); assertEquals(proto, created); } public void testPrototypeFactoryPublicSerializationError() { Mock2 proto = new Mock2(new Object()); Factory factory = FactoryUtils.prototypeFactory(proto); assertNotNull(factory); try { Object created = factory.create(); } catch (FactoryException ex) { assertTrue(ex.getCause() instanceof SerializationException); return; } fail(); } public void testPrototypeFactoryPublicBad() { Object proto = new Object(); try { Factory factory = FactoryUtils.prototypeFactory(proto); } catch (IllegalArgumentException ex) { return; } fail(); } public static class Mock1 { private final int iVal; public Mock1(int val) { iVal = val; } public Mock1(Mock1 mock) { iVal = mock.iVal; } public boolean equals(Object obj) { if (obj instanceof Mock1) { if (iVal == ((Mock1) obj).iVal) { return true; } } return false; } } public static class Mock2 implements Serializable { private final Object iVal; public Mock2(Object val) { iVal = val; } public boolean equals(Object obj) { if (obj instanceof Mock2) { if (iVal == ((Mock2) obj).iVal) { return true; } } return false; } } // reflectionFactory //------------------------------------------------------------------ public void testReflectionFactoryNull() { try { Factory factory = FactoryUtils.reflectionFactory(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testReflectionFactorySimple() { Factory factory = FactoryUtils.reflectionFactory(Date.class); assertNotNull(factory); Object created = factory.create(); assertTrue(created instanceof Date); assertEquals((double) System.currentTimeMillis(), (double) ((Date) created).getTime(), 0.01d); } public void testReflectionFactoryMismatch() { try { Factory factory = FactoryUtils.reflectionFactory(Date.class, null, new Object[] {null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testReflectionFactoryNoConstructor() { try { Factory factory = FactoryUtils.reflectionFactory(Date.class, new Class[] {Long.class}, new Object[] {null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testReflectionFactoryComplex() { TimeZone.setDefault(TimeZone.getTimeZone("GMT")); Factory factory = FactoryUtils.reflectionFactory(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); assertEquals(new Date(1000 * 60 * 60 * 24), created); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/functor/TestPredicateUtils.java Index: TestPredicateUtils.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.commons.lang.functor; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.lang.SerializationException; import org.apache.commons.lang.functor.TransformerUtils; /** * Tests the org.apache.commons.lang.functor.PredicateUtils class. * * @author <a href="mailto:scolebourne@;joda.org">Stephen Colebourne</a> * @version $Id: TestPredicateUtils.java,v 1.1 2002/11/05 16:45:13 bayard Exp $ */ public class TestPredicateUtils extends junit.framework.TestCase { private static final Object cObject = new Object(); private static final Object cString = "Hello"; private static final Object cInteger = new Integer(6); /** * Construct */ public TestPredicateUtils(String name) { super(name); } /** * Return class aa a test suite. */ public static Test suite() { return new TestSuite(TestPredicateUtils.class); } /** * Set up instance variables required by this test case. */ public void setUp() { } /** * Tear down instance variables required by this test case. */ public void tearDown() { } // exceptionPredicate //------------------------------------------------------------------ public void testExceptionPredicate() { assertNotNull(PredicateUtils.exceptionPredicate()); assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate()); try { PredicateUtils.exceptionPredicate().evaluate(null); } catch (PredicateException ex) { try { PredicateUtils.exceptionPredicate().evaluate(cString); } catch (PredicateException ex2) { return; } } fail(); } // nullPredicate //------------------------------------------------------------------ public void testNullPredicate() { assertNotNull(PredicateUtils.nullPredicate()); assertSame(PredicateUtils.nullPredicate(), PredicateUtils.nullPredicate()); assertEquals(true, PredicateUtils.nullPredicate().evaluate(null)); assertEquals(false, PredicateUtils.nullPredicate().evaluate(cObject)); assertEquals(false, PredicateUtils.nullPredicate().evaluate(cString)); assertEquals(false, PredicateUtils.nullPredicate().evaluate(cInteger)); } // notNullPredicate //------------------------------------------------------------------ public void testIsNotNullPredicate() { assertNotNull(PredicateUtils.notNullPredicate()); assertSame(PredicateUtils.notNullPredicate(), PredicateUtils.notNullPredicate()); assertEquals(false, PredicateUtils.notNullPredicate().evaluate(null)); assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cObject)); assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cString)); assertEquals(true, PredicateUtils.notNullPredicate().evaluate(cInteger)); } // equalPredicate //------------------------------------------------------------------ public void testEqualPredicate() { assertSame(PredicateUtils.nullPredicate(), PredicateUtils.equalPredicate(null)); assertNotNull(PredicateUtils.equalPredicate(new Integer(6))); assertEquals(false, PredicateUtils.equalPredicate(new Integer(6)).evaluate(null)); assertEquals(false, PredicateUtils.equalPredicate(new Integer(6)).evaluate(cObject)); assertEquals(false, PredicateUtils.equalPredicate(new Integer(6)).evaluate(cString)); assertEquals(true, PredicateUtils.equalPredicate(new Integer(6)).evaluate(cInteger)); } // identityPredicate //------------------------------------------------------------------ public void testIdentityPredicate() { assertSame(PredicateUtils.nullPredicate(), PredicateUtils.identityPredicate(null)); assertNotNull(PredicateUtils.identityPredicate(new Integer(6))); assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(null)); assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cObject)); assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cString)); assertEquals(false, PredicateUtils.identityPredicate(new Integer(6)).evaluate(cInteger)); assertEquals(true, PredicateUtils.identityPredicate(cInteger).evaluate(cInteger)); } // truePredicate //------------------------------------------------------------------ public void testTruePredicate() { assertNotNull(PredicateUtils.truePredicate()); assertSame(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()); assertEquals(true, PredicateUtils.truePredicate().evaluate(null)); assertEquals(true, PredicateUtils.truePredicate().evaluate(cObject)); assertEquals(true, PredicateUtils.truePredicate().evaluate(cString)); assertEquals(true, PredicateUtils.truePredicate().evaluate(cInteger)); } // falsePredicate //------------------------------------------------------------------ public void testFalsePredicate() { assertNotNull(PredicateUtils.falsePredicate()); assertSame(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()); assertEquals(false, PredicateUtils.falsePredicate().evaluate(null)); assertEquals(false, PredicateUtils.falsePredicate().evaluate(cObject)); assertEquals(false, PredicateUtils.falsePredicate().evaluate(cString)); assertEquals(false, PredicateUtils.falsePredicate().evaluate(cInteger)); } // notPredicate //------------------------------------------------------------------ public void testNotPredicate() { assertNotNull(PredicateUtils.notPredicate(PredicateUtils.truePredicate())); assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cObject)); assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cString)); assertEquals(false, PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cInteger)); } public void testNotPredicateEx() { try { PredicateUtils.notPredicate(null); } catch (IllegalArgumentException ex) { return; } fail(); } // andPredicate //------------------------------------------------------------------ public void testAndPredicate() { assertEquals(true, PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.andPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); } public void testAndPredicateEx() { try { PredicateUtils.andPredicate(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } // allPredicate //------------------------------------------------------------------ public void testAllPredicate() { assertEquals(true, PredicateUtils.allPredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.allPredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.allPredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.allPredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null)); Collection coll = new ArrayList(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(true, PredicateUtils.allPredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); assertEquals(false, PredicateUtils.allPredicate(coll).evaluate(null)); } public void testAllPredicateEx1() { try { PredicateUtils.allPredicate((Predicate[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAllPredicateEx2() { try { PredicateUtils.allPredicate(new Predicate[] {null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAllPredicateEx3() { try { PredicateUtils.allPredicate(new Predicate[] {null, null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAllPredicateEx4() { try { PredicateUtils.allPredicate((Collection) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAllPredicateEx5() { try { PredicateUtils.allPredicate(Collections.EMPTY_LIST); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAllPredicateEx6() { try { Collection coll = new ArrayList(); coll.add(null); coll.add(null); PredicateUtils.allPredicate(coll); } catch (IllegalArgumentException ex) { return; } fail(); } // orPredicate //------------------------------------------------------------------ public void testOrPredicate() { assertEquals(true, PredicateUtils.orPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.orPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.orPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.orPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); } public void testOrPredicateEx() { try { PredicateUtils.orPredicate(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } // anyPredicate //------------------------------------------------------------------ public void testAnyPredicate() { assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.anyPredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null)); Collection coll = new ArrayList(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(true, PredicateUtils.anyPredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); assertEquals(false, PredicateUtils.anyPredicate(coll).evaluate(null)); } public void testAnyPredicateEx1() { try { PredicateUtils.anyPredicate((Predicate[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAnyPredicateEx2() { try { PredicateUtils.anyPredicate(new Predicate[] {null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAnyPredicateEx3() { try { PredicateUtils.anyPredicate(new Predicate[] {null, null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAnyPredicateEx4() { try { PredicateUtils.anyPredicate((Collection) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAnyPredicateEx5() { try { PredicateUtils.anyPredicate(Collections.EMPTY_LIST); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAnyPredicateEx6() { try { Collection coll = new ArrayList(); coll.add(null); coll.add(null); PredicateUtils.anyPredicate(coll); } catch (IllegalArgumentException ex) { return; } fail(); } // eitherPredicate //------------------------------------------------------------------ public void testEitherPredicate() { assertEquals(false, PredicateUtils.eitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.eitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.eitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.eitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); } public void testEitherPredicateEx() { try { PredicateUtils.eitherPredicate(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } // onePredicate //------------------------------------------------------------------ public void testOnePredicate() { assertEquals(false, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(true, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null)); assertEquals(true, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()}).evaluate(null)); assertEquals(true, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.onePredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null)); Collection coll = new ArrayList(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(true, PredicateUtils.onePredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); assertEquals(false, PredicateUtils.onePredicate(coll).evaluate(null)); } public void testOnePredicateEx1() { try { PredicateUtils.onePredicate((Predicate[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testOnePredicateEx2() { try { PredicateUtils.onePredicate(new Predicate[] {null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testOnePredicateEx3() { try { PredicateUtils.onePredicate(new Predicate[] {null, null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testOnePredicateEx4() { try { PredicateUtils.onePredicate((Collection) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testOnePredicateEx5() { try { PredicateUtils.onePredicate(Collections.EMPTY_LIST); } catch (IllegalArgumentException ex) { return; } fail(); } public void testOnePredicateEx6() { try { Collection coll = new ArrayList(); coll.add(null); coll.add(null); PredicateUtils.onePredicate(coll); } catch (IllegalArgumentException ex) { return; } fail(); } // neitherPredicate //------------------------------------------------------------------ public void testNeitherPredicate() { assertEquals(false, PredicateUtils.neitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.neitherPredicate(PredicateUtils.truePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); assertEquals(false, PredicateUtils.neitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.neitherPredicate(PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()).evaluate(null)); } public void testNeitherPredicateEx() { try { PredicateUtils.neitherPredicate(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } // nonePredicate //------------------------------------------------------------------ public void testNonePredicate() { assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.truePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.truePredicate()}).evaluate(null)); assertEquals(true, PredicateUtils.nonePredicate(new Predicate[] { PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate(), PredicateUtils.falsePredicate()}).evaluate(null)); Collection coll = new ArrayList(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.truePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.truePredicate()); assertEquals(false, PredicateUtils.nonePredicate(coll).evaluate(null)); coll.clear(); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); coll.add(PredicateUtils.falsePredicate()); assertEquals(true, PredicateUtils.nonePredicate(coll).evaluate(null)); } public void testNonePredicateEx1() { try { PredicateUtils.nonePredicate((Predicate[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testNonePredicateEx2() { try { PredicateUtils.nonePredicate(new Predicate[] {null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testNonePredicateEx3() { try { PredicateUtils.nonePredicate(new Predicate[] {null, null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testNonePredicateEx4() { try { PredicateUtils.nonePredicate((Collection) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testNonePredicateEx5() { try { PredicateUtils.nonePredicate(Collections.EMPTY_LIST); } catch (IllegalArgumentException ex) { return; } fail(); } public void testNonePredicateEx6() { try { Collection coll = new ArrayList(); coll.add(null); coll.add(null); PredicateUtils.nonePredicate(coll); } catch (IllegalArgumentException ex) { return; } fail(); } // instanceofPredicate //------------------------------------------------------------------ public void testInstanceOfPredicate() { assertNotNull(PredicateUtils.instanceofPredicate(String.class)); assertEquals(false, PredicateUtils.instanceofPredicate(String.class).evaluate(null)); assertEquals(false, PredicateUtils.instanceofPredicate(String.class).evaluate(cObject)); assertEquals(true, PredicateUtils.instanceofPredicate(String.class).evaluate(cString)); assertEquals(false, PredicateUtils.instanceofPredicate(String.class).evaluate(cInteger)); } // uniquePredicate //------------------------------------------------------------------ public void testUniquePredicate() { Predicate p = PredicateUtils.uniquePredicate(); assertEquals(true, p.evaluate(new Object())); assertEquals(true, p.evaluate(new Object())); assertEquals(true, p.evaluate(new Object())); assertEquals(true, p.evaluate(cString)); assertEquals(false, p.evaluate(cString)); assertEquals(false, p.evaluate(cString)); } // asPredicate(Transformer) //------------------------------------------------------------------ public void testAsPredicateTransformer() { assertEquals(false, PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.FALSE)); assertEquals(true, PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(Boolean.TRUE)); } public void testAsPredicateTransformerEx1() { try { PredicateUtils.asPredicate(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testAsPredicateTransformerEx2() { try { PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(null); } catch (PredicateException ex) { return; } fail(); } // invokerPredicate //------------------------------------------------------------------ public void testInvokerPredicate() { List list = new ArrayList(); assertEquals(true, PredicateUtils.invokerPredicate("isEmpty").evaluate(list)); list.add(new Object()); assertEquals(false, PredicateUtils.invokerPredicate("isEmpty").evaluate(list)); } public void testInvokerPredicateEx1() { try { PredicateUtils.invokerPredicate(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testInvokerPredicateEx2() { try { PredicateUtils.invokerPredicate("isEmpty").evaluate(null); } catch (PredicateException ex) { return; } fail(); } public void testInvokerPredicateEx3() { try { PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object()); } catch (PredicateException ex) { return; } fail(); } // invokerPredicate2 //------------------------------------------------------------------ public void testInvokerPredicate2() { List list = new ArrayList(); assertEquals(false, PredicateUtils.invokerPredicate( "contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(list)); list.add(cString); assertEquals(true, PredicateUtils.invokerPredicate( "contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(list)); } public void testInvokerPredicate2Ex1() { try { PredicateUtils.invokerPredicate(null, null, null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testInvokerPredicate2Ex2() { try { PredicateUtils.invokerPredicate("contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(null); } catch (PredicateException ex) { return; } fail(); } public void testInvokerPredicate2Ex3() { try { PredicateUtils.invokerPredicate( "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).evaluate(new Object()); } catch (PredicateException ex) { return; } fail(); } // nullIsException //------------------------------------------------------------------ public void testNullIsExceptionPredicate() { assertEquals(true, PredicateUtils.nullIsExceptionPredicate(PredicateUtils.truePredicate()).evaluate(new Object())); try { PredicateUtils.nullIsExceptionPredicate(PredicateUtils.truePredicate()).evaluate(null); } catch (PredicateException ex) { return; } fail(); } public void testNullIsExceptionPredicateEx1() { try { PredicateUtils.nullIsExceptionPredicate(null); } catch (IllegalArgumentException ex) { return; } fail(); } // nullIsTrue //------------------------------------------------------------------ public void testNullIsTruePredicate() { assertEquals(true, PredicateUtils.nullIsTruePredicate(PredicateUtils.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.nullIsTruePredicate(PredicateUtils.truePredicate()).evaluate(new Object())); assertEquals(false, PredicateUtils.nullIsTruePredicate(PredicateUtils.falsePredicate()).evaluate(new Object())); } public void testNullIsTruePredicateEx1() { try { PredicateUtils.nullIsTruePredicate(null); } catch (IllegalArgumentException ex) { return; } fail(); } // nullIsFalse //------------------------------------------------------------------ public void testNullIsFalsePredicate() { assertEquals(false, PredicateUtils.nullIsFalsePredicate(PredicateUtils.truePredicate()).evaluate(null)); assertEquals(true, PredicateUtils.nullIsFalsePredicate(PredicateUtils.truePredicate()).evaluate(new Object())); assertEquals(false, PredicateUtils.nullIsFalsePredicate(PredicateUtils.falsePredicate()).evaluate(new Object())); } public void testNullIsFalsePredicateEx1() { try { PredicateUtils.nullIsFalsePredicate(null); } catch (IllegalArgumentException ex) { return; } fail(); } } 1.1 jakarta-commons/lang/src/test/org/apache/commons/lang/functor/TestTransformerUtils.java Index: TestTransformerUtils.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.commons.lang.functor; import java.io.Serializable; 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; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.lang.SerializationException; import org.apache.commons.lang.functor.Closure; import org.apache.commons.lang.functor.ClosureUtils; import org.apache.commons.lang.functor.Factory; import org.apache.commons.lang.functor.FactoryUtils; import org.apache.commons.lang.functor.Predicate; import org.apache.commons.lang.functor.PredicateUtils; /** * Tests the org.apache.commons.lang.functor.TransformerUtils class. * * @author <a href="mailto:scolebourne@;joda.org">Stephen Colebourne</a> * @version $Id: TestTransformerUtils.java,v 1.1 2002/11/05 16:45:13 bayard Exp $ */ public class TestTransformerUtils extends junit.framework.TestCase { private static final Object cObject = new Object(); private static final Object cString = "Hello"; private static final Object cInteger = new Integer(6); /** * Construct */ public TestTransformerUtils(String name) { super(name); } /** * Return class aa a test suite. */ public static Test suite() { return new TestSuite(TestTransformerUtils.class); } /** * Set up instance variables required by this test case. */ public void setUp() { } /** * Tear down instance variables required by this test case. */ public void tearDown() { } // exceptionTransformer //------------------------------------------------------------------ public void testExceptionTransformer() { assertNotNull(TransformerUtils.exceptionTransformer()); assertSame(TransformerUtils.exceptionTransformer(), TransformerUtils.exceptionTransformer()); try { TransformerUtils.exceptionTransformer().transform(null); } catch (TransformerException ex) { try { TransformerUtils.exceptionTransformer().transform(cString); } catch (TransformerException ex2) { return; } } fail(); } // nullTransformer //------------------------------------------------------------------ public void testNullTransformer() { assertNotNull(TransformerUtils.nullTransformer()); assertSame(TransformerUtils.nullTransformer(), TransformerUtils.nullTransformer()); assertEquals(null, TransformerUtils.nullTransformer().transform(null)); assertEquals(null, TransformerUtils.nullTransformer().transform(cObject)); assertEquals(null, TransformerUtils.nullTransformer().transform(cString)); assertEquals(null, TransformerUtils.nullTransformer().transform(cInteger)); } // nopTransformer //------------------------------------------------------------------ public void testNopTransformer() { assertNotNull(TransformerUtils.nullTransformer()); assertSame(TransformerUtils.nullTransformer(), TransformerUtils.nullTransformer()); assertEquals(null, TransformerUtils.nopTransformer().transform(null)); assertEquals(cObject, TransformerUtils.nopTransformer().transform(cObject)); assertEquals(cString, TransformerUtils.nopTransformer().transform(cString)); assertEquals(cInteger, TransformerUtils.nopTransformer().transform(cInteger)); } // constantTransformer //------------------------------------------------------------------ public void testConstantTransformer() { assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(null)); assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cObject)); assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cString)); assertEquals(cObject, TransformerUtils.constantTransformer(cObject).transform(cInteger)); } // cloneTransformer //------------------------------------------------------------------ public void testCloneTransformer() { assertEquals(null, TransformerUtils.cloneTransformer().transform(null)); assertEquals(cString, TransformerUtils.cloneTransformer().transform(cString)); assertEquals(cInteger, TransformerUtils.cloneTransformer().transform(cInteger)); try { assertEquals(cObject, TransformerUtils.cloneTransformer().transform(cObject)); } catch (TransformerException ex) { return; } fail(); } // mapTransformer //------------------------------------------------------------------ public void testMapTransformer() { Map map = new HashMap(); map.put(null, new Integer(0)); map.put(cObject, new Integer(1)); map.put(cString, new Integer(2)); assertEquals(new Integer(0), TransformerUtils.mapTransformer(map).transform(null)); 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)); } // commandTransformer //------------------------------------------------------------------ public void testClosureTransformer() { assertEquals(null, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(null)); assertEquals(cObject, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cObject)); assertEquals(cString, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cString)); assertEquals(cInteger, TransformerUtils.asTransformer(ClosureUtils.nopClosure()).transform(cInteger)); try { TransformerUtils.asTransformer((Closure) null); } catch (IllegalArgumentException ex) { return; } fail(); } // predicateTransformer //------------------------------------------------------------------ public void testPredicateTransformer() { assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(null)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cObject)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cString)); assertEquals(Boolean.TRUE, TransformerUtils.asTransformer(PredicateUtils.truePredicate()).transform(cInteger)); try { TransformerUtils.asTransformer((Predicate) null); } catch (IllegalArgumentException ex) { return; } fail(); } // factoryTransformer //------------------------------------------------------------------ public void testFactoryTransformer() { assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(null)); assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cObject)); assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cString)); assertEquals(null, TransformerUtils.asTransformer(FactoryUtils.nullFactory()).transform(cInteger)); try { TransformerUtils.asTransformer((Factory) null); } catch (IllegalArgumentException ex) { return; } fail(); } // chainedTransformer //------------------------------------------------------------------ public void testChainedTransformer() { Transformer a = TransformerUtils.constantTransformer("A"); Transformer b = TransformerUtils.constantTransformer("B"); assertEquals("A", TransformerUtils.chainedTransformer(b, a).transform(null)); assertEquals("B", TransformerUtils.chainedTransformer(a, b).transform(null)); assertEquals("A", TransformerUtils.chainedTransformer(new Transformer[] {b, a}).transform(null)); Collection coll = new ArrayList(); coll.add(b); coll.add(a); assertEquals("A", TransformerUtils.chainedTransformer(coll).transform(null)); } public void testChainedTransformerEx1a() { try { TransformerUtils.chainedTransformer(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedTransformerEx1b() { try { TransformerUtils.chainedTransformer((Transformer[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedTransformerEx1c() { try { TransformerUtils.chainedTransformer((Collection) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedTransformerEx2() { try { TransformerUtils.chainedTransformer(new Transformer[0]); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedTransformerEx3() { try { TransformerUtils.chainedTransformer(new Transformer[] {null, null}); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedTransformerEx4() { try { TransformerUtils.chainedTransformer(Collections.EMPTY_LIST); } catch (IllegalArgumentException ex) { return; } fail(); } public void testChainedTransformerEx5() { try { Collection coll = new ArrayList(); coll.add(null); coll.add(null); TransformerUtils.chainedTransformer(coll); } catch (IllegalArgumentException ex) { return; } fail(); } // switchTransformer //------------------------------------------------------------------ public void testSwitchTransformer() { Transformer a = TransformerUtils.constantTransformer("A"); Transformer b = TransformerUtils.constantTransformer("B"); Transformer c = TransformerUtils.constantTransformer("C"); assertEquals("A", TransformerUtils.switchTransformer(PredicateUtils.truePredicate(), a, b).transform(null)); assertEquals("B", TransformerUtils.switchTransformer(PredicateUtils.falsePredicate(), a, b).transform(null)); assertEquals(null, TransformerUtils.switchTransformer( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Transformer[] {a, b}).transform("WELL")); assertEquals("A", TransformerUtils.switchTransformer( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Transformer[] {a, b}).transform("HELLO")); assertEquals("B", TransformerUtils.switchTransformer( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Transformer[] {a, b}).transform("THERE")); assertEquals("C", TransformerUtils.switchTransformer( new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")}, new Transformer[] {a, b}, c).transform("WELL")); Map map = new HashMap(); map.put(PredicateUtils.equalPredicate("HELLO"), a); map.put(PredicateUtils.equalPredicate("THERE"), b); assertEquals(null, TransformerUtils.switchTransformer(map).transform("WELL")); assertEquals("A", TransformerUtils.switchTransformer(map).transform("HELLO")); assertEquals("B", TransformerUtils.switchTransformer(map).transform("THERE")); map.put(null, c); assertEquals("C", TransformerUtils.switchTransformer(map).transform("WELL")); } public void testSwitchTransformerEx1a() { try { TransformerUtils.switchTransformer(null, null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx1b() { try { TransformerUtils.switchTransformer((Predicate[]) null, (Transformer[]) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx1c() { try { TransformerUtils.switchTransformer((Map) null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx2() { try { TransformerUtils.switchTransformer(new Predicate[0], new Transformer[0]); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx3() { try { TransformerUtils.switchTransformer(new Predicate[2], new Transformer[2]); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx4() { try { TransformerUtils.switchTransformer(Collections.EMPTY_MAP); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx5() { try { Map map = new HashMap(); map.put(null, null); map.put(null, null); TransformerUtils.switchTransformer(map); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchTransformerEx6() { try { TransformerUtils.switchTransformer(new Predicate[2], new Transformer[1]); } catch (IllegalArgumentException ex) { return; } fail(); } // switchMapTransformer //------------------------------------------------------------------ public void testSwitchMapTransformer() { Transformer a = TransformerUtils.constantTransformer("A"); Transformer b = TransformerUtils.constantTransformer("B"); Transformer c = TransformerUtils.constantTransformer("C"); Map map = new HashMap(); map.put("HELLO", a); map.put("THERE", b); assertEquals(null, TransformerUtils.switchMapTransformer(map).transform("WELL")); assertEquals("A", TransformerUtils.switchMapTransformer(map).transform("HELLO")); assertEquals("B", TransformerUtils.switchMapTransformer(map).transform("THERE")); map.put(null, c); assertEquals("C", TransformerUtils.switchMapTransformer(map).transform("WELL")); } public void testSwitchMapTransformerEx1() { try { TransformerUtils.switchMapTransformer(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testSwitchMapTransformerEx2() { try { TransformerUtils.switchMapTransformer(Collections.EMPTY_MAP); } catch (IllegalArgumentException ex) { return; } fail(); } // invokerTransformer //------------------------------------------------------------------ public void testInvokerTransformer() { List list = new ArrayList(); assertEquals(new Integer(0), TransformerUtils.invokerTransformer("size").transform(list)); list.add(new Object()); assertEquals(new Integer(1), TransformerUtils.invokerTransformer("size").transform(list)); assertEquals(null, TransformerUtils.invokerTransformer("size").transform(null)); } public void testInvokerTransformerEx1() { try { TransformerUtils.invokerTransformer(null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testInvokerTransformerEx3() { try { TransformerUtils.invokerTransformer("noSuchMethod").transform(new Object()); } catch (TransformerException ex) { return; } fail(); } // invokerTransformer2 //------------------------------------------------------------------ public void testInvokerTransformer2() { List list = new ArrayList(); assertEquals(Boolean.FALSE, TransformerUtils.invokerTransformer( "contains", new Class[] {Object.class}, new Object[] {cString}).transform(list)); list.add(cString); assertEquals(Boolean.TRUE, TransformerUtils.invokerTransformer( "contains", new Class[] {Object.class}, new Object[] {cString}).transform(list)); assertEquals(null, TransformerUtils.invokerTransformer( "contains", new Class[] {Object.class}, new Object[] {cString}).transform(null)); } public void testInvokerTransformer2Ex1() { try { TransformerUtils.invokerTransformer(null, null, null); } catch (IllegalArgumentException ex) { return; } fail(); } public void testInvokerTransformer2Ex3() { try { TransformerUtils.invokerTransformer( "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).transform(new Object()); } catch (TransformerException ex) { return; } fail(); } }
-- To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>