This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git

commit b5e784d45bf10464427ebb7fc25382af970f2bb3
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Mon Oct 21 09:41:23 2019 -0400

    [BEANUTILS-527] Convert from Collections4 to java.util.function #8.
    
    Clean ups.
---
 .../apache/commons/beanutils2/BeanComparator.java  | 209 +++++-----
 .../org/apache/commons/beanutils2/AlphaBean.java   |  93 ++---
 .../commons/beanutils2/BeanComparatorTestCase.java | 444 ++++++++++-----------
 3 files changed, 359 insertions(+), 387 deletions(-)

diff --git a/src/main/java/org/apache/commons/beanutils2/BeanComparator.java 
b/src/main/java/org/apache/commons/beanutils2/BeanComparator.java
index dcb2d25..06f494d 100644
--- a/src/main/java/org/apache/commons/beanutils2/BeanComparator.java
+++ b/src/main/java/org/apache/commons/beanutils2/BeanComparator.java
@@ -21,165 +21,140 @@ import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Comparator;
 
-
 /**
  * <p>
- * This comparator compares two beans by the specified bean property.
- * It is also possible to compare beans based on nested, indexed,
- * combined, mapped bean properties. Please see the {@link PropertyUtilsBean}
- * documentation for all property name possibilities.
+ * This comparator compares two beans by the specified bean property. It is 
also possible to compare beans based on
+ * nested, indexed, combined, mapped bean properties. Please see the {@link 
PropertyUtilsBean} documentation for all
+ * property name possibilities.
  *
- * </p><p>
- * <strong>Note:</strong> The BeanComparator passes the values of the specified
- * bean property to a ComparableComparator, if no comparator is
- * specified in the constructor. If you are comparing two beans based
- * on a property that could contain "null" values, a suitable 
<code>Comparator</code>
- * or <code>ComparatorChain</code> should be supplied in the constructor.
- * Note that the passed in {@code Comparator} must be able to handle the
- * passed in objects. Because the type of the property to be compared is not
- * known at compile time no type checks can be performed by the compiler.
- * Thus {@code ClassCastException} exceptions can be thrown if unexpected
- * property values occur.
+ * </p>
+ * <p>
+ * <strong>Note:</strong> The BeanComparator passes the values of the 
specified bean property to an internal natural
+ * order {@link Comparator}, if no comparator is specified in the constructor. 
If you are comparing two beans based on a
+ * property that could contain "null" values, a suitable 
<code>Comparator</code> or Apache Commons Collection
+ * <code>ComparatorChain</code> should be supplied in the constructor. Note 
that the passed in {@code Comparator} must
+ * be able to handle the passed in objects. Because the type of the property 
to be compared is not known at compile time
+ * no type checks can be performed by the compiler. Thus {@code 
ClassCastException} exceptions can be thrown if
+ * unexpected property values occur.
  * </p>
  *
  * @param <T> the type of beans to be compared by this {@code Comparator}
+ * @param <V> the type of property to compare
  */
