Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java?rev=1311904&r1=1311903&r2=1311904&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java Tue Apr 10 18:22:29 2012 @@ -1,38 +1,38 @@ -/* - * 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.collections; - -import java.util.ArrayList; -import java.util.Collection; - -import org.junit.Before; - -public abstract class AbstractDecoratedCollectionTest<C> { - /** - * The {@link Collection} being decorated. - */ - protected Collection<C> original; - /** - * The Collection under test that decorates {@link #original}. - */ - protected Collection<C> decorated; - - @Before - public void setUpDecoratedCollection() throws Exception { - original = new ArrayList<C>(); - } -} +/* + * 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.collections; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Before; + +public abstract class AbstractDecoratedCollectionTest<C> { + /** + * The {@link Collection} being decorated. + */ + protected Collection<C> original; + /** + * The Collection under test that decorates {@link #original}. + */ + protected Collection<C> decorated; + + @Before + public void setUpDecoratedCollection() throws Exception { + original = new ArrayList<C>(); + } +}
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MockTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MockTestCase.java?rev=1311904&r1=1311903&r2=1311904&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MockTestCase.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/MockTestCase.java Tue Apr 10 18:22:29 2012 @@ -1,67 +1,67 @@ -/* - * 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.collections; - -import java.util.ArrayList; -import java.util.List; -import java.util.ListIterator; - -import org.easymock.EasyMock; -import org.easymock.IExpectationSetters; - -/** - * Provides utilities for making mock-based tests. Most notable is the generic "type-safe" - * {@link #createMock(Class)} method, and {@link #replay()} and {@link #verify()} methods - * that call the respective methods on all created mock objects. - * - * @author Stephen Kestle - */ -public abstract class MockTestCase { - private List<Object> mockObjects = new ArrayList<Object>(); - - @SuppressWarnings("unchecked") - protected <T> T createMock(Class<?> name) { - T mock = (T) EasyMock.createMock(name); - return registerMock(mock); - } - - private <T> T registerMock(T mock) { - mockObjects.add(mock); - return mock; - } - - protected <T> IExpectationSetters<T> expect(T t) { - return EasyMock.expect(t); - } - - protected final void replay() { - for (Object o : mockObjects) { - EasyMock.replay(o); - } - } - - protected final void verify() { - for (ListIterator<Object> i = mockObjects.listIterator(); i.hasNext();) { - try { - EasyMock.verify(i.next()); - } catch (AssertionError e) { - throw new AssertionError((i.previousIndex() + 1) + "" - + e.getMessage()); - } - } - } -} +/* + * 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.collections; + +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; + +import org.easymock.EasyMock; +import org.easymock.IExpectationSetters; + +/** + * Provides utilities for making mock-based tests. Most notable is the generic "type-safe" + * {@link #createMock(Class)} method, and {@link #replay()} and {@link #verify()} methods + * that call the respective methods on all created mock objects. + * + * @author Stephen Kestle + */ +public abstract class MockTestCase { + private List<Object> mockObjects = new ArrayList<Object>(); + + @SuppressWarnings("unchecked") + protected <T> T createMock(Class<?> name) { + T mock = (T) EasyMock.createMock(name); + return registerMock(mock); + } + + private <T> T registerMock(T mock) { + mockObjects.add(mock); + return mock; + } + + protected <T> IExpectationSetters<T> expect(T t) { + return EasyMock.expect(t); + } + + protected final void replay() { + for (Object o : mockObjects) { + EasyMock.replay(o); + } + } + + protected final void verify() { + for (ListIterator<Object> i = mockObjects.listIterator(); i.hasNext();) { + try { + EasyMock.verify(i.next()); + } catch (AssertionError e) { + throw new AssertionError((i.previousIndex() + 1) + "" + + e.getMessage()); + } + } + } +} Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestIndexedCollection.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestIndexedCollection.java?rev=1311904&r1=1311903&r2=1311904&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestIndexedCollection.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/TestIndexedCollection.java Tue Apr 10 18:22:29 2012 @@ -1,90 +1,90 @@ -/* - * 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.collections; - -import static java.util.Arrays.asList; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; - -import org.junit.Before; -import org.junit.Test; - -@SuppressWarnings("boxing") -public class TestIndexedCollection extends AbstractDecoratedCollectionTest<String> { - private IndexedCollection<Integer, String> indexed; - - @Before - public void setUp() throws Exception { - indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() { - public Integer transform(String input) { - return Integer.parseInt(input); - } - }); - decorated = indexed; - } - - @Test - public void addedObjectsCanBeRetrievedByKey() throws Exception { - decorated.add("12"); - decorated.add("16"); - decorated.add("1"); - decorated.addAll(asList("2","3","4")); - assertEquals("12", indexed.get(12)); - assertEquals("16", indexed.get(16)); - assertEquals("1", indexed.get(1)); - assertEquals("2", indexed.get(2)); - assertEquals("3", indexed.get(3)); - assertEquals("4", indexed.get(4)); - } - - @Test(expected=IllegalArgumentException.class) - public void ensureDuplicateObjectsCauseException() throws Exception { - decorated.add("1"); - decorated.add("1"); - } - - @Test - public void decoratedCollectionIsIndexedOnCreation() throws Exception { - original.add("1"); - original.add("2"); - original.add("3"); - - indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() { - public Integer transform(String input) { - return Integer.parseInt(input); - } - }); - assertEquals("1", indexed.get(1)); - assertEquals("2", indexed.get(2)); - assertEquals("3", indexed.get(3)); - } - - @Test - public void reindexUpdatesIndexWhenTheDecoratedCollectionIsModifiedSeparately() throws Exception { - original.add("1"); - original.add("2"); - original.add("3"); - - assertNull(indexed.get(1)); - assertNull(indexed.get(2)); - assertNull(indexed.get(3)); - indexed.reindex(); - assertEquals("1", indexed.get(1)); - assertEquals("2", indexed.get(2)); - assertEquals("3", indexed.get(3)); - } -} +/* + * 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.collections; + +import static java.util.Arrays.asList; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + +import org.junit.Before; +import org.junit.Test; + +@SuppressWarnings("boxing") +public class TestIndexedCollection extends AbstractDecoratedCollectionTest<String> { + private IndexedCollection<Integer, String> indexed; + + @Before + public void setUp() throws Exception { + indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() { + public Integer transform(String input) { + return Integer.parseInt(input); + } + }); + decorated = indexed; + } + + @Test + public void addedObjectsCanBeRetrievedByKey() throws Exception { + decorated.add("12"); + decorated.add("16"); + decorated.add("1"); + decorated.addAll(asList("2","3","4")); + assertEquals("12", indexed.get(12)); + assertEquals("16", indexed.get(16)); + assertEquals("1", indexed.get(1)); + assertEquals("2", indexed.get(2)); + assertEquals("3", indexed.get(3)); + assertEquals("4", indexed.get(4)); + } + + @Test(expected=IllegalArgumentException.class) + public void ensureDuplicateObjectsCauseException() throws Exception { + decorated.add("1"); + decorated.add("1"); + } + + @Test + public void decoratedCollectionIsIndexedOnCreation() throws Exception { + original.add("1"); + original.add("2"); + original.add("3"); + + indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() { + public Integer transform(String input) { + return Integer.parseInt(input); + } + }); + assertEquals("1", indexed.get(1)); + assertEquals("2", indexed.get(2)); + assertEquals("3", indexed.get(3)); + } + + @Test + public void reindexUpdatesIndexWhenTheDecoratedCollectionIsModifiedSeparately() throws Exception { + original.add("1"); + original.add("2"); + original.add("3"); + + assertNull(indexed.get(1)); + assertNull(indexed.get(2)); + assertNull(indexed.get(3)); + indexed.reindex(); + assertEquals("1", indexed.get(1)); + assertEquals("2", indexed.get(2)); + assertEquals("3", indexed.get(3)); + } +} Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java?rev=1311904&r1=1311903&r2=1311904&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java Tue Apr 10 18:22:29 2012 @@ -1,35 +1,35 @@ -/* - * 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.collections.functors; - -import org.apache.commons.collections.Closure; -import org.junit.Assert; -import org.junit.Test; - -public abstract class BasicClosureTestBase { - - @Test - public void closureSanityTests() throws Exception { - Closure<?> closure = generateClosure(); - Assert.assertNotNull(closure); - } - - /** - * @return a closure for general sanity tests. - */ - protected abstract <T> Closure<T> generateClosure(); -} +/* + * 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.collections.functors; + +import org.apache.commons.collections.Closure; +import org.junit.Assert; +import org.junit.Test; + +public abstract class BasicClosureTestBase { + + @Test + public void closureSanityTests() throws Exception { + Closure<?> closure = generateClosure(); + Assert.assertNotNull(closure); + } + + /** + * @return a closure for general sanity tests. + */ + protected abstract <T> Closure<T> generateClosure(); +} Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java?rev=1311904&r1=1311903&r2=1311904&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java Tue Apr 10 18:22:29 2012 @@ -1,93 +1,93 @@ -/* - * 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.collections.functors; - -import java.io.IOException; - -import org.apache.commons.collections.Closure; -import org.apache.commons.collections.FunctorException; -import org.junit.Assert; -import org.junit.Test; - -public class TestCatchAndRethrowClosure extends BasicClosureTestBase { - - private static <T> Closure<T> generateIOExceptionClosure() { - return new CatchAndRethrowClosure<T>() { - - @Override - protected void executeAndThrow(T input) throws IOException { - throw new IOException(); - } - }; - } - - private static <T> Closure<T> generateNullPointerExceptionClosure() { - return new CatchAndRethrowClosure<T>() { - - @Override - protected void executeAndThrow(T input) { - throw new NullPointerException(); - } - }; - } - - private static <T> Closure<T> generateNoExceptionClosure() { - return new CatchAndRethrowClosure<T>() { - - @Override - protected void executeAndThrow(T input) { - } - }; - } - - @Override - protected <T> Closure<T> generateClosure() { - return generateNoExceptionClosure(); - } - - @Test - public void testThrowingClosure() { - Closure<Integer> closure = generateNoExceptionClosure(); - try { - closure.execute(Integer.valueOf(0)); - } catch (FunctorException ex) { - Assert.fail(); - } catch (RuntimeException ex) { - Assert.fail(); - } - - closure = generateIOExceptionClosure(); - try { - closure.execute(Integer.valueOf(0)); - Assert.fail(); - } catch (FunctorException ex) { - Assert.assertTrue(ex.getCause() instanceof IOException); - } catch (RuntimeException ex) { - Assert.fail(); - } - - closure = generateNullPointerExceptionClosure(); - try { - closure.execute(Integer.valueOf(0)); - Assert.fail(); - } catch (FunctorException ex) { - Assert.fail(); - } catch (RuntimeException ex) { - Assert.assertTrue(ex instanceof NullPointerException); - } - } -} +/* + * 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.collections.functors; + +import java.io.IOException; + +import org.apache.commons.collections.Closure; +import org.apache.commons.collections.FunctorException; +import org.junit.Assert; +import org.junit.Test; + +public class TestCatchAndRethrowClosure extends BasicClosureTestBase { + + private static <T> Closure<T> generateIOExceptionClosure() { + return new CatchAndRethrowClosure<T>() { + + @Override + protected void executeAndThrow(T input) throws IOException { + throw new IOException(); + } + }; + } + + private static <T> Closure<T> generateNullPointerExceptionClosure() { + return new CatchAndRethrowClosure<T>() { + + @Override + protected void executeAndThrow(T input) { + throw new NullPointerException(); + } + }; + } + + private static <T> Closure<T> generateNoExceptionClosure() { + return new CatchAndRethrowClosure<T>() { + + @Override + protected void executeAndThrow(T input) { + } + }; + } + + @Override + protected <T> Closure<T> generateClosure() { + return generateNoExceptionClosure(); + } + + @Test + public void testThrowingClosure() { + Closure<Integer> closure = generateNoExceptionClosure(); + try { + closure.execute(Integer.valueOf(0)); + } catch (FunctorException ex) { + Assert.fail(); + } catch (RuntimeException ex) { + Assert.fail(); + } + + closure = generateIOExceptionClosure(); + try { + closure.execute(Integer.valueOf(0)); + Assert.fail(); + } catch (FunctorException ex) { + Assert.assertTrue(ex.getCause() instanceof IOException); + } catch (RuntimeException ex) { + Assert.fail(); + } + + closure = generateNullPointerExceptionClosure(); + try { + closure.execute(Integer.valueOf(0)); + Assert.fail(); + } catch (FunctorException ex) { + Assert.fail(); + } catch (RuntimeException ex) { + Assert.assertTrue(ex instanceof NullPointerException); + } + } +} Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java?rev=1311904&r1=1311903&r2=1311904&view=diff ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java (original) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java Tue Apr 10 18:22:29 2012 @@ -1,82 +1,82 @@ -/* - * 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.collections.functors; - -import static org.apache.commons.collections.functors.ComparatorPredicate.*; -import java.util.Comparator; - -import org.apache.commons.collections.Predicate; -import org.junit.Test; - - -public class TestComparatorPredicate extends BasicPredicateTestBase { - private class TestComparator<T extends Comparable<T>> implements Comparator<T> { - public int compare(T first, T second) { - return first.compareTo(second); - } - } - - @Test - public void compareEquals() { - Integer value = Integer.valueOf(10); - Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>()); - assertFalse(p, Integer.valueOf(value.intValue() - 1)); - assertTrue(p, Integer.valueOf(value.intValue())); - assertFalse(p, Integer.valueOf(value.intValue() + 1)); - } - - @Test - public void compareGreater() { - Integer value = Integer.valueOf(10); - Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER); - assertTrue(p, Integer.valueOf(value.intValue() - 1)); - assertFalse(p, Integer.valueOf(value.intValue())); - assertFalse(p, Integer.valueOf(value.intValue() + 1)); - } - - @Test - public void compareLess() { - Integer value = Integer.valueOf(10); - Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS); - assertFalse(p, Integer.valueOf(value.intValue() - 1)); - assertFalse(p, Integer.valueOf(value.intValue())); - assertTrue(p, Integer.valueOf(value.intValue() + 1)); - } - - @Test - public void compareGreaterOrEqual() { - Integer value = Integer.valueOf(10); - Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER_OR_EQUAL); - assertTrue(p, Integer.valueOf(value.intValue() - 1)); - assertTrue(p, Integer.valueOf(value.intValue())); - assertFalse(p, Integer.valueOf(value.intValue() + 1)); - } - - @Test - public void compareLessOrEqual() { - Integer value = Integer.valueOf(10); - Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS_OR_EQUAL); - assertFalse(p, Integer.valueOf(value.intValue() - 1)); - assertTrue(p, Integer.valueOf(value.intValue())); - assertTrue(p, Integer.valueOf(value.intValue() + 1)); - } - - @Override - protected Predicate<?> generatePredicate() { - return comparatorPredicate(Integer.valueOf(10), new TestComparator<Integer>()); - } -} +/* + * 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.collections.functors; + +import static org.apache.commons.collections.functors.ComparatorPredicate.*; +import java.util.Comparator; + +import org.apache.commons.collections.Predicate; +import org.junit.Test; + + +public class TestComparatorPredicate extends BasicPredicateTestBase { + private class TestComparator<T extends Comparable<T>> implements Comparator<T> { + public int compare(T first, T second) { + return first.compareTo(second); + } + } + + @Test + public void compareEquals() { + Integer value = Integer.valueOf(10); + Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>()); + assertFalse(p, Integer.valueOf(value.intValue() - 1)); + assertTrue(p, Integer.valueOf(value.intValue())); + assertFalse(p, Integer.valueOf(value.intValue() + 1)); + } + + @Test + public void compareGreater() { + Integer value = Integer.valueOf(10); + Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER); + assertTrue(p, Integer.valueOf(value.intValue() - 1)); + assertFalse(p, Integer.valueOf(value.intValue())); + assertFalse(p, Integer.valueOf(value.intValue() + 1)); + } + + @Test + public void compareLess() { + Integer value = Integer.valueOf(10); + Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS); + assertFalse(p, Integer.valueOf(value.intValue() - 1)); + assertFalse(p, Integer.valueOf(value.intValue())); + assertTrue(p, Integer.valueOf(value.intValue() + 1)); + } + + @Test + public void compareGreaterOrEqual() { + Integer value = Integer.valueOf(10); + Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER_OR_EQUAL); + assertTrue(p, Integer.valueOf(value.intValue() - 1)); + assertTrue(p, Integer.valueOf(value.intValue())); + assertFalse(p, Integer.valueOf(value.intValue() + 1)); + } + + @Test + public void compareLessOrEqual() { + Integer value = Integer.valueOf(10); + Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS_OR_EQUAL); + assertFalse(p, Integer.valueOf(value.intValue() - 1)); + assertTrue(p, Integer.valueOf(value.intValue())); + assertTrue(p, Integer.valueOf(value.intValue() + 1)); + } + + @Override + protected Predicate<?> generatePredicate() { + return comparatorPredicate(Integer.valueOf(10), new TestComparator<Integer>()); + } +} Added: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/list/difference/SequencesComparatorTest.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/list/difference/SequencesComparatorTest.java?rev=1311904&view=auto ============================================================================== --- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/list/difference/SequencesComparatorTest.java (added) +++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/list/difference/SequencesComparatorTest.java Tue Apr 10 18:22:29 2012 @@ -0,0 +1,235 @@ +/* + * 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.collections.list.difference; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class SequencesComparatorTest { + + @Test + public void testLength() { + for (int i = 0; i < before.size(); ++i) { + SequencesComparator<Character> comparator = + new SequencesComparator<Character>(sequence(before.get(i)), + sequence(after.get(i))); + Assert.assertEquals(length[i], comparator.getScript().getModifications()); + } + } + + @Test + public void testExecution() { + ExecutionVisitor<Character> ev = new ExecutionVisitor<Character>(); + for (int i = 0; i < before.size(); ++i) { + ev.setList(sequence(before.get(i))); + new SequencesComparator<Character>(sequence(before.get(i)), + sequence(after.get(i))).getScript().visit(ev); + Assert.assertEquals(after.get(i), ev.getString()); + } + } + + @Test + public void testMinimal() { + String[] shadokAlph = new String[] { + new String("GA"), + new String("BU"), + new String("ZO"), + new String("MEU") + }; + List<String> sentenceBefore = new ArrayList<String>(); + List<String> sentenceAfter = new ArrayList<String>(); + sentenceBefore.add(shadokAlph[0]); + sentenceBefore.add(shadokAlph[2]); + sentenceBefore.add(shadokAlph[3]); + sentenceBefore.add(shadokAlph[1]); + sentenceBefore.add(shadokAlph[0]); + sentenceBefore.add(shadokAlph[0]); + sentenceBefore.add(shadokAlph[2]); + sentenceBefore.add(shadokAlph[1]); + sentenceBefore.add(shadokAlph[3]); + sentenceBefore.add(shadokAlph[0]); + sentenceBefore.add(shadokAlph[2]); + sentenceBefore.add(shadokAlph[1]); + sentenceBefore.add(shadokAlph[3]); + sentenceBefore.add(shadokAlph[2]); + sentenceBefore.add(shadokAlph[2]); + sentenceBefore.add(shadokAlph[0]); + sentenceBefore.add(shadokAlph[1]); + sentenceBefore.add(shadokAlph[3]); + sentenceBefore.add(shadokAlph[0]); + sentenceBefore.add(shadokAlph[3]); + + Random random = new Random(4564634237452342L); + + for (int nbCom = 0; nbCom <= 40; nbCom+=5) { + sentenceAfter.clear(); + sentenceAfter.addAll(sentenceBefore); + for (int i = 0; i<nbCom; i++) { + if (random.nextInt(2) == 0) { + sentenceAfter.add(random.nextInt(sentenceAfter.size() + 1), + shadokAlph[random.nextInt(4)]); + } else { + sentenceAfter.remove(random.nextInt(sentenceAfter.size())); + } + } + + SequencesComparator<String> comparator = + new SequencesComparator<String>(sentenceBefore, sentenceAfter); + Assert.assertTrue(comparator.getScript().getModifications() <= nbCom); + } + } + + @Test + public void testShadok() { + int lgMax = 5; + String[] shadokAlph = new String[] { + new String("GA"), + new String("BU"), + new String("ZO"), + new String("MEU") + }; + List<List<String>> shadokSentences = new ArrayList<List<String>>(); + for (int lg=0; lg<lgMax; ++lg) { + List<List<String>> newTab = new ArrayList<List<String>>(); + newTab.add(new ArrayList<String>()); + for (int k = 0; k < shadokAlph.length; k++) { + for (List<String> sentence : shadokSentences) { + List<String> newSentence = new ArrayList<String>(sentence); + newSentence.add(shadokAlph[k]); + newTab.add(newSentence); + } + } + shadokSentences = newTab; + } + + ExecutionVisitor<String> ev = new ExecutionVisitor<String>(); + + for (int i = 0; i < shadokSentences.size(); ++i) { + for (int j = 0; j < shadokSentences.size(); ++j) { + ev.setList(shadokSentences.get(i)); + new SequencesComparator<String>(shadokSentences.get(i), + shadokSentences.get(j)).getScript().visit(ev); + + StringBuilder concat = new StringBuilder(); + for (final String s : shadokSentences.get(j)) { + concat.append(s); + } + Assert.assertEquals(concat.toString(), ev.getString()); + } + } + } + + private List<Character> sequence(String string) { + List<Character> list = new ArrayList<Character>(); + for (int i = 0; i < string.length(); ++i) { + list.add(new Character(string.charAt(i))); + } + return list; + } + + private class ExecutionVisitor<T> implements CommandVisitor<T> { + + private List<T> v; + private int index; + + public void setList(List<T> array) { + v = new ArrayList<T>(array); + index = 0; + } + + public void visitInsertCommand(T object) { + v.add(index++, object); + } + + public void visitKeepCommand(T object) { + ++index; + } + + public void visitDeleteCommand(T object) { + v.remove(index); + } + + public String getString() { + StringBuffer buffer = new StringBuffer(); + for (T c : v) { + buffer.append(c); + } + return buffer.toString(); + } + + } + + @Before + public void setUp() { + + before = Arrays.asList(new String[] { + "bottle", + "nematode knowledge", + "", + "aa", + "prefixed string", + "ABCABBA", + "glop glop", + "coq", + "spider-man" + }); + + after = Arrays.asList(new String[] { + "noodle", + "empty bottle", + "", + "C", + "prefix", + "CBABAC", + "pas glop pas glop", + "ane", + "klingon" + }); + + length = new int[] { + 6, + 16, + 0, + 3, + 9, + 5, + 8, + 6, + 13 + }; + + } + + @After + public void tearDown() { + before = null; + after = null; + length = null; + } + + private List<String> before; + private List<String> after; + private int[] length; + +} Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/list/difference/SequencesComparatorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/list/difference/SequencesComparatorTest.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision"
