diff --git a/src/main/java/org/apache/commons/collections/IndexedCollection.java b/src/main/java/org/apache/commons/collections/IndexedCollection.java index fcda9bc9e..f0806955e 100644 --- a/src/main/java/org/apache/commons/collections/IndexedCollection.java +++ b/src/main/java/org/apache/commons/collections/IndexedCollection.java @@ -1,136 +1,136 @@ -/* - * 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.Collection; -import java.util.HashMap; - -import org.apache.commons.collections.collection.AbstractCollectionDecorator; - -/** - * An IndexedCollection is a Map-like view onto a Collection. It accepts a - * keyTransformer to define how the keys are converted from the values. - *

- * Modifications made to this decorator modify the index as well as the - * decorated {@link Collection}. However, modifications to the underlying - * {@link Collection} will not updated the index and it will get out of sync. - *

- * If modification to the decorated {@link Collection} is unavoidable, then a - * call to {@link #reindex()} will update the index to the current contents of - * the {@link Collection}. - * - * @param the type of object in the index. - * @param the type of object in the collection. - * @author Stephen Kestle - */ -// TODO support MultiMap/non-unique index behavior -// TODO add support for remove and clear -public class IndexedCollection extends AbstractCollectionDecorator { - /** - * . - */ - private static final long serialVersionUID = -5512610452568370038L; - - /** - * Create an {@link IndexedCollection} for a unique index. - * - * @param the index object type. - * @param the collection type. - * @param coll the decorated {@link Collection}. - * @param keyTransformer the {@link Transformer} for generating index keys. - * @return the created {@link IndexedCollection}. - */ - public static IndexedCollection uniqueIndexedCollection(final Collection coll, final Transformer keyTransformer) { - return new IndexedCollection(coll, keyTransformer, new HashMap()); - } - - /** - * The {@link Transformer} for generating index keys. - */ - private final Transformer keyTransformer; - - /** - * The map of indexes to collected objects. - */ - private final HashMap index; - - /** - * Create a {@link IndexedCollection} for a unique index. - * - * @param coll the decorated {@link Collection}. - * @param keyTransformer the {@link Transformer} for generating index keys. - */ - public IndexedCollection(Collection coll, Transformer keyTransformer, HashMap map) { - super(coll); - this.keyTransformer = keyTransformer; - this.index = map; - reindex(); - } - - /** - * Clears the index and re-indexes the entire decorated {@link Collection}. - */ - public void reindex() { - index.clear(); - for (C c : decorated()) { - addIndex(c); - } - } - - /** - * Adds an object to the collection and index. - */ - @Override - // TODO: Add error handling for when super.add() fails - public boolean add(C object) { - addIndex(object); - return super.add(object); - } - - /** - * Adds an entire collection to the collection and index. - */ - @Override - // TODO: Add error handling for when super.addAll() fails - public boolean addAll(Collection coll) { - for (C c : coll) { - addIndex(c); - } - return super.addAll(coll); - } - - /** - * Provides checking for adding the index. - * - * @param object the object to index. - */ - private void addIndex(C object) { - final C existingObject = index.put(keyTransformer.transform(object), object); - if (existingObject != null) { - throw new IllegalArgumentException("Duplicate key in uniquely indexed collection."); - } - } - - /** - * Get the element associated with the given key. - * @param key to look up - * @return element found - */ - public C get(K key) { - return index.get(key); - } -} +/* + * 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.Collection; +import java.util.HashMap; + +import org.apache.commons.collections.collection.AbstractCollectionDecorator; + +/** + * An IndexedCollection is a Map-like view onto a Collection. It accepts a + * keyTransformer to define how the keys are converted from the values. + *

+ * Modifications made to this decorator modify the index as well as the + * decorated {@link Collection}. However, modifications to the underlying + * {@link Collection} will not updated the index and it will get out of sync. + *

