Author: kinow Date: Thu Jun 28 02:59:23 2012 New Revision: 1354803 URL: http://svn.apache.org/viewvc?rev=1354803&view=rev Log: Tests for functor core algorithms. Work related to FUNCTOR-12.
A few algorithm classes weren't using BaseFunctorTest. It was changed in this commit. Some further work was needed to make test-objects serializable. And an extra test was included in BaseFunctorTest, to compare (using equals) a functor with a non-functor object. Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/BaseFunctorTest.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoUntil.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoWhile.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestInPlaceTransform.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRecursiveEvaluation.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRemoveMatching.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRetainMatching.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestUntilDo.java commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestWhileDo.java Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/BaseFunctorTest.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/BaseFunctorTest.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/BaseFunctorTest.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/BaseFunctorTest.java Thu Jun 28 02:59:23 2012 @@ -55,6 +55,8 @@ public abstract class BaseFunctorTest { } else { assertTrue("equals must be symmetric",! obj2.equals(obj)); } + + assertTrue("a functor is not equal to an integer", ! obj.equals(new Integer(1))); } @Test Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoUntil.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoUntil.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoUntil.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoUntil.java Thu Jun 28 02:59:23 2012 @@ -17,35 +17,28 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.Serializable; + +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.Procedure; import org.apache.commons.functor.core.Offset; -import org.apache.commons.functor.core.algorithm.DoUntil; import org.junit.Test; /** * Tests {@link DoUntil} algorithm. */ -public class TestDoUntil { +public class TestDoUntil extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { + // Functor Testing Framework + // ------------------------------------------------------------------------ + + @Override + protected Object makeFunctor() throws Exception { Counter counter = new Counter(); - Object obj = new DoUntil(counter, new Offset(10)); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new DoUntil(counter, new Offset(10)); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + return new DoUntil(counter, new Offset(10)); } - + @Test public void testDoUntil() { for(int i=0;i<3;++i){ @@ -58,11 +51,32 @@ public class TestDoUntil { // Classes // ------------------------------------------------------------------------ - static class Counter implements Procedure { + static class Counter implements Procedure, Serializable { + private static final long serialVersionUID = 1L; public void run() { count++; } public int count = 0; + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if (obj == null || !obj.getClass().equals(getClass())) { + return false; + } + Counter that = (Counter)obj; + return this.count == that.count; + } + + @Override + public int hashCode() { + int hash = "Counter".hashCode(); + hash <<= 2; + hash ^= this.count; + return hash; + } } } Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoWhile.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoWhile.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoWhile.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestDoWhile.java Thu Jun 28 02:59:23 2012 @@ -17,35 +17,28 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.Serializable; + +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.Procedure; import org.apache.commons.functor.core.Limit; -import org.apache.commons.functor.core.algorithm.DoWhile; import org.junit.Test; /** * Tests {@link DoWhile} algorithm. */ -public class TestDoWhile { +public class TestDoWhile extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { + // Functor Testing Framework + // ------------------------------------------------------------------------ + + @Override + protected Object makeFunctor() throws Exception { Counter counter = new Counter(); - Object obj = new DoWhile(counter, new Limit(10)); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new DoWhile(counter, new Limit(10)); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + return new DoWhile(counter, new Limit(10)); } - + @Test public void testDoWhile() { for(int i=0;i<3;i++){ @@ -58,11 +51,32 @@ public class TestDoWhile { // Classes // ------------------------------------------------------------------------ - static class Counter implements Procedure { + static class Counter implements Procedure, Serializable { + private static final long serialVersionUID = 1L; public void run() { count++; } public int count = 0; + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if (obj == null || !obj.getClass().equals(getClass())) { + return false; + } + Counter that = (Counter)obj; + return this.count == that.count; + } + + @Override + public int hashCode() { + int hash = "Counter".hashCode(); + hash <<= 2; + hash ^= this.count; + return hash; + } } } Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldLeft.java Thu Jun 28 02:59:23 2012 @@ -17,45 +17,27 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.Serializable; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.BinaryFunction; -import org.apache.commons.functor.core.algorithm.FoldLeft; import org.apache.commons.functor.generator.IteratorToGeneratorAdapter; import org.junit.Test; /** * Tests {@link FoldLeft} algorithm. */ -public class TestFoldLeft { +public class TestFoldLeft extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { - Object obj = new FoldLeft<Integer>(new BinaryFunction<Integer, Integer, Integer>() { - public Integer evaluate(Integer a, Integer b) { - return new Integer(a + b); - } - }); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new FoldLeft<Integer>(new BinaryFunction<Integer, Integer, Integer>() { - public Integer evaluate(Integer a, Integer b) { - return new Integer(a + b); - } - }); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + @Override + protected Object makeFunctor() throws Exception { + return new FoldLeft<Integer>(new Sum()); } - + @Test public void testFoldLeft() { FoldLeft<Integer> foldLeft = new FoldLeft<Integer>(new BinaryFunction<Integer, Integer, Integer>() { @@ -65,6 +47,7 @@ public class TestFoldLeft { }); assertEquals(new Integer(sum), foldLeft.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()))); assertEquals(new Integer(sum), foldLeft.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()), new Integer(0))); + assertEquals(new Integer(0), foldLeft.evaluate(IteratorToGeneratorAdapter.adapt(new ArrayList<Integer>(0).iterator()), new Integer(0)), new Integer(0)); } // Attributes @@ -72,4 +55,28 @@ public class TestFoldLeft { private List<Integer> list = Arrays.asList(0,1,2); private int sum = 3; + // Classes + // ------------------------------------------------------------------------ + + static class Sum implements BinaryFunction<Integer, Integer, Integer>, Serializable { + private static final long serialVersionUID = 1L; + + public Integer evaluate(Integer a, Integer b) { + return new Integer(a + b); + } + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + return obj != null && obj.getClass().equals(this.getClass()); + } + + @Override + public int hashCode() { + return "Sum".hashCode(); + } + } + } Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestFoldRight.java Thu Jun 28 02:59:23 2012 @@ -17,61 +17,67 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.Serializable; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.BinaryFunction; -import org.apache.commons.functor.core.algorithm.FoldRight; import org.apache.commons.functor.generator.IteratorToGeneratorAdapter; import org.junit.Test; /** * Tests {@link FoldRight} algorithm. */ -public class TestFoldRight { +public class TestFoldRight extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { - Object obj = new FoldRight<Object>(new BinaryFunction<Object, Object, Object>() { - public Object evaluate(Object left, Object right) { - StringBuffer buf = left instanceof StringBuffer ? (StringBuffer) left : new StringBuffer().append(left); - return buf.append(right); - } - }); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new FoldRight<Object>(new BinaryFunction<Object, Object, Object>() { - public Object evaluate(Object left, Object right) { - StringBuffer buf = left instanceof StringBuffer ? (StringBuffer) left : new StringBuffer().append(left); - return buf.append(right); - } - }); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + @Override + protected Object makeFunctor() throws Exception { + return new FoldRight<Object>(new StringConcatenator()); } - + @Test public void testFoldRight() { FoldRight<Object> foldRight = new FoldRight<Object>(new BinaryFunction<Object, Object, Object>() { public Object evaluate(Object left, Object right) { - StringBuffer buf = left instanceof StringBuffer ? (StringBuffer) left : new StringBuffer().append(left); + StringBuilder buf = left instanceof StringBuilder ? (StringBuilder) left : new StringBuilder().append(left); return buf.append(right); } }); assertEquals("0123456789", foldRight.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator())).toString()); assertEquals("0123456789x", foldRight.evaluate(IteratorToGeneratorAdapter.adapt(list.iterator()), "x").toString()); + assertEquals("x", foldRight.evaluate(IteratorToGeneratorAdapter.adapt(new ArrayList<Object>().iterator()), "x").toString()); } // Attributes // ------------------------------------------------------------------------ private List<Object> list = Arrays.<Object>asList(0,1,2,3,4,5,6,7,8,9); + // Classes + // ------------------------------------------------------------------------ + + static class StringConcatenator implements BinaryFunction<Object, Object, Object>, Serializable { + private static final long serialVersionUID = 1L; + + public Object evaluate(Object left, Object right) { + StringBuilder buf = left instanceof StringBuilder ? (StringBuilder) left : new StringBuilder().append(left); + return buf.append(right); + } + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + return obj != null && obj instanceof StringConcatenator; + } + + @Override + public int hashCode() { + return "StringConcatenator".hashCode(); + } + } + } Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestInPlaceTransform.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestInPlaceTransform.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestInPlaceTransform.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestInPlaceTransform.java Thu Jun 28 02:59:23 2012 @@ -58,7 +58,7 @@ public class TestInPlaceTransform extend @Override protected Object makeFunctor() throws Exception { - return new InPlaceTransform<Integer>(); + return InPlaceTransform.instance(); } @Test Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestIndexOfInGenerator.java Thu Jun 28 02:59:23 2012 @@ -25,7 +25,6 @@ import org.apache.commons.functor.BaseFu import org.apache.commons.functor.UnaryPredicate; import org.apache.commons.functor.adapter.LeftBoundPredicate; import org.apache.commons.functor.core.IsEqual; -import org.apache.commons.functor.core.algorithm.IndexOfInGenerator; import org.apache.commons.functor.generator.IteratorToGeneratorAdapter; import org.junit.Test; @@ -36,7 +35,7 @@ public class TestIndexOfInGenerator exte @Override protected Object makeFunctor() throws Exception { - return new IndexOfInGenerator<Integer>(); + return IndexOfInGenerator.instance(); } @Test Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRecursiveEvaluation.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRecursiveEvaluation.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRecursiveEvaluation.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRecursiveEvaluation.java Thu Jun 28 02:59:23 2012 @@ -17,31 +17,22 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.Serializable; + +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.Function; -import org.apache.commons.functor.core.algorithm.RecursiveEvaluation; import org.junit.Test; /** * Tests {@link RecursiveEvaluation} algorithm. */ -public class TestRecursiveEvaluation { +public class TestRecursiveEvaluation extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { - Object obj = new RecursiveEvaluation(new RecFunc(0, false)); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new RecursiveEvaluation(new RecFunc(0, false)); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + @Override + protected Object makeFunctor() throws Exception { + return new RecursiveEvaluation(new RecFunc(0, false)); } @Test @@ -54,10 +45,28 @@ public class TestRecursiveEvaluation { Function<Object> func = (Function) new RecursiveEvaluation(new RecFunc(0, true)).evaluate(); assertEquals(new Integer(5), func.evaluate()); } + + @Test + public void testConstructors() { + try { + new RecursiveEvaluation(new RecFunc(0, false), java.lang.Integer.class); + fail("Not supposed to get here."); + } catch(IllegalArgumentException e) {} + try { + new RecursiveEvaluation(null); + fail("Not supposed to get here."); + } catch(NullPointerException e) {} + } + // Classes + // ------------------------------------------------------------------------ + /** Recursive function for test. */ - class RecFunc implements Function<Object> { - int times = 0; boolean returnFunc = false; + static class RecFunc implements Function<Object>, Serializable { + private static final long serialVersionUID = 1L; + + int times = 0; + boolean returnFunc = false; public RecFunc(int times, boolean returnFunc) { this.times = times; @@ -69,16 +78,59 @@ public class TestRecursiveEvaluation { return new RecFunc(++times, returnFunc); } else { if (returnFunc) { - return new Function<Object>() { - public Object evaluate() { - return new Integer(times); - } - }; + return new InnerFunction(times); } else { return new Integer(times); } } } + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if(obj == null || obj.getClass() != this.getClass()) { + return false; + } + return this.times == ((RecFunc)obj).times && this.returnFunc == ((RecFunc)obj).returnFunc; + } + + @Override + public int hashCode() { + return "RecFunc".hashCode() << 2 ^ times; + } } + + /** Inner function called from recursive function */ + static class InnerFunction implements Function<Object>, Serializable { + private static final long serialVersionUID = 1L; + + private int times; + + public InnerFunction(int times) { + this.times = times; + } + + public Object evaluate() { + return new Integer(times); + } + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if(obj == null || ! (obj instanceof InnerFunction)) { + return false; + } + return this.times == ((InnerFunction)obj).times; + } + + @Override + public int hashCode() { + return "InnerFunction".hashCode() << 2 ^ times; + } + }; } Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRemoveMatching.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRemoveMatching.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRemoveMatching.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRemoveMatching.java Thu Jun 28 02:59:23 2012 @@ -60,7 +60,7 @@ public class TestRemoveMatching extends @Override protected Object makeFunctor() throws Exception { - return new RemoveMatching<Integer>(); + return RemoveMatching.instance(); } @Test Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRetainMatching.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRetainMatching.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRetainMatching.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestRetainMatching.java Thu Jun 28 02:59:23 2012 @@ -60,7 +60,7 @@ public class TestRetainMatching extends @Override protected Object makeFunctor() throws Exception { - return new RetainMatching<Integer>(); + return RetainMatching.instance(); } @Test Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestUntilDo.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestUntilDo.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestUntilDo.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestUntilDo.java Thu Jun 28 02:59:23 2012 @@ -17,35 +17,28 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.Serializable; + +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.Procedure; import org.apache.commons.functor.core.Offset; -import org.apache.commons.functor.core.algorithm.UntilDo; import org.junit.Test; /** * Tests {@link UntilDo} algorithm. */ -public class TestUntilDo { +public class TestUntilDo extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { + // Functor Testing Framework + // ------------------------------------------------------------------------ + + @Override + protected Object makeFunctor() throws Exception { Counter counter = new Counter(); - Object obj = new UntilDo(new Offset(10), counter); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new UntilDo(new Offset(10), counter); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + return new UntilDo(new Offset(10), counter); } - + @Test public void testUntilDo() { for (int i=0;i<3;i++){ @@ -58,11 +51,32 @@ public class TestUntilDo { // Classes // ------------------------------------------------------------------------ - static class Counter implements Procedure { + static class Counter implements Procedure, Serializable { + private static final long serialVersionUID = 1L; public void run() { count++; } public int count = 0; + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if (obj == null || !obj.getClass().equals(getClass())) { + return false; + } + Counter that = (Counter)obj; + return this.count == that.count; + } + + @Override + public int hashCode() { + int hash = "Counter".hashCode(); + hash <<= 2; + hash ^= this.count; + return hash; + } } } Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestWhileDo.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestWhileDo.java?rev=1354803&r1=1354802&r2=1354803&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestWhileDo.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/core/algorithm/TestWhileDo.java Thu Jun 28 02:59:23 2012 @@ -17,36 +17,29 @@ package org.apache.commons.functor.core.algorithm; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.Serializable; + +import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.Procedure; import org.apache.commons.functor.core.Limit; import org.apache.commons.functor.core.Offset; -import org.apache.commons.functor.core.algorithm.WhileDo; import org.junit.Test; /** * Tests {@link WhileDo} algorithm. */ -public class TestWhileDo { +public class TestWhileDo extends BaseFunctorTest { - @Test - public final void testObjectEquals() throws Exception { + // Functor Testing Framework + // ------------------------------------------------------------------------ + + @Override + protected Object makeFunctor() throws Exception { Counter counter = new Counter(); - Object obj = new WhileDo(new Offset(10), counter); - assertEquals("equals must be reflexive",obj,obj); - assertEquals("hashCode must be reflexive",obj.hashCode(),obj.hashCode()); - assertTrue(! obj.equals(null) ); // should be able to compare to null - - Object obj2 = new WhileDo(new Offset(10), counter); - if (obj.equals(obj2)) { - assertEquals("equals implies hash equals",obj.hashCode(),obj2.hashCode()); - assertEquals("equals must be symmetric",obj2,obj); - } else { - assertTrue("equals must be symmetric",! obj2.equals(obj)); - } + return new WhileDo(new Offset(10), counter); } - + @Test public void testWhileDo() { for (int i=0;i<3;i++){ @@ -59,11 +52,32 @@ public class TestWhileDo { // Classes // ------------------------------------------------------------------------ - static class Counter implements Procedure { + static class Counter implements Procedure, Serializable { + private static final long serialVersionUID = 1L; public void run() { count++; } public int count = 0; + + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if (obj == null || !obj.getClass().equals(getClass())) { + return false; + } + Counter that = (Counter)obj; + return this.count == that.count; + } + + @Override + public int hashCode() { + int hash = "Counter".hashCode(); + hash <<= 2; + hash ^= this.count; + return hash; + } } }