    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.commons.beanutils;
    19
    20
    21  import java.lang.reflect.InvocationTargetException;
    22  import java.util.HashMap;
    23  import java.util.Map;
    24  import junit.framework.TestCase;
    25  import junit.framework.Test;
    26  import junit.framework.TestSuite;
    27
    28
    29  /***
    30   * <p>
    31   *  Test Case for the BeanComparator class.
    32   *
    33   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    34   * @version $Revision: 557796 $
    35   */
    36
    37  public class BeanComparatorTestCase extends TestCase {
    38
    39      // ---------------------------------------------------- Instance Variables
    40
    41      /***
    42       * The test beans for each test.
    43       */
    44      protected TestBean bean = null;
    45      protected AlphaBean alphaBean1 = null;
    46      protected AlphaBean alphaBean2 = null;
    47
    48      // The test BeanComparator
    49      protected BeanComparator beanComparator = null;
    50
    51
    52
    53
    54
    55      // ---------------------------------------------------------- Constructors
    56
    57      /***
    58       * Construct a new instance of this test case.
    59       *
    60       * @param name Name of the test case
    61       */
    62      public BeanComparatorTestCase(String name) {
    63          super(name);
    64      }
    65
    66
    67      // -------------------------------------------------- Overall Test Methods
    68
    69
    70      /***
    71       * Set up instance variables required by this test case.
    72       */
    73      public void setUp() {
    74          bean = new TestBean();
    75          alphaBean1 = new AlphaBean("alphaBean1");
    76          alphaBean2 = new AlphaBean("alphaBean2");
    77
    78
    79      }
    80
    81
    82      /***
    83       * Return the tests included in this test suite.
    84       */
    85      public static Test suite() {
    86          return (new TestSuite(BeanComparatorTestCase.class));
    87      }
    88
    89      /***
    90       * Tear down instance variables required by this test case.
    91       */
    92      public void tearDown() {
    93          bean = null;
    94          alphaBean1 = null;
    95          alphaBean2 = null;
    96          beanComparator = null;
    97      }
    98
    99
    100     // ------------------------------------------------ Individual Test Methods
    101
    102
    103     /***
    104      *  tests comparing two beans via their name using the default Comparator
    105      */
    106     public void testSimpleCompare() {
    107         try {
    108           beanComparator = new BeanComparator("name");
    109           int result = beanComparator.compare(alphaBean1, alphaBean2);
    110           assertTrue("Comparator did not sort properly.  Result:" + result,result==-1);
    111
    112         }
    113         catch (Exception e) {
    114             fail("Exception");
    115         }
    116     }
    117
    118     /***
    119      *  tests comparing two beans via their name using the default Comparator, but the inverse
    120      */
    121     public void testSimpleCompareInverse() {
    122         try {
    123           beanComparator = new BeanComparator("name");
    124           int result = beanComparator.compare(alphaBean2, alphaBean1);
    125           assertTrue("Comparator did not sort properly.  Result:" + result,result==1);
    126
    127         }
    128         catch (Exception e) {
    129             fail("Exception" + e);
    130         }
    131     }
    132
    133     /***
    134      *  tests comparing two beans via their name using the default Comparator where they have the same value.
    135      */
    136     public void testCompareIdentical() {
    137         try {
    138           alphaBean1 = new AlphaBean("alphabean");
    139           alphaBean2 = new AlphaBean("alphabean");
    140           beanComparator = new BeanComparator("name");
    141           int result = beanComparator.compare(alphaBean1, alphaBean2);
    142           assertTrue("Comparator did not sort properly.  Result:" + result,result==0);
    143
    144         }
    145         catch (Exception e) {
    146             fail("Exception");
    147         }
    148     }
    149
    150     /***
    151      *  tests comparing one bean against itself.
    152      */
    153     public void testCompareBeanAgainstSelf() {
    154         try {
    155           beanComparator = new BeanComparator("name");
    156           int result = beanComparator.compare(alphaBean1, alphaBean1);
    157           assertTrue("Comparator did not sort properly.  Result:" + result,result==0);
    158
    159         }
    160         catch (Exception e) {
    161             fail("Exception");
    162         }
    163     }
    164
    165     /***
    166      *  tests comparing two beans via their name using the default Comparator, but with one of the beans
    167      *  being null.
    168      */
    169     public void testCompareWithNulls() {
    170         try {
    171           beanComparator = new BeanComparator("name");
    172           beanComparator.compare(alphaBean2, null);
    173
    174           // DEP not sure if this is the best way to test an exception?
    175           fail("Should not be able to compare a null value.");
    176
    177         }
    178         catch (Exception e) {
    179
    180         }
    181     }
    182
    183     /***
    184      *  tests comparing two beans who don't have a property
    185      */
    186     public void testCompareOnMissingProperty() {
    187         try {
    188           beanComparator = new BeanComparator("bogusName");
    189           beanComparator.compare(alphaBean2, alphaBean1);
    190           fail("should not be able to compare");
    191
    192
    193         }
    194         catch (Exception e) {
    195           assertTrue("Wrong exception was thrown: " + e, e.toString().indexOf("Unknown property") > -1);
    196         }
    197     }
    198
    199     /***
    200      *  tests comparing two beans on a boolean property, which is not possible.
    201      */
    202     public void testCompareOnBooleanProperty() {
    203         try {
    204           TestBean testBeanA = new TestBean();
    205           TestBean testBeanB = new TestBean();
    206
    207           testBeanA.setBooleanProperty(true);
    208           testBeanB.setBooleanProperty(false);
    209
    210           beanComparator = new BeanComparator("booleanProperty");
    211           beanComparator.compare(testBeanA, testBeanB);
    212
    213           // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
    214           //      so this test no longer fails
    215           // fail("BeanComparator should throw an exception when comparing two booleans.");
    216
    217         }
    218         catch (ClassCastException cce){
    219           ; // Expected result
    220         }
    221         catch (Exception e) {
    222             fail("Exception" + e);
    223         }
    224     }
    225
    226     /***
    227      *  tests comparing two beans on a boolean property, then changing the property and testing
    228      */
    229     public void testSetProperty() {
    230         try {
    231           TestBean testBeanA = new TestBean();
    232           TestBean testBeanB = new TestBean();
    233
    234           testBeanA.setDoubleProperty(5.5);
    235           testBeanB.setDoubleProperty(1.0);
    236
    237           beanComparator = new BeanComparator("doubleProperty");
    238           int result = beanComparator.compare(testBeanA, testBeanB);
    239
    240           assertTrue("Comparator did not sort properly.  Result:" + result,result==1);
    241
    242           testBeanA.setStringProperty("string 1");
    243           testBeanB.setStringProperty("string 2");
    244
    245           beanComparator.setProperty("stringProperty");
    246
    247           result = beanComparator.compare(testBeanA, testBeanB);
    248
    249           assertTrue("Comparator did not sort properly.  Result:" + result,result==-1);
    250
    251         }
    252         catch (ClassCastException cce){
    253           fail("ClassCaseException " + cce.toString());
    254         }
    255         catch (Exception e) {
    256           fail("Exception" + e);
    257         }
    258     }
    259 }
    260
    261