1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31 public class BeanComparatorTest {
32
33
34
35
36 protected TestBean bean;
37 protected AlphaBean alphaBean1;
38
39 protected AlphaBean alphaBean2;
40
41
42
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
53
54 @AfterEach
55 public void tearDown() {
56 bean = null;
57 alphaBean1 = null;
58 alphaBean2 = null;
59 }
60
61
62
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
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
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
99
100
101
102 } catch (final ClassCastException cce) {
103
104 }
105 }
106
107
108
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
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
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
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
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 }