+ * If modification to the decorated {@link Collection} is unavoidable, then a + * call to {@link #reindex()} will update the index to the current contents of + * the {@link Collection}. + * + * @param the type of object in the index. + * @param the type of object in the collection. + * @author Stephen Kestle + */ +// TODO support MultiMap/non-unique index behavior +// TODO add support for remove and clear +public class IndexedCollection extends AbstractCollectionDecorator { + /** + * . + */ + private static final long serialVersionUID = -5512610452568370038L; + + /** + * Create an {@link IndexedCollection} for a unique index. + * + * @param the index object type. + * @param the collection type. + * @param coll the decorated {@link Collection}. + * @param keyTransformer the {@link Transformer} for generating index keys. + * @return the created {@link IndexedCollection}. + */ + public static IndexedCollection uniqueIndexedCollection(final Collection coll, final Transformer keyTransformer) { + return new IndexedCollection(coll, keyTransformer, new HashMap()); + } + + /** + * The {@link Transformer} for generating index keys. + */ + private final Transformer keyTransformer; + + /** + * The map of indexes to collected objects. + */ + private final HashMap index; + + /** + * Create a {@link IndexedCollection} for a unique index. + * + * @param coll the decorated {@link Collection}. + * @param keyTransformer the {@link Transformer} for generating index keys. + */ + public IndexedCollection(Collection coll, Transformer keyTransformer, HashMap map) { + super(coll); + this.keyTransformer = keyTransformer; + this.index = map; + reindex(); + } + + /** + * Clears the index and re-indexes the entire decorated {@link Collection}. + */ + public void reindex() { + index.clear(); + for (C c : decorated()) { + addIndex(c); + } + } + + /** + * Adds an object to the collection and index. + */ + @Override + // TODO: Add error handling for when super.add() fails + public boolean add(C object) { + addIndex(object); + return super.add(object); + } + + /** + * Adds an entire collection to the collection and index. + */ + @Override + // TODO: Add error handling for when super.addAll() fails + public boolean addAll(Collection coll) { + for (C c : coll) { + addIndex(c); + } + return super.addAll(coll); + } + + /** + * Provides checking for adding the index. + * + * @param object the object to index. + */ + private void addIndex(C object) { + final C existingObject = index.put(keyTransformer.transform(object), object); + if (existingObject != null) { + throw new IllegalArgumentException("Duplicate key in uniquely indexed collection."); + } + } + + /** + * Get the element associated with the given key. + * @param key to look up + * @return element found + */ + public C get(K key) { + return index.get(key); + } +} diff --git a/src/main/java/org/apache/commons/collections/functors/CatchAndRethrowClosure.java b/src/main/java/org/apache/commons/collections/functors/CatchAndRethrowClosure.java index d970a11f3..a77e91dce 100644 --- a/src/main/java/org/apache/commons/collections/functors/CatchAndRethrowClosure.java +++ b/src/main/java/org/apache/commons/collections/functors/CatchAndRethrowClosure.java @@ -1,76 +1,76 @@ -/* - * 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.apache.commons.collections.FunctorException; - -/** - * {@link Closure} that catches any checked exception and re-throws it as a - * {@link FunctorException} runtime exception. Example usage: - * - *

- * // Create a catch and re-throw closure via anonymous subclass
- * CatchAndRethrowClosure<String> writer = new ThrowingClosure() {
- *     private java.io.Writer out = // some writer
- *     
- *     protected void executeAndThrow(String input) throws IOException {
- *         out.write(input); // throwing of IOException allowed
- *     }
- * };
- * 
- * // use catch and re-throw closure
- * java.util.List strList = // some list
- * try {
- *     CollctionUtils.forAllDo(strList, writer);
- * } catch (FunctorException ex) {
- *     Throwable originalError = ex.getCause();
- *     // handle error
- * }
- * 
- * - * @since Commons Collections 4.0 - * @version $Revision$ - */ -public abstract class CatchAndRethrowClosure implements Closure { - - /** - * Execute this closure on the specified input object. - * - * @param input the input to execute on - * @throws FunctorException (runtime) if the closure execution resulted in a - * checked exception. - */ - public void execute(E input) { - try { - executeAndThrow(input); - } catch (RuntimeException ex) { - throw ex; - } catch (Throwable t) { - throw new FunctorException(t); - } - } - - /** - * Execute this closure on the specified input object. - * - * @param input the input to execute on - * @throws Throwable if the closure execution resulted in a checked - * exception. - */ - protected abstract void executeAndThrow(E input) throws Throwable; -} +/* + * 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.apache.commons.collections.FunctorException; + +/** + * {@link Closure} that catches any checked exception and re-throws it as a + * {@link FunctorException} runtime exception. Example usage: + * + *
+ * // Create a catch and re-throw closure via anonymous subclass
+ * CatchAndRethrowClosure<String> writer = new ThrowingClosure() {
+ *     private java.io.Writer out = // some writer
+ *     
+ *     protected void executeAndThrow(String input) throws IOException {
+ *         out.write(input); // throwing of IOException allowed
+ *     }
+ * };
+ * 
+ * // use catch and re-throw closure
+ * java.util.List strList = // some list
+ * try {
+ *     CollctionUtils.forAllDo(strList, writer);
+ * } catch (FunctorException ex) {
+ *     Throwable originalError = ex.getCause();
+ *     // handle error
+ * }
+ * 
+ * + * @since Commons Collections 4.0 + * @version $Revision$ + */ +public abstract class CatchAndRethrowClosure implements Closure { + + /** + * Execute this closure on the specified input object. + * + * @param input the input to execute on + * @throws FunctorException (runtime) if the closure execution resulted in a + * checked exception. + */ + public void execute(E input) { + try { + executeAndThrow(input); + } catch (RuntimeException ex) { + throw ex; + } catch (Throwable t) { + throw new FunctorException(t); + } + } + + /** + * Execute this closure on the specified input object. + * + * @param input the input to execute on + * @throws Throwable if the closure execution resulted in a checked + * exception. + */ + protected abstract void executeAndThrow(E input) throws Throwable; +} diff --git a/src/main/java/org/apache/commons/collections/functors/ComparatorPredicate.java b/src/main/java/org/apache/commons/collections/functors/ComparatorPredicate.java index e88dea238..60bbb6b32 100644 --- a/src/main/java/org/apache/commons/collections/functors/ComparatorPredicate.java +++ b/src/main/java/org/apache/commons/collections/functors/ComparatorPredicate.java @@ -1,188 +1,188 @@ -/* - * 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.Serializable; -import java.util.Comparator; - -import org.apache.commons.collections.Predicate; - -/** - * Predicate that compares the input object with the one stored in the predicate using a comparator. - * In addition, the comparator result can be evaluated in accordance to a supplied criterion value. - * - * In order to demonstrate the use of the predicate, the following variables are declared: - * - *
- * Integer ONE = new Integer(1);
- * Integer TWO = new Integer(2);
- *
- * Comparator comparator = new Comparator() {
- *
- *     public int compare(Object first, Object second) {
- *         return ((Integer) second) - ((Integer) first);
- *     }
- *
- * };
- * 
- * - * Using the declared variables, the ComparatorPredicate can be used used in the - * following way: - * - *
- * ComparatorPredicate.getInstance(ONE, comparator).evaluate(TWO);
- * 
- * - * The input variable TWO in compared to the stored variable ONE using - * the supplied comparator. This is the default usage of the predicate and will return - * true if the underlying comparator returns 0. In addition to the default - * usage of the predicate, it is possible to evaluate the comparator's result in several ways. The - * following {@link Criterion} enumeration values are provided by the predicate: - *

- * - *
    - *
  • EQUAL
  • - *
  • GREATER
  • - *
  • GREATER_OR_EQUAL
  • - *
  • LESS
  • - *
  • LESS_OR_EQUAL
  • - *
- * - * The following examples demonstrates how these constants can be used in order to manipulate the - * evaluation of a comparator result. - * - *
- * ComparatorPredicate.getInstance(ONE, comparator, ComparatorPredicate.Criterion.GREATER).evaluate(TWO);
- * 
- * - * The input variable TWO is compared to the stored variable ONE using the supplied comparator - * using the GREATER evaluation criterion constant. This instructs the predicate to - * return true if the comparator returns a value greater than 0. - * - * @since Commons Collections 4.0 - * @version $Revision$ - * - * @author Rune Peter Bjørnstad. - */ -public class ComparatorPredicate implements Predicate, Serializable { - - private static final long serialVersionUID = -1863209236504077399L; - - public enum Criterion { - EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL, - } - - // Instance variables: - - /** The internal object to compare with */ - private final T object; - - /** The comparator to use for comparison */ - private final Comparator comparator; - - /** The comparison evaluation criterion to use */ - private final Criterion criterion; - - /** - * Factory to create the comparator predicate - * - * @param object the object to compare to - * @param comparator the comparator to use for comparison - * @return the predicate - * @throws IllegalArgumentException if comparator is null - */ - public static Predicate comparatorPredicate(T object, Comparator comparator) { - return comparatorPredicate(object, comparator, Criterion.EQUAL); - } - - /** - * Factory to create the comparator predicate - * - * @param object the object to compare to - * @param comparator the comparator to use for comparison - * @param criterion the criterion to use to evaluate comparison - * @return the predicate - * @throws IllegalArgumentException if comparator is null of criterion is invalid - */ - public static Predicate comparatorPredicate(T object, Comparator comparator, Criterion criterion) { - if (comparator == null) { - throw new IllegalArgumentException("Comparator must not be null."); - } - if (criterion == null) { - throw new IllegalArgumentException("Criterion must not be null."); - } - return new ComparatorPredicate(object, comparator, criterion); - } - - /** - * Constructor that performs no validation. - * Use getInstance if you want. - * - * @param object the object to compare to - * @param comparator the comparator to use for comparison - * @param criterion the criterion to use to evaluate comparison - */ - public ComparatorPredicate(T object, Comparator comparator, Criterion criterion) { - super(); - this.object = object; - this.comparator = comparator; - this.criterion = criterion; - } - - /** - * Evaluates the predicate. The predicate evaluates to true in the following cases: - * - *
    - *
  • comparator.compare(object, input) == 0 && criterion == EQUAL
  • - *
  • comparator.compare(object, input) < 0 && criterion == LESS
  • - *
  • comparator.compare(object, input) > 0 && criterion == GREATER
  • - *
  • comparator.compare(object, input) >= 0 && criterion == GREATER_OR_EQUAL
  • - *
  • comparator.compare(object, input) <= 0 && criterion == LESS_OR_EQUAL
  • - *
- * - * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object) - * @see java.util.Comparator#compare(java.lang.Object first, java.lang.Object second) - * - * @throws IllegalStateException if the criterion is invalid (really not possible) - */ - public boolean evaluate(T target) { - - boolean result = false; - int comparison = comparator.compare(object, target); - switch (criterion) { - case EQUAL: - result = (comparison == 0); - break; - case GREATER: - result = (comparison > 0); - break; - case LESS: - result = (comparison < 0); - break; - case GREATER_OR_EQUAL: - result = (comparison >= 0); - break; - case LESS_OR_EQUAL: - result = (comparison <= 0); - break; - default: - throw new IllegalStateException("The current criterion '" + criterion + "' is invalid."); - } - - return result; - } -} +/* + * 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.Serializable; +import java.util.Comparator; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate that compares the input object with the one stored in the predicate using a comparator. + * In addition, the comparator result can be evaluated in accordance to a supplied criterion value. + * + * In order to demonstrate the use of the predicate, the following variables are declared: + * + *
+ * Integer ONE = new Integer(1);
+ * Integer TWO = new Integer(2);
+ *
+ * Comparator comparator = new Comparator() {
+ *
+ *     public int compare(Object first, Object second) {
+ *         return ((Integer) second) - ((Integer) first);
+ *     }
+ *
+ * };
+ * 
+ * + * Using the declared variables, the ComparatorPredicate can be used used in the + * following way: + * + *
+ * ComparatorPredicate.getInstance(ONE, comparator).evaluate(TWO);
+ * 
+ * + * The input variable TWO in compared to the stored variable ONE using + * the supplied comparator. This is the default usage of the predicate and will return + * true if the underlying comparator returns 0. In addition to the default + * usage of the predicate, it is possible to evaluate the comparator's result in several ways. The + * following {@link Criterion} enumeration values are provided by the predicate: + *

+ * + *
    + *
  • EQUAL
  • + *
  • GREATER
  • + *
  • GREATER_OR_EQUAL
  • + *
  • LESS
  • + *
  • LESS_OR_EQUAL
  • + *
+ * + * The following examples demonstrates how these constants can be used in order to manipulate the + * evaluation of a comparator result. + * + *
+ * ComparatorPredicate.getInstance(ONE, comparator, ComparatorPredicate.Criterion.GREATER).evaluate(TWO);
+ * 
+ * + * The input variable TWO is compared to the stored variable ONE using the supplied comparator + * using the GREATER evaluation criterion constant. This instructs the predicate to + * return true if the comparator returns a value greater than 0. + * + * @since Commons Collections 4.0 + * @version $Revision$ + * + * @author Rune Peter Bjørnstad. + */ +public class ComparatorPredicate implements Predicate, Serializable { + + private static final long serialVersionUID = -1863209236504077399L; + + public enum Criterion { + EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL, + } + + // Instance variables: + + /** The internal object to compare with */ + private final T object; + + /** The comparator to use for comparison */ + private final Comparator comparator; + + /** The comparison evaluation criterion to use */ + private final Criterion criterion; + + /** + * Factory to create the comparator predicate + * + * @param object the object to compare to + * @param comparator the comparator to use for comparison + * @return the predicate + * @throws IllegalArgumentException if comparator is null + */ + public static Predicate comparatorPredicate(T object, Comparator comparator) { + return comparatorPredicate(object, comparator, Criterion.EQUAL); + } + + /** + * Factory to create the comparator predicate + * + * @param object the object to compare to + * @param comparator the comparator to use for comparison + * @param criterion the criterion to use to evaluate comparison + * @return the predicate + * @throws IllegalArgumentException if comparator is null of criterion is invalid + */ + public static Predicate comparatorPredicate(T object, Comparator comparator, Criterion criterion) { + if (comparator == null) { + throw new IllegalArgumentException("Comparator must not be null."); + } + if (criterion == null) { + throw new IllegalArgumentException("Criterion must not be null."); + } + return new ComparatorPredicate(object, comparator, criterion); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want. + * + * @param object the object to compare to + * @param comparator the comparator to use for comparison + * @param criterion the criterion to use to evaluate comparison + */ + public ComparatorPredicate(T object, Comparator comparator, Criterion criterion) { + super(); + this.object = object; + this.comparator = comparator; + this.criterion = criterion; + } + + /** + * Evaluates the predicate. The predicate evaluates to true in the following cases: + * + *
    + *
  • comparator.compare(object, input) == 0 && criterion == EQUAL
  • + *
  • comparator.compare(object, input) < 0 && criterion == LESS
  • + *
  • comparator.compare(object, input) > 0 && criterion == GREATER
  • + *
  • comparator.compare(object, input) >= 0 && criterion == GREATER_OR_EQUAL
  • + *
  • comparator.compare(object, input) <= 0 && criterion == LESS_OR_EQUAL
  • + *
+ * + * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object) + * @see java.util.Comparator#compare(java.lang.Object first, java.lang.Object second) + * + * @throws IllegalStateException if the criterion is invalid (really not possible) + */ + public boolean evaluate(T target) { + + boolean result = false; + int comparison = comparator.compare(object, target); + switch (criterion) { + case EQUAL: + result = (comparison == 0); + break; + case GREATER: + result = (comparison > 0); + break; + case LESS: + result = (comparison < 0); + break; + case GREATER_OR_EQUAL: + result = (comparison >= 0); + break; + case LESS_OR_EQUAL: + result = (comparison <= 0); + break; + default: + throw new IllegalStateException("The current criterion '" + criterion + "' is invalid."); + } + + return result; + } +} diff --git a/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java b/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java index 4d3a2588d..f7a1441b8 100644 --- a/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java +++ b/src/test/java/org/apache/commons/collections/AbstractDecoratedCollectionTest.java @@ -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 { - /** - * The {@link Collection} being decorated. - */ - protected Collection original; - /** - * The Collection under test that decorates {@link #original}. - */ - protected Collection decorated; - - @Before - public void setUpDecoratedCollection() throws Exception { - original = new ArrayList(); - } -} +/* + * 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 { + /** + * The {@link Collection} being decorated. + */ + protected Collection original; + /** + * The Collection under test that decorates {@link #original}. + */ + protected Collection decorated; + + @Before + public void setUpDecoratedCollection() throws Exception { + original = new ArrayList(); + } +} diff --git a/src/test/java/org/apache/commons/collections/MockTestCase.java b/src/test/java/org/apache/commons/collections/MockTestCase.java index 21ea8bfdc..6dfb14694 100644 --- a/src/test/java/org/apache/commons/collections/MockTestCase.java +++ b/src/test/java/org/apache/commons/collections/MockTestCase.java @@ -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 mockObjects = new ArrayList(); - - @SuppressWarnings("unchecked") - protected T createMock(Class name) { - T mock = (T) EasyMock.createMock(name); - return registerMock(mock); - } - - private T registerMock(T mock) { - mockObjects.add(mock); - return mock; - } - - protected IExpectationSetters expect(T t) { - return EasyMock.expect(t); - } - - protected final void replay() { - for (Object o : mockObjects) { - EasyMock.replay(o); - } - } - - protected final void verify() { - for (ListIterator 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 mockObjects = new ArrayList(); + + @SuppressWarnings("unchecked") + protected T createMock(Class name) { + T mock = (T) EasyMock.createMock(name); + return registerMock(mock); + } + + private T registerMock(T mock) { + mockObjects.add(mock); + return mock; + } + + protected IExpectationSetters expect(T t) { + return EasyMock.expect(t); + } + + protected final void replay() { + for (Object o : mockObjects) { + EasyMock.replay(o); + } + } + + protected final void verify() { + for (ListIterator i = mockObjects.listIterator(); i.hasNext();) { + try { + EasyMock.verify(i.next()); + } catch (AssertionError e) { + throw new AssertionError((i.previousIndex() + 1) + "" + + e.getMessage()); + } + } + } +} diff --git a/src/test/java/org/apache/commons/collections/TestIndexedCollection.java b/src/test/java/org/apache/commons/collections/TestIndexedCollection.java index 19e98f2a0..cd161810b 100644 --- a/src/test/java/org/apache/commons/collections/TestIndexedCollection.java +++ b/src/test/java/org/apache/commons/collections/TestIndexedCollection.java @@ -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 { - private IndexedCollection indexed; - - @Before - public void setUp() throws Exception { - indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer() { - 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() { - 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 { + private IndexedCollection indexed; + + @Before + public void setUp() throws Exception { + indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer() { + 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() { + 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)); + } +} diff --git a/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java b/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java index 522aa90c3..1138c2c41 100644 --- a/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java +++ b/src/test/java/org/apache/commons/collections/functors/BasicClosureTestBase.java @@ -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 Closure 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 Closure generateClosure(); +} diff --git a/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java b/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java index b66381f30..a0d686f89 100644 --- a/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java +++ b/src/test/java/org/apache/commons/collections/functors/TestCatchAndRethrowClosure.java @@ -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 Closure generateIOExceptionClosure() { - return new CatchAndRethrowClosure() { - - @Override - protected void executeAndThrow(T input) throws IOException { - throw new IOException(); - } - }; - } - - private static Closure generateNullPointerExceptionClosure() { - return new CatchAndRethrowClosure() { - - @Override - protected void executeAndThrow(T input) { - throw new NullPointerException(); - } - }; - } - - private static Closure generateNoExceptionClosure() { - return new CatchAndRethrowClosure() { - - @Override - protected void executeAndThrow(T input) { - } - }; - } - - @Override - protected Closure generateClosure() { - return generateNoExceptionClosure(); - } - - @Test - public void testThrowingClosure() { - Closure 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 Closure generateIOExceptionClosure() { + return new CatchAndRethrowClosure() { + + @Override + protected void executeAndThrow(T input) throws IOException { + throw new IOException(); + } + }; + } + + private static Closure generateNullPointerExceptionClosure() { + return new CatchAndRethrowClosure() { + + @Override + protected void executeAndThrow(T input) { + throw new NullPointerException(); + } + }; + } + + private static Closure generateNoExceptionClosure() { + return new CatchAndRethrowClosure() { + + @Override + protected void executeAndThrow(T input) { + } + }; + } + + @Override + protected Closure generateClosure() { + return generateNoExceptionClosure(); + } + + @Test + public void testThrowingClosure() { + Closure 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); + } + } +} diff --git a/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java b/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java index 8bcd9b724..e12511b7e 100644 --- a/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java +++ b/src/test/java/org/apache/commons/collections/functors/TestComparatorPredicate.java @@ -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> implements Comparator { - public int compare(T first, T second) { - return first.compareTo(second); - } - } - - @Test - public void compareEquals() { - Integer value = Integer.valueOf(10); - Predicate p = comparatorPredicate(value, new TestComparator()); - 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 p = comparatorPredicate(value, new TestComparator(), 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 p = comparatorPredicate(value, new TestComparator(), 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 p = comparatorPredicate(value, new TestComparator(), 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 p = comparatorPredicate(value, new TestComparator(), 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()); - } -} +/* + * 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> implements Comparator { + public int compare(T first, T second) { + return first.compareTo(second); + } + } + + @Test + public void compareEquals() { + Integer value = Integer.valueOf(10); + Predicate p = comparatorPredicate(value, new TestComparator()); + 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 p = comparatorPredicate(value, new TestComparator(), 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 p = comparatorPredicate(value, new TestComparator(), 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 p = comparatorPredicate(value, new TestComparator(), 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 p = comparatorPredicate(value, new TestComparator(), 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()); + } +}