View Javadoc
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 junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  
26  /**
27   * <p>
28   *  Test Case for the BeanComparator class.
29   *
30   * @version $Id$
31   */
32  
33  public class BeanComparatorTestCase extends TestCase {
34  
35      // ---------------------------------------------------- Instance Variables
36  
37      /**
38       * The test beans for each test.
39       */
40      protected TestBean bean = null;
41      protected AlphaBean alphaBean1 = null;
42      protected AlphaBean alphaBean2 = null;
43  
44  
45      // ---------------------------------------------------------- Constructors
46  
47      /**
48       * Construct a new instance of this test case.
49       *
50       * @param name Name of the test case
51       */
52      public BeanComparatorTestCase(final String name) {
53          super(name);
54      }
55  
56  
57      // -------------------------------------------------- Overall Test Methods
58  
59  
60      /**
61       * Set up instance variables required by this test case.
62       */
63      @Override
64      public void setUp() {
65          bean = new TestBean();
66          alphaBean1 = new AlphaBean("alphaBean1");
67          alphaBean2 = new AlphaBean("alphaBean2");
68  
69  
70      }
71  
72  
73      /**
74       * Return the tests included in this test suite.
75       */
76      public static Test suite() {
77          return (new TestSuite(BeanComparatorTestCase.class));
78      }
79  
80      /**
81       * Tear down instance variables required by this test case.
82       */
83      @Override
84      public void tearDown() {
85          bean = null;
86          alphaBean1 = null;
87          alphaBean2 = null;
88      }
89  
90  
91      // ------------------------------------------------ Individual Test Methods
92  
93  
94      /**
95       *  tests comparing two beans via their name using the default Comparator
96       */
97      public void testSimpleCompare() {
98          final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
99                  "name");
100         final int result = beanComparator.compare(alphaBean1, alphaBean2);
101         assertTrue("Comparator did not sort properly.  Result:" + result,
102                 result == -1);
103     }
104 
105     /**
106      *  tests comparing two beans via their name using the default Comparator, but the inverse
107      */
108     public void testSimpleCompareInverse() {
109         final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
110                 "name");
111         final int result = beanComparator.compare(alphaBean2, alphaBean1);
112         assertTrue("Comparator did not sort properly.  Result:" + result,
113                 result == 1);
114     }
115 
116     /**
117      *  tests comparing two beans via their name using the default Comparator where they have the same value.
118      */
119     public void testCompareIdentical() {
120         alphaBean1 = new AlphaBean("alphabean");
121         alphaBean2 = new AlphaBean("alphabean");
122         final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
123                 "name");
124         final int result = beanComparator.compare(alphaBean1, alphaBean2);
125         assertTrue("Comparator did not sort properly.  Result:" + result,
126                 result == 0);
127     }
128 
129     /**
130      *  tests comparing one bean against itself.
131      */
132     public void testCompareBeanAgainstSelf() {
133         final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
134                 "name");
135         final int result = beanComparator.compare(alphaBean1, alphaBean1);
136         assertTrue("Comparator did not sort properly.  Result:" + result,
137                 result == 0);
138     }
139 
140     /**
141      *  tests comparing two beans via their name using the default Comparator, but with one of the beans
142      *  being null.
143      */
144     public void testCompareWithNulls() {
145         try {
146           final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>("name");
147           beanComparator.compare(alphaBean2, null);
148 
149           fail("Should not be able to compare a null value.");
150         }
151         catch (final Exception e) {
152             // expected result
153         }
154     }
155 
156     /**
157      *  tests comparing two beans who don't have a property
158      */
159     public void testCompareOnMissingProperty() {
160         try {
161           final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>("bogusName");
162           beanComparator.compare(alphaBean2, alphaBean1);
163           fail("should not be able to compare");
164 
165 
166         }
167         catch (final Exception e) {
168           assertTrue("Wrong exception was thrown: " + e, e.toString().indexOf("Unknown property") > -1);
169         }
170     }
171 
172     /**
173      *  tests comparing two beans on a boolean property, which is not possible.
174      */
175     public void testCompareOnBooleanProperty() {
176         try {
177           final TestBean testBeanA = new TestBean();
178           final TestBean testBeanB = new TestBean();
179 
180           testBeanA.setBooleanProperty(true);
181           testBeanB.setBooleanProperty(false);
182 
183           final BeanComparator<TestBean> beanComparator = new BeanComparator<TestBean>("booleanProperty");
184           beanComparator.compare(testBeanA, testBeanB);
185 
186           // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
187           //      so this test no longer fails
188           // fail("BeanComparator should throw an exception when comparing two booleans.");
189 
190         }
191         catch (final ClassCastException cce){
192           // Expected result
193         }
194     }
195 
196     /**
197      *  tests comparing two beans on a boolean property, then changing the property and testing
198      */
199     public void testSetProperty() {
200         final TestBean testBeanA = new TestBean();
201         final TestBean testBeanB = new TestBean();
202 
203         testBeanA.setDoubleProperty(5.5);
204         testBeanB.setDoubleProperty(1.0);
205 
206         final BeanComparator<TestBean> beanComparator = new BeanComparator<TestBean>(
207                 "doubleProperty");
208         int result = beanComparator.compare(testBeanA, testBeanB);
209 
210         assertTrue("Comparator did not sort properly.  Result:" + result,
211                 result == 1);
212 
213         testBeanA.setStringProperty("string 1");
214         testBeanB.setStringProperty("string 2");
215 
216         beanComparator.setProperty("stringProperty");
217 
218         result = beanComparator.compare(testBeanA, testBeanB);
219 
220         assertTrue("Comparator did not sort properly.  Result:" + result,
221                 result == -1);
222     }
223 }