-public class BeanComparator<T> implements Comparator<T>, Serializable {
+public class BeanComparator<T, V> implements Comparator<T>, Serializable {
 
     private static final long serialVersionUID = 1L;
     private String property;
-    private final Comparator<?> comparator;
+    private final Comparator<V> comparator;
 
     /**
-     * <p>Constructs a Bean Comparator without a property set.
-     * </p><p>
-     * <strong>Note</strong> that this is intended to be used
-     * only in bean-centric environments.
-     * </p><p>
-     * Until {@link #setProperty} is called with a non-null value.
-     * this comparator will compare the Objects only.
+     * <p>
+     * Constructs a Bean Comparator without a property set.
+     * </p>
+     * <p>
+     * <strong>Note</strong> that this is intended to be used only in 
bean-centric environments.
+     * </p>
+     * <p>
+     * Until {@link #setProperty} is called with a non-null value. this 
comparator will compare the Objects only.
      * </p>
      */
     public BeanComparator() {
-        this( null );
+        this(null);
     }
 
     /**
-     * <p>Constructs a property-based comparator for beans.
-     * This compares two beans by the property
-     * specified in the property parameter. This constructor creates
-     * a <code>BeanComparator</code> that uses a 
<code>ComparableComparator</code>
-     * to compare the property values.
+     * <p>
+     * Constructs a property-based comparator for beans. This compares two 
beans by the property specified in the
+     * property parameter. This constructor creates a 
<code>BeanComparator</code> that uses a
+     * <code>ComparableComparator</code> to compare the property values.
      * </p>
      *
-     * <p>Passing "null" to this constructor will cause the BeanComparator
-     * to compare objects based on natural order, that is
-     * <code>java.lang.Comparable</code>.
+     * <p>
+     * Passing "null" to this constructor will cause the BeanComparator to 
compare objects based on natural order, that
+     * is <code>java.lang.Comparable</code>.
      * </p>
      *
-     * @param property String Name of a bean property, which may contain the
-     * name of a simple, nested, indexed, mapped, or combined
-     * property. See {@link PropertyUtilsBean} for property query language 
syntax.
-     * If the property passed in is null then the actual objects will be 
compared
+     * @param property String Name of a bean property, which may contain the 
name of a simple, nested, indexed, mapped,
+     *        or combined property. See {@link PropertyUtilsBean} for property 
query language syntax. If the property
+     *        passed in is null then the actual objects will be compared
      */
-    public BeanComparator( final String property ) {
-        this( property, NaturalOrderComparator.INSTANCE );
+    public BeanComparator(final String property) {
+        this(property, NaturalOrderComparator.INSTANCE);
     }
 
     /**
-     * Constructs a property-based comparator for beans.
-     * This constructor creates
-     * a BeanComparator that uses the supplied Comparator to compare
-     * the property values.
+     * Constructs a property-based comparator for beans. This constructor 
creates a BeanComparator that uses the
+     * supplied Comparator to compare the property values.
      *
-     * @param property Name of a bean property, can contain the name
-     * of a simple, nested, indexed, mapped, or combined
-     * property. See {@link PropertyUtilsBean} for property query language
-     * syntax.
-     * @param comparator BeanComparator will pass the values of the
-     * specified bean property to this Comparator.
-     * If your bean property is not a comparable or
-     * contains null values, a suitable comparator
-     * may be supplied in this constructor.
+     * @param property Name of a bean property, can contain the name of a 
simple, nested, indexed, mapped, or combined
+     *        property. See {@link PropertyUtilsBean} for property query 
language syntax.
+     * @param comparator BeanComparator will pass the values of the specified 
bean property to this Comparator. If your
+     *        bean property is not a comparable or contains null values, a 
suitable comparator may be supplied in this
+     *        constructor.
      */
-    public BeanComparator( final String property, final Comparator<?> 
comparator ) {
-        setProperty( property );
-        if (comparator != null) {
-            this.comparator = comparator;
-        } else {
-            this.comparator = NaturalOrderComparator.INSTANCE;
-        }
+    public BeanComparator(final String property, final Comparator<V> 
comparator) {
+        setProperty(property);
+        this.comparator = comparator != null ? comparator : 
NaturalOrderComparator.INSTANCE;
     }
 
     /**
      * Sets the method to be called to compare two JavaBeans
      *
-     * @param property String method name to call to compare
-     * If the property passed in is null then the actual objects will be 
compared
+     * @param property String method name to call to compare If the property 
passed in is null then the actual objects
+     *        will be compared
      */
-    public void setProperty( final String property ) {
+    public void setProperty(final String property) {
         this.property = property;
     }
 
-
     /**
      * Gets the property attribute of the BeanComparator
      *
-     * @return String method name to call to compare.
-     * A null value indicates that the actual objects will be compared
+     * @return String method name to call to compare. A null value indicates 
that the actual objects will be compared
      */
     public String getProperty() {
         return property;
     }
 
-
     /**
      * Gets the Comparator being used to compare beans.
      *
      * @return the Comparator being used to compare beans
      */
-    public Comparator<?> getComparator() {
+    public Comparator<V> getComparator() {
         return comparator;
     }
 
-
     /**
-     * Compare two JavaBeans by their shared property.
-     * If {@link #getProperty} is null then the actual objects will be 
compared.
+     * Compare two JavaBeans by their shared property. If {@link #getProperty} 
is null then the actual objects will be
+     * compared.
      *
-     * @param  o1 Object The first bean to get data from to compare against
-     * @param  o2 Object The second bean to get data from to compare
+     * @param o1 Object The first bean to get data from to compare against
+     * @param o2 Object The second bean to get data from to compare
      * @return int negative or positive based on order
      */
     @Override
-    public int compare( final T o1, final T o2 ) {
+    public int compare(final T o1, final T o2) {
 
-        if ( property == null ) {
+        if (property == null) {
             // compare the actual objects
-            return internalCompare( o1, o2 );
+            return internalCompare(o1, o2);
         }
 
         try {
-            final Object value1 = PropertyUtils.getProperty( o1, property );
-            final Object value2 = PropertyUtils.getProperty( o2, property );
-            return internalCompare( value1, value2 );
-        }
-        catch ( final IllegalAccessException iae ) {
-            throw new RuntimeException( "IllegalAccessException: " + 
iae.toString() );
-        }
-        catch ( final InvocationTargetException ite ) {
-            throw new RuntimeException( "InvocationTargetException: " + 
ite.toString() );
-        }
-        catch ( final NoSuchMethodException nsme ) {
-            throw new RuntimeException( "NoSuchMethodException: " + 
nsme.toString() );
+            final Object value1 = PropertyUtils.getProperty(o1, property);
+            final Object value2 = PropertyUtils.getProperty(o2, property);
+            return internalCompare(value1, value2);
+        } catch (final NoSuchMethodException | IllegalAccessException | 
InvocationTargetException e) {
+            throw new RuntimeException(e.getClass().getSimpleName()+": " + 
e.toString());
         }
     }
 
     /**
-     * Two <code>BeanComparator</code>'s are equals if and only if
-     * the wrapped comparators and the property names to be compared
-     * are equal.
-     * @param  o Comparator to compare to
+     * Two <code>BeanComparator</code>'s are equals if and only if the wrapped 
comparators and the property names to be
+     * compared are equal.
+     *
+     * @param o Comparator to compare to
      * @return whether the the comparators are equal or not
      */
     @Override
@@ -191,19 +166,16 @@ public class BeanComparator<T> implements Comparator<T>, 
Serializable {
             return false;
         }
 
-        final BeanComparator<?> beanComparator = (BeanComparator<?>) o;
+        final BeanComparator<?, ?> beanComparator = (BeanComparator<?, ?>) o;
 
         if (!comparator.equals(beanComparator.comparator)) {
             return false;
         }
-        if (property != null)
-        {
+        if (property != null) {
             if (!property.equals(beanComparator.property)) {
                 return false;
             }
-        }
-        else
-        {
+        } else {
             return beanComparator.property == null;
         }
 
@@ -212,6 +184,7 @@ public class BeanComparator<T> implements Comparator<T>, 
Serializable {
 
     /**
      * Hashcode compatible with equals.
+     *
      * @return the hash code for this comparator
      */
     @Override
@@ -220,38 +193,33 @@ public class BeanComparator<T> implements Comparator<T>, 
Serializable {
     }
 
     /**
-     * Compares the given values using the internal {@code Comparator}.
-     * <em>Note</em>: This comparison cannot be performed in a type-safe way; 
so
-     * {@code ClassCastException} exceptions may be thrown.
+     * Compares the given values using the internal {@code Comparator}. 
<em>Note</em>: This comparison cannot be
+     * performed in a type-safe way; so {@code ClassCastException} exceptions 
may be thrown.
      *
      * @param val1 the first value to be compared
      * @param val2 the second value to be compared
      * @return the result of the comparison
      */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
     private int internalCompare(final Object val1, final Object val2) {
-        @SuppressWarnings("rawtypes")
-        final
-        // to make the compiler happy
-        Comparator c = comparator;
-        return c.compare(val1, val2);
+        return ((Comparator) comparator).compare(val1, val2);
     }
-    
+
     /**
-     * A {@link Comparator Comparator} that compares {@link Comparable 
Comparable}
-     * objects.
+     * A {@link Comparator Comparator} that compares {@link Comparable 
Comparable} objects.
      * <p>
-     * This Comparator is useful, for example, for enforcing the natural order 
in
-     * custom implementations of {@link java.util.SortedSet SortedSet} and
-     * {@link java.util.SortedMap SortedMap}.
+     * This Comparator is useful, for example, for enforcing the natural order 
in custom implementations of
+     * {@link java.util.SortedSet SortedSet} and {@link java.util.SortedMap 
SortedMap}.
      * </p>
      *
      * @param <E> the type of objects compared by this comparator
      * @see java.util.Collections#reverseOrder()
      */
-    private static class NaturalOrderComparator<E extends Comparable<? super 
E>> implements Comparator<E>, Serializable {
+    private static class NaturalOrderComparator<E extends Comparable<? super 
E>>
+            implements Comparator<E>, Serializable {
 
         /** Serialization version. */
-        private static final long serialVersionUID=-291439688585137865L;
+        private static final long serialVersionUID = -291439688585137865L;
 
         /** The singleton instance. */
         @SuppressWarnings("rawtypes")
@@ -265,9 +233,11 @@ public class BeanComparator<T> implements Comparator<T>, 
Serializable {
         }
 
         /**
-         * Compare the two {@link Comparable Comparable} arguments.
-         * This method is equivalent to:
-         * <pre>((Comparable)obj1).compareTo(obj2)</pre>
+         * Compare the two {@link Comparable Comparable} arguments. This 
method is equivalent to:
+         *
+         * <pre>
+         * ((Comparable) obj1).compareTo(obj2)
+         * </pre>
          */
         @Override
         public int compare(final E obj1, final E obj2) {
@@ -276,13 +246,12 @@ public class BeanComparator<T> implements Comparator<T>, 
Serializable {
 
         @Override
         public int hashCode() {
-            return "NaturalOrderingComparator".hashCode();
+            return "NaturalOrderComparator".hashCode();
         }
 
         @Override
         public boolean equals(final Object object) {
-            return this == object ||
-                   null != object && object.getClass().equals(this.getClass());
+            return this == object || null != object && 
object.getClass().equals(this.getClass());
         }
     }
 }
diff --git a/src/test/java/org/apache/commons/beanutils2/AlphaBean.java 
b/src/test/java/org/apache/commons/beanutils2/AlphaBean.java
index 0a8dabe..c4da8a4 100644
--- a/src/test/java/org/apache/commons/beanutils2/AlphaBean.java
+++ b/src/test/java/org/apache/commons/beanutils2/AlphaBean.java
@@ -1,45 +1,48 @@
-/*
- * 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.beanutils2;
-
-/**
- */
-public class AlphaBean extends AbstractParent implements Child {
-
-    private String name;
-
-    public AlphaBean() {}
-
-    public AlphaBean(final String name) {
-        setName(name);
-    }
-
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    public void setName(final String name) {
-        this.name = name;
-    }
-
-    /**
-     * Used for testing that correct exception is thrown.
-     */
-    public void bogus(final String badParameter){}
-}
+/*
+ * 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.beanutils2;
+
+/**
+ */
+public class AlphaBean extends AbstractParent implements Child {
+
+    private String name;
+
+    public AlphaBean() {
+    }
+
+    public AlphaBean(final String name) {
+        setName(name);
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    public void setName(final String name) {
+        this.name = name;
+    }
+
+    /**
+     * Used for testing that correct exception is thrown.
+     */
+    public void bogus(final String badParameter) {
+        // noop
+    }
+}
diff --git 
a/src/test/java/org/apache/commons/beanutils2/BeanComparatorTestCase.java 
b/src/test/java/org/apache/commons/beanutils2/BeanComparatorTestCase.java
index 0e1cfe7..df59e24 100644
--- a/src/test/java/org/apache/commons/beanutils2/BeanComparatorTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/BeanComparatorTestCase.java
@@ -1,222 +1,222 @@
-/*
- * 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.beanutils2;
-
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-
-/**
- * <p>
- *  Test Case for the BeanComparator class.
- *
- */
-
-public class BeanComparatorTestCase extends TestCase {
-
-    // ---------------------------------------------------- Instance Variables
-
-    /**
-     * The test beans for each test.
-     */
-    protected TestBean bean = null;
-    protected AlphaBean alphaBean1 = null;
-    protected AlphaBean alphaBean2 = null;
-
-
-    // ---------------------------------------------------------- Constructors
-
-    /**
-     * Construct a new instance of this test case.
-     *
-     * @param name Name of the test case
-     */
-    public BeanComparatorTestCase(final String name) {
-        super(name);
-    }
-
-
-    // -------------------------------------------------- Overall Test Methods
-
-
-    /**
-     * Set up instance variables required by this test case.
-     */
-    @Override
-    public void setUp() {
-        bean = new TestBean();
-        alphaBean1 = new AlphaBean("alphaBean1");
-        alphaBean2 = new AlphaBean("alphaBean2");
-
-
-    }
-
-
-    /**
-     * Return the tests included in this test suite.
-     */
-    public static Test suite() {
-        return new TestSuite(BeanComparatorTestCase.class);
-    }
-
-    /**
-     * Tear down instance variables required by this test case.
-     */
-    @Override
-    public void tearDown() {
-        bean = null;
-        alphaBean1 = null;
-        alphaBean2 = null;
-    }
-
-
-    // ------------------------------------------------ Individual Test Methods
-
-
-    /**
-     *  tests comparing two beans via their name using the default Comparator
-     */
-    public void testSimpleCompare() {
-        final BeanComparator<AlphaBean> beanComparator = new BeanComparator<>(
-                "name");
-        final int result = beanComparator.compare(alphaBean1, alphaBean2);
-        assertTrue("Comparator did not sort properly.  Result:" + result,
-                result == -1);
-    }
-
-    /**
-     *  tests comparing two beans via their name using the default Comparator, 
but the inverse
-     */
-    public void testSimpleCompareInverse() {
-        final BeanComparator<AlphaBean> beanComparator = new BeanComparator<>(
-                "name");
-        final int result = beanComparator.compare(alphaBean2, alphaBean1);
-        assertTrue("Comparator did not sort properly.  Result:" + result,
-                result == 1);
-    }
-
-    /**
-     *  tests comparing two beans via their name using the default Comparator 
where they have the same value.
-     */
-    public void testCompareIdentical() {
-        alphaBean1 = new AlphaBean("alphabean");
-        alphaBean2 = new AlphaBean("alphabean");
-        final BeanComparator<AlphaBean> beanComparator = new BeanComparator<>(
-                "name");
-        final int result = beanComparator.compare(alphaBean1, alphaBean2);
-        assertTrue("Comparator did not sort properly.  Result:" + result,
-                result == 0);
-    }
-
-    /**
-     *  tests comparing one bean against itself.
-     */
-    public void testCompareBeanAgainstSelf() {
-        final BeanComparator<AlphaBean> beanComparator = new BeanComparator<>(
-                "name");
-        final int result = beanComparator.compare(alphaBean1, alphaBean1);
-        assertTrue("Comparator did not sort properly.  Result:" + result,
-                result == 0);
-    }
-
-    /**
-     *  tests comparing two beans via their name using the default Comparator, 
but with one of the beans
-     *  being null.
-     */
-    public void testCompareWithNulls() {
-        try {
-          final BeanComparator<AlphaBean> beanComparator = new 
BeanComparator<>("name");
-          beanComparator.compare(alphaBean2, null);
-
-          fail("Should not be able to compare a null value.");
-        }
-        catch (final Exception e) {
-            // expected result
-        }
-    }
-
-    /**
-     *  tests comparing two beans who don't have a property
-     */
-    public void testCompareOnMissingProperty() {
-        try {
-          final BeanComparator<AlphaBean> beanComparator = new 
BeanComparator<>("bogusName");
-          beanComparator.compare(alphaBean2, alphaBean1);
-          fail("should not be able to compare");
-
-
-        }
-        catch (final Exception e) {
-          assertTrue("Wrong exception was thrown: " + e, 
e.toString().indexOf("Unknown property") > -1);
-        }
-    }
-
-    /**
-     *  tests comparing two beans on a boolean property, which is not possible.
-     */
-    public void testCompareOnBooleanProperty() {
-        try {
-          final TestBean testBeanA = new TestBean();
-          final TestBean testBeanB = new TestBean();
-
-          testBeanA.setBooleanProperty(true);
-          testBeanB.setBooleanProperty(false);
-
-          final BeanComparator<TestBean> beanComparator = new 
BeanComparator<>("booleanProperty");
-          beanComparator.compare(testBeanA, testBeanB);
-
-          // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
-          //      so this test no longer fails
-          // fail("BeanComparator should throw an exception when comparing two 
booleans.");
-
-        }
-        catch (final ClassCastException cce){
-          // Expected result
-        }
-    }
-
-    /**
-     *  tests comparing two beans on a boolean property, then changing the 
property and testing
-     */
-    public void testSetProperty() {
-        final TestBean testBeanA = new TestBean();
-        final TestBean testBeanB = new TestBean();
-
-        testBeanA.setDoubleProperty(5.5);
-        testBeanB.setDoubleProperty(1.0);
-
-        final BeanComparator<TestBean> beanComparator = new BeanComparator<>(
-                "doubleProperty");
-        int result = beanComparator.compare(testBeanA, testBeanB);
-
-        assertTrue("Comparator did not sort properly.  Result:" + result,
-                result == 1);
-
-        testBeanA.setStringProperty("string 1");
-        testBeanB.setStringProperty("string 2");
-
-        beanComparator.setProperty("stringProperty");
-
-        result = beanComparator.compare(testBeanA, testBeanB);
-
-        assertTrue("Comparator did not sort properly.  Result:" + result,
-                result == -1);
-    }
-}
+/*
+ * 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.beanutils2;
+
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * <p>
+ *  Test Case for the BeanComparator class.
+ *
+ */
+
+public class BeanComparatorTestCase extends TestCase {
+
+    // ---------------------------------------------------- Instance Variables
+
+    /**
+     * The test beans for each test.
+     */
+    protected TestBean bean = null;
+    protected AlphaBean alphaBean1 = null;
+    protected AlphaBean alphaBean2 = null;
+
+
+    // ---------------------------------------------------------- Constructors
+
+    /**
+     * Construct a new instance of this test case.
+     *
+     * @param name Name of the test case
+     */
+    public BeanComparatorTestCase(final String name) {
+        super(name);
+    }
+
+
+    // -------------------------------------------------- Overall Test Methods
+
+
+    /**
+     * Set up instance variables required by this test case.
+     */
+    @Override
+    public void setUp() {
+        bean = new TestBean();
+        alphaBean1 = new AlphaBean("alphaBean1");
+        alphaBean2 = new AlphaBean("alphaBean2");
+
+
+    }
+
+
+    /**
+     * Return the tests included in this test suite.
+     */
+    public static Test suite() {
+        return new TestSuite(BeanComparatorTestCase.class);
+    }
+
+    /**
+     * Tear down instance variables required by this test case.
+     */
+    @Override
+    public void tearDown() {
+        bean = null;
+        alphaBean1 = null;
+        alphaBean2 = null;
+    }
+
+
+    // ------------------------------------------------ Individual Test Methods
+
+
+    /**
+     *  tests comparing two beans via their name using the default Comparator
+     */
+    public void testSimpleCompare() {
+        final BeanComparator<AlphaBean, String> beanComparator = new 
BeanComparator<>(
+                "name");
+        final int result = beanComparator.compare(alphaBean1, alphaBean2);
+        assertTrue("Comparator did not sort properly.  Result:" + result,
+                result == -1);
+    }
+
+    /**
+     *  tests comparing two beans via their name using the default Comparator, 
but the inverse
+     */
+    public void testSimpleCompareInverse() {
+        final BeanComparator<AlphaBean, String> beanComparator = new 
BeanComparator<>(
+                "name");
+        final int result = beanComparator.compare(alphaBean2, alphaBean1);
+        assertTrue("Comparator did not sort properly.  Result:" + result,
+                result == 1);
+    }
+
+    /**
+     *  tests comparing two beans via their name using the default Comparator 
where they have the same value.
+     */
+    public void testCompareIdentical() {
+        alphaBean1 = new AlphaBean("alphabean");
+        alphaBean2 = new AlphaBean("alphabean");
+        final BeanComparator<AlphaBean, String> beanComparator = new 
BeanComparator<>(
+                "name");
+        final int result = beanComparator.compare(alphaBean1, alphaBean2);
+        assertTrue("Comparator did not sort properly.  Result:" + result,
+                result == 0);
+    }
+
+    /**
+     *  tests comparing one bean against itself.
+     */
+    public void testCompareBeanAgainstSelf() {
+        final BeanComparator<AlphaBean, String> beanComparator = new 
BeanComparator<>(
+                "name");
+        final int result = beanComparator.compare(alphaBean1, alphaBean1);
+        assertTrue("Comparator did not sort properly.  Result:" + result,
+                result == 0);
+    }
+
+    /**
+     *  tests comparing two beans via their name using the default Comparator, 
but with one of the beans
+     *  being null.
+     */
+    public void testCompareWithNulls() {
+        try {
+          final BeanComparator<AlphaBean, String> beanComparator = new 
BeanComparator<>("name");
+          beanComparator.compare(alphaBean2, null);
+
+          fail("Should not be able to compare a null value.");
+        }
+        catch (final Exception e) {
+            // expected result
+        }
+    }
+
+    /**
+     *  tests comparing two beans who don't have a property
+     */
+    public void testCompareOnMissingProperty() {
+        try {
+          final BeanComparator<AlphaBean, String> beanComparator = new 
BeanComparator<>("bogusName");
+          beanComparator.compare(alphaBean2, alphaBean1);
+          fail("should not be able to compare");
+
+
+        }
+        catch (final Exception e) {
+          assertTrue("Wrong exception was thrown: " + e, 
e.toString().indexOf("Unknown property") > -1);
+        }
+    }
+
+    /**
+     *  tests comparing two beans on a boolean property, which is not possible.
+     */
+    public void testCompareOnBooleanProperty() {
+        try {
+          final TestBean testBeanA = new TestBean();
+          final TestBean testBeanB = new TestBean();
+
+          testBeanA.setBooleanProperty(true);
+          testBeanB.setBooleanProperty(false);
+
+          final BeanComparator<TestBean, String> beanComparator = new 
BeanComparator<>("booleanProperty");
+          beanComparator.compare(testBeanA, testBeanB);
+
+          // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
+          //      so this test no longer fails
+          // fail("BeanComparator should throw an exception when comparing two 
booleans.");
+
+        }
+        catch (final ClassCastException cce){
+          // Expected result
+        }
+    }
+
+    /**
+     *  tests comparing two beans on a boolean property, then changing the 
property and testing
+     */
+    public void testSetProperty() {
+        final TestBean testBeanA = new TestBean();
+        final TestBean testBeanB = new TestBean();
+
+        testBeanA.setDoubleProperty(5.5);
+        testBeanB.setDoubleProperty(1.0);
+
+        final BeanComparator<TestBean, String> beanComparator = new 
BeanComparator<>(
+                "doubleProperty");
+        int result = beanComparator.compare(testBeanA, testBeanB);
+
+        assertTrue("Comparator did not sort properly.  Result:" + result,
+                result == 1);
+
+        testBeanA.setStringProperty("string 1");
+        testBeanB.setStringProperty("string 2");
+
+        beanComparator.setProperty("stringProperty");
+
+        result = beanComparator.compare(testBeanA, testBeanB);
+
+        assertTrue("Comparator did not sort properly.  Result:" + result,
+                result == -1);
+    }
+}

Reply via email to