Author: simonetripodi Date: Thu May 31 15:25:08 2012 New Revision: 1344774 URL: http://svn.apache.org/viewvc?rev=1344774&view=rev Log: [FUNCTOR-7] Tests for generators - patch submitted by Bruno P. Kinoshita
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java (with props) commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java (with props) commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java (with props) commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java (with props) commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java (with props) commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java (with props) Modified: commons/proper/functor/trunk/src/changes/changes.xml commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java Modified: commons/proper/functor/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/changes/changes.xml?rev=1344774&r1=1344773&r2=1344774&view=diff ============================================================================== --- commons/proper/functor/trunk/src/changes/changes.xml (original) +++ commons/proper/functor/trunk/src/changes/changes.xml Thu May 31 15:25:08 2012 @@ -23,6 +23,9 @@ </properties> <body> <release version="1.0" date="2012-??-??" description="First release."> + <action dev="simonetripodi" issue="FUNCTOR-7" due-to="Bruno P. Kinoshita"> + Tests for generators + </action> <action dev="simonetripodi" issue="FUNCTOR-5" due-to="Bruno P. Kinoshita"> Complete the javadoc description of Limit </action> Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java?rev=1344774&view=auto ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java (added) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java Thu May 31 15:25:08 2012 @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.functor.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.functor.UnaryPredicate; +import org.apache.commons.functor.UnaryProcedure; +import org.apache.commons.functor.generator.util.IntegerRange; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the Filtered Generator class. + * @author Bruno P. Kinoshita (brunodepau...@yahoo.com.br) + */ +public class TestFilteredGenerator +{ + + @Before + public void setUp() throws Exception { + wrappedGenerator = new IntegerRange(1, 10); + filteredGenerator = new FilteredGenerator<Integer>(wrappedGenerator, isEven); + } + + @After + public void tearDown() { + wrappedGenerator = null; + isEven = null; + filteredGenerator = null; + } + + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testConstructorProhibitsNull() { + try { + new FilteredGenerator<Integer>(filteredGenerator, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new FilteredGenerator<Integer>(null, isEven); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new FilteredGenerator<Integer>(null, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + } + + @Test + public void testEquals() { + Generator<Integer> anotherGenerate = new FilteredGenerator<Integer>(new IntegerRange(1, 10), isEven); + assertEquals(filteredGenerator, filteredGenerator); + assertEquals(filteredGenerator, anotherGenerate); + assertTrue(!filteredGenerator.equals((FilteredGenerator<Integer>)null)); + + Generator<Integer> aGenerateWithADifferentPredicate = new FilteredGenerator<Integer>( + new IntegerRange(1, 10), new UnaryPredicate<Integer>() { + public boolean test(Integer obj) { + return obj % 2 == 0; + } + }); + + assertTrue(!filteredGenerator.equals(aGenerateWithADifferentPredicate)); + + Generator<Integer> aGenerateWithADifferentWrapped = new FilteredGenerator<Integer>(new IntegerRange(1,11), isEven); + assertTrue(!filteredGenerator.equals(aGenerateWithADifferentWrapped)); + } + + @Test + public void testHashcode() { + assertEquals(filteredGenerator.hashCode(), filteredGenerator.hashCode()); + assertEquals(filteredGenerator.hashCode(), new FilteredGenerator<Integer>(wrappedGenerator, isEven).hashCode()); + assertFalse(filteredGenerator.hashCode() == new FilteredGenerator<Integer>(wrappedGenerator, isEven) { + @Override + protected Generator<? extends Integer> getWrappedGenerator() { + return null; + } + }.hashCode()); + } + + @Test + public void testGenerate() { + final List<Integer> evenNumbers = new ArrayList<Integer>(); + filteredGenerator.run(new UnaryProcedure<Integer>() { + public void run(Integer obj) { + evenNumbers.add(obj); + } + }); + assertEquals(4, evenNumbers.size()); + + List<Integer> expected = Arrays.asList(2, 4, 6, 8); + assertEquals(expected, evenNumbers); + } + + // Attributes + // ------------------------------------------------------------------------ + private Generator<Integer> wrappedGenerator = null; + private UnaryPredicate<Integer> isEven = new UnaryPredicate<Integer>() + { + public boolean test( Integer obj ) { + return obj % 2 == 0; + } + }; + private Generator<Integer> filteredGenerator = null; + +} Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestFilteredGenerator.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java?rev=1344774&view=auto ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java (added) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java Thu May 31 15:25:08 2012 @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.functor.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.commons.functor.UnaryPredicate; +import org.apache.commons.functor.generator.util.IntegerRange; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the Generate Until class. + * @author Bruno P. Kinoshita (brunodepau...@yahoo.com.br) + */ +public class TestGenerateUntil +{ + + @Before + public void setUp() throws Exception { + wrappedGenerator = new IntegerRange(1, 10); + generateUntil = new GenerateUntil<Integer>(wrappedGenerator, isMoreThanFive); + } + + @After + public void tearDown() { + wrappedGenerator = null; + isMoreThanFive = null; + generateUntil = null; + } + + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testConstructorProhibitsNull() { + try { + new GenerateUntil<Integer>(generateUntil, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new GenerateUntil<Integer>(null, isMoreThanFive); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new GenerateUntil<Integer>(null, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + } + + @Test + public void testEquals() { + Generator<Integer> anotherGenerate = new GenerateUntil<Integer>(new IntegerRange(1, 10), isMoreThanFive); + assertEquals(generateUntil, generateUntil); + assertEquals(generateUntil, anotherGenerate); + assertTrue(!generateUntil.equals((GenerateUntil<Integer>)null)); + + Generator<Integer> aGenerateWithADifferentPredicate = new GenerateUntil<Integer>( + new IntegerRange(1, 10), + new UnaryPredicate<Integer>() { + public boolean test(Integer obj) { + return obj > FIVE; + } + }); + assertTrue(!generateUntil.equals(aGenerateWithADifferentPredicate)); + + Generator<Integer> aGenerateWithADifferentWrapped = new GenerateUntil<Integer>(new IntegerRange(1,2), isMoreThanFive); + assertTrue(!generateUntil.equals(aGenerateWithADifferentWrapped)); + } + + @Test + public void testHashcode() { + assertEquals(generateUntil.hashCode(), generateUntil.hashCode()); + assertEquals(generateUntil.hashCode(), new GenerateUntil<Integer>(wrappedGenerator, isMoreThanFive).hashCode()); + assertFalse(generateUntil.hashCode() == new GenerateUntil<Integer>(wrappedGenerator, isMoreThanFive) { + @Override + protected Generator<? extends Integer> getWrappedGenerator() + { + return null; + } + }.hashCode()); + } + + // Attributes + // ------------------------------------------------------------------------ + private static final Integer FIVE = new Integer(5); + + private Generator<Integer> wrappedGenerator = null; + private UnaryPredicate<Integer> isMoreThanFive = new UnaryPredicate<Integer>() { + public boolean test( Integer obj ) { + return obj > FIVE; + } + }; + private Generator<Integer> generateUntil = null; +} Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateUntil.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java?rev=1344774&view=auto ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java (added) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java Thu May 31 15:25:08 2012 @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.functor.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.commons.functor.UnaryPredicate; +import org.apache.commons.functor.generator.util.IntegerRange; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the Generate While class. + * @author Bruno P. Kinoshita (brunodepau...@yahoo.com.br) + */ +public class TestGenerateWhile +{ + + @Before + public void setUp() throws Exception { + wrappedGenerator = new IntegerRange(1, 10); + generateWhile = new GenerateWhile<Integer>(wrappedGenerator, isLessThanFive); + } + + @After + public void tearDown() { + wrappedGenerator = null; + isLessThanFive = null; + generateWhile = null; + } + + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testConstructorProhibitsNull() { + try { + new GenerateWhile<Integer>(generateWhile, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new GenerateWhile<Integer>(null, isLessThanFive); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new GenerateWhile<Integer>(null, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + } + + @Test + public void testEquals() { + Generator<Integer> anotherGenerate = new GenerateWhile<Integer>(new IntegerRange(1, 10), isLessThanFive); + assertEquals(generateWhile, generateWhile); + assertEquals(generateWhile, anotherGenerate); + assertTrue(!generateWhile.equals((GenerateWhile<Integer>)null)); + + Generator<Integer> aGenerateWithADifferentPredicate = new GenerateWhile<Integer>( + new IntegerRange(1, 10), new UnaryPredicate<Integer>() { + public boolean test(Integer obj) { + return obj < FIVE; + } + }); + + assertTrue(!generateWhile.equals(aGenerateWithADifferentPredicate)); + + Generator<Integer> aGenerateWithADifferentWrapped = new GenerateWhile<Integer>(new IntegerRange(1,11), isLessThanFive); + assertTrue(!generateWhile.equals(aGenerateWithADifferentWrapped)); + } + + @Test + public void testHashcode() { + assertEquals(generateWhile.hashCode(), generateWhile.hashCode()); + assertEquals(generateWhile.hashCode(), new GenerateWhile<Integer>(wrappedGenerator, isLessThanFive).hashCode()); + assertFalse(generateWhile.hashCode() == new GenerateWhile<Integer>(wrappedGenerator, isLessThanFive) { + @Override + protected Generator<? extends Integer> getWrappedGenerator() { + return null; + } + }.hashCode()); + } + + // Attributes + // ------------------------------------------------------------------------ + private static final Integer FIVE = new Integer(5); + + private Generator<Integer> wrappedGenerator = null; + private UnaryPredicate<Integer> isLessThanFive = new UnaryPredicate<Integer>() + { + public boolean test( Integer obj ) { + return obj < FIVE; + } + }; + private Generator<Integer> generateWhile = null; + +} Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestGenerateWhile.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java?rev=1344774&r1=1344773&r2=1344774&view=diff ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java (original) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java Thu May 31 15:25:08 2012 @@ -31,6 +31,7 @@ import org.junit.Before; import org.junit.Test; /** + * Tests the Iterator to Generator Adapter class. * @version $Revision$ $Date$ * @author Rodney Waldhoff */ Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java?rev=1344774&view=auto ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java (added) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java Thu May 31 15:25:08 2012 @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.functor.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.functor.UnaryFunction; +import org.apache.commons.functor.UnaryProcedure; +import org.apache.commons.functor.generator.util.IntegerRange; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the Transformed Generator class. + * @author Bruno P. Kinoshita (brunodepau...@yahoo.com.br) + */ +public class TestTransformedGenerator +{ + + @Before + public void setUp() throws Exception { + wrappedGenerator = new IntegerRange(1, 10); + sumsTwoGenerator = new TransformedGenerator<Integer, Integer>(wrappedGenerator, sumsTwo); + } + + @After + public void tearDown() { + wrappedGenerator = null; + sumsTwo = null; + sumsTwoGenerator = null; + } + + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testConstructorProhibitsNull() { + try { + new TransformedGenerator<Integer, Integer>(null, sumsTwo); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new TransformedGenerator<Integer, Integer>(wrappedGenerator, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new TransformedGenerator<Integer, Integer>(null, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + } + + @Test + public void testEquals() { + TransformedGenerator<Integer, Integer> anotherTransformedGenerator = + new TransformedGenerator<Integer, Integer>(wrappedGenerator, sumsTwo); + assertEquals(sumsTwoGenerator, sumsTwoGenerator); + assertEquals(sumsTwoGenerator, anotherTransformedGenerator); + assertTrue(!sumsTwoGenerator.equals((TransformedGenerator<Integer, Integer>)null)); + + TransformedGenerator<Integer, Integer> aGenerateWithADifferentFunction = + new TransformedGenerator<Integer, Integer>(wrappedGenerator, new UnaryFunction<Integer, Integer>() { + public Integer evaluate( Integer obj ) { + return obj; + } + }); + assertTrue( !sumsTwoGenerator.equals(aGenerateWithADifferentFunction)); + + TransformedGenerator<Integer, Integer> aTransformedGeneratorWithADifferentWrapped = + new TransformedGenerator<Integer, Integer>(new IntegerRange(1,2), sumsTwo); + assertTrue(!sumsTwoGenerator.equals(aTransformedGeneratorWithADifferentWrapped)); + } + + @Test + public void testHashcode() { + assertEquals(sumsTwoGenerator.hashCode(), sumsTwoGenerator.hashCode()); + assertEquals(sumsTwoGenerator.hashCode(), new TransformedGenerator<Integer, Integer>(wrappedGenerator, sumsTwo).hashCode()); + } + + @Test + public void testGenerate() { + final List<Integer> doubledValues = new ArrayList<Integer>(); + sumsTwoGenerator.run(new UnaryProcedure<Integer>() { + public void run( Integer obj ) { + doubledValues.add(obj); + } + }); + + final List<Integer> expected = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10 , 11); + + assertEquals(9, doubledValues.size()); + assertEquals(expected, doubledValues); + } + + // Attributes + // ------------------------------------------------------------------------ + private static final Integer TWO = new Integer(2); + + private Generator<Integer> wrappedGenerator = null; + private UnaryFunction<Integer, Integer> sumsTwo = new UnaryFunction<Integer, Integer>() { + public Integer evaluate( Integer obj ) { + return obj += TWO; + } + }; + private TransformedGenerator<Integer, Integer> sumsTwoGenerator = null; + +} Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestTransformedGenerator.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java?rev=1344774&view=auto ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java (added) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java Thu May 31 15:25:08 2012 @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.functor.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.functor.UnaryPredicate; +import org.apache.commons.functor.UnaryProcedure; +import org.apache.commons.functor.generator.util.IntegerRange; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the Until Generate class. + * @author Bruno P. Kinoshita (brunodepau...@yahoo.com.br) + */ +public class TestUntilGenerate +{ + + @Before + public void setUp() throws Exception { + wrappedGenerator = new IntegerRange(1, 10); + untilGenerate = new UntilGenerate<Integer>(isLessThanFive, wrappedGenerator); + } + + @After + public void tearDown() { + wrappedGenerator = null; + isLessThanFive = null; + untilGenerate = null; + } + + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testConstructorProhibitsNull() { + try { + new UntilGenerate<Integer>(null, untilGenerate); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new UntilGenerate<Integer>(isLessThanFive, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new UntilGenerate<Integer>(null, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + } + + @Test + public void testEquals() { + Generator<Integer> anotherGenerate = new UntilGenerate<Integer>(isLessThanFive, new IntegerRange(1, 10)); + assertEquals(untilGenerate, untilGenerate); + assertEquals(untilGenerate, anotherGenerate); + assertTrue(!untilGenerate.equals((UntilGenerate<Integer>)null)); + + Generator<Integer> aGenerateWithADifferentPredicate = new UntilGenerate<Integer>( + new UnaryPredicate<Integer>() { + public boolean test(Integer obj) { + return obj < FIVE; + } + }, new IntegerRange(1, 10)); + assertTrue(!untilGenerate.equals(aGenerateWithADifferentPredicate)); + + Generator<Integer> aGenerateWithADifferentWrapped = new UntilGenerate<Integer>(isLessThanFive, new IntegerRange(1,2)); + assertTrue(!untilGenerate.equals(aGenerateWithADifferentWrapped)); + } + + @Test + public void testHashcode() { + assertEquals(untilGenerate.hashCode(), untilGenerate.hashCode()); + assertEquals(untilGenerate.hashCode(), new UntilGenerate<Integer>(isLessThanFive, wrappedGenerator).hashCode()); + assertFalse(untilGenerate.hashCode() == new UntilGenerate<Integer>(isLessThanFive, wrappedGenerator) { + @Override + protected Generator<? extends Integer> getWrappedGenerator() + { + return null; + } + }.hashCode()); + } + + @Test + public void testGenerate() { + final List<Integer> numbersGreaterThanFive = new ArrayList<Integer>(); + untilGenerate.run(new UnaryProcedure<Integer>() { + public void run( Integer obj ) { + numbersGreaterThanFive.add(obj); + } + }); + assertEquals(5, numbersGreaterThanFive.size()); + + final List<Integer> expected = Arrays.asList(5, 6, 7, 8, 9); + assertEquals(expected, numbersGreaterThanFive); + } + + // Attributes + // ------------------------------------------------------------------------ + private static final Integer FIVE = new Integer(5); + + private Generator<Integer> wrappedGenerator = null; + private UnaryPredicate<Integer> isLessThanFive = new UnaryPredicate<Integer>() { + public boolean test( Integer obj ) { + return obj < FIVE; + } + }; + private Generator<Integer> untilGenerate = null; +} Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestUntilGenerate.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java?rev=1344774&view=auto ============================================================================== --- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java (added) +++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java Thu May 31 15:25:08 2012 @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.functor.generator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.functor.UnaryPredicate; +import org.apache.commons.functor.UnaryProcedure; +import org.apache.commons.functor.generator.util.IntegerRange; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests the While Generate class. + * @author Bruno P. Kinoshita (brunodepau...@yahoo.com.br) + */ +public class TestWhileGenerate { + + @Before + public void setUp() throws Exception { + wrappedGenerator = new IntegerRange(1, 10); + whileGenerate = new WhileGenerate<Integer>(isLessThanFive, wrappedGenerator); + } + + @After + public void tearDown() { + wrappedGenerator = null; + isLessThanFive = null; + whileGenerate = null; + } + + // Tests + // ------------------------------------------------------------------------ + + @Test + public void testConstructorProhibitsNull() { + try { + new WhileGenerate<Integer>(null, whileGenerate); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new WhileGenerate<Integer>(isLessThanFive, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + try { + new WhileGenerate<Integer>(null, null); + fail("ExpectedNullPointerException"); + } catch(IllegalArgumentException e) { + // expected + } + } + + @Test + public void testEquals() { + Generator<Integer> anotherGenerate = new WhileGenerate<Integer>(isLessThanFive, new IntegerRange(1, 10)); + assertEquals(whileGenerate, whileGenerate); + assertEquals(whileGenerate, anotherGenerate); + assertTrue(!whileGenerate.equals((WhileGenerate<Integer>)null)); + + Generator<Integer> aGenerateWithADifferentPredicate = new WhileGenerate<Integer>( + new UnaryPredicate<Integer>() { + public boolean test(Integer obj) { + return obj < FIVE; + } + }, new IntegerRange(1, 10)); + assertTrue(!whileGenerate.equals(aGenerateWithADifferentPredicate)); + + Generator<Integer> aGenerateWithADifferentWrapped = new WhileGenerate<Integer>(isLessThanFive, new IntegerRange(1,11)); + assertTrue(!whileGenerate.equals(aGenerateWithADifferentWrapped)); + } + + @Test + public void testHashcode() { + assertEquals(whileGenerate.hashCode(), whileGenerate.hashCode()); + assertEquals(whileGenerate.hashCode(), new WhileGenerate<Integer>(isLessThanFive, wrappedGenerator).hashCode()); + assertFalse(whileGenerate.hashCode() == new WhileGenerate<Integer>(isLessThanFive, wrappedGenerator) { + @Override + protected Generator<? extends Integer> getWrappedGenerator() { + return null; + } + }.hashCode()); + } + + @Test + public void testGenerate() { + final List<Integer> numbersMinorThanFive = new ArrayList<Integer>(); + whileGenerate.run(new UnaryProcedure<Integer>() { + public void run( Integer obj ) { + numbersMinorThanFive.add(obj); + } + }); + assertEquals(4, numbersMinorThanFive.size()); + + List<Integer> expected = Arrays.asList(1, 2, 3, 4); + assertEquals(expected, numbersMinorThanFive); + } + + // Attributes + // ------------------------------------------------------------------------ + private static final Integer FIVE = new Integer(5); + + private Generator<Integer> wrappedGenerator = null; + private UnaryPredicate<Integer> isLessThanFive = new UnaryPredicate<Integer>() { + public boolean test( Integer obj ) { + return obj < FIVE; + } + }; + private Generator<Integer> whileGenerate = null; +} Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestWhileGenerate.java ------------------------------------------------------------------------------ svn:mime-type = text/plain