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    *      https://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.beanutils2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.junit.jupiter.api.AfterEach;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Test Case for the BeanComparator class.
30   */
31  public class BeanComparatorTest {
32  
33      /**
34       * The test beans for each test.
35       */
36      protected TestBean bean;
37      protected AlphaBean alphaBean1;
38  
39      protected AlphaBean alphaBean2;
40  
41      /**
42       * Sets up instance variables required by this test case.
43       */
44      @BeforeEach
45      public void setUp() {
46          bean = new TestBean();
47          alphaBean1 = new AlphaBean("alphaBean1");
48          alphaBean2 = new AlphaBean("alphaBean2");
49      }
50  
51      /**
52       * Tears down instance variables required by this test case.
53       */
54      @AfterEach
55      public void tearDown() {
56          bean = null;
57          alphaBean1 = null;
58          alphaBean2 = null;
59      }
60  
61      /**
62       * Tests comparing one bean against itself.
63       */
64      @Test
65      public void testCompareBeanAgainstSelf() {
66          final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
67          final int result = beanComparator.compare(alphaBean1, alphaBean1);
68          assertEquals(0, result, () -> "Comparator did not sort properly.  Result:" + result);
69      }
70  
71      /**
72       * Tests comparing two beans via their name using the default Comparator where they have the same value.
73       */
74      @Test
75      public void testCompareIdentical() {
76          alphaBean1 = new AlphaBean("alphabean");
77          alphaBean2 = new AlphaBean("alphabean");
78          final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
79          final int result = beanComparator.compare(alphaBean1, alphaBean2);
80          assertEquals(0, result, () -> "Comparator did not sort properly.  Result:" + result);
81      }
82  
83      /**
84       * Tests comparing two beans on a boolean property, which is not possible.
85       */
86      @Test
87      public void testCompareOnBooleanProperty() {
88          try {
89              final TestBean testBeanA = new TestBean();
90              final TestBean testBeanB = new TestBean();
91  
92              testBeanA.setBooleanProperty(true);
93              testBeanB.setBooleanProperty(false);
94  
95              final BeanComparator<TestBean, String> beanComparator = new BeanComparator<>("booleanProperty");
96              beanComparator.compare(testBeanA, testBeanB);
97  
98              // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
99              // so this test no longer fails
100             // fail("BeanComparator should throw an exception when comparing two booleans.");
101 
102         } catch (final ClassCastException cce) {
103             // Expected result
104         }
105     }
106 
107     /**
108      * Tests comparing two beans who don't have a property
109      */
110     @Test
111     public void testCompareOnMissingProperty() {
112         final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("bogusName");
113         final Exception e = assertThrows(RuntimeException.class, () -> beanComparator.compare(alphaBean2, alphaBean1));
114         assertTrue(e.toString().contains("Unknown property"), () -> "Wrong exception was thrown: " + e);
115     }
116 
117     /**
118      * Tests comparing two beans via their name using the default Comparator, but with one of the beans being null.
119      */
120     @Test
121     public void testCompareWithNulls() {
122         final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
123         assertThrows(NullPointerException.class, () -> beanComparator.compare(alphaBean2, null));
124     }
125 
126     /**
127      * Tests comparing two beans on a boolean property, then changing the property and testing/
128      */
129     @Test
130     public void testSetProperty() {
131         final TestBean testBeanA = new TestBean();
132         final TestBean testBeanB = new TestBean();
133 
134         testBeanA.setDoubleProperty(5.5);
135         testBeanB.setDoubleProperty(1.0);
136 
137         final BeanComparator<TestBean, String> beanComparator = new BeanComparator<>("doubleProperty");
138         final int result1 = beanComparator.compare(testBeanA, testBeanB);
139 
140         assertEquals(1, result1, () -> "Comparator did not sort properly.  Result:" + result1);
141 
142         testBeanA.setStringProperty("string 1");
143         testBeanB.setStringProperty("string 2");
144 
145         beanComparator.setProperty("stringProperty");
146 
147         final int result2 = beanComparator.compare(testBeanA, testBeanB);
148 
149         assertEquals(-1, result2, () -> "Comparator did not sort properly.  Result:" + result2);
150     }
151 
152     /**
153      * Tests comparing two beans via their name using the default Comparator
154      */
155     @Test
156     public void testSimpleCompare() {
157         final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
158         final int result = beanComparator.compare(alphaBean1, alphaBean2);
159         assertEquals(-1, result, () -> "Comparator did not sort properly.  Result:" + result);
160     }
161 
162     /**
163      * Tests comparing two beans via their name using the default Comparator, but the inverse
164      */
165     @Test
166     public void testSimpleCompareInverse() {
167         final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
168         final int result = beanComparator.compare(alphaBean2, alphaBean1);
169         assertEquals(1, result, () -> "Comparator did not sort properly.  Result:" + result);
170     }
171 }