|
17 | 17 |
|
18 | 18 | package org.apache.commons.beanutils2;
|
19 | 19 |
|
| 20 | +import static org.junit.jupiter.api.Assertions.assertAll; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
21 | 23 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
22 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
23 | 25 |
|
| 26 | +import java.util.Comparator; |
| 27 | + |
24 | 28 | import org.junit.jupiter.api.AfterEach;
|
25 | 29 | import org.junit.jupiter.api.BeforeEach;
|
26 | 30 | import org.junit.jupiter.api.Test;
|
@@ -168,4 +172,96 @@ public void testSimpleCompareInverse() {
|
168 | 172 | final int result = beanComparator.compare(alphaBean2, alphaBean1);
|
169 | 173 | assertEquals(1, result, () -> "Comparator did not sort properly. Result:" + result);
|
170 | 174 | }
|
| 175 | + |
| 176 | + /** |
| 177 | + * Tests comparing two beans via their name using the default natural order Comparator |
| 178 | + */ |
| 179 | + @Test |
| 180 | + public void testSimpleCompareWithDefaultNaturalComparator() { |
| 181 | + final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name", null); |
| 182 | + final int result = beanComparator.compare(alphaBean1, alphaBean2); |
| 183 | + assertEquals(-1, result, () -> "Comparator did not sort properly. Result:" + result); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Tests comparing two beans via their name using the default natural order Comparator, but the inverse |
| 188 | + */ |
| 189 | + @Test |
| 190 | + public void testSimpleCompareInverseWithDefaultNaturalComparator() { |
| 191 | + final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name", null); |
| 192 | + final int result = beanComparator.compare(alphaBean2, alphaBean1); |
| 193 | + assertEquals(1, result, () -> "Comparator did not sort properly. Result:" + result); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Tests comparing two comparable beans using the default natural order Comparator |
| 198 | + */ |
| 199 | + @Test |
| 200 | + public void testNaturalCompare() { |
| 201 | + final BeanComparator<String, ?> beanComparator = new BeanComparator<>(); |
| 202 | + final int result = beanComparator.compare("string1", "string2"); |
| 203 | + assertEquals(-1, result, () -> "Comparator did not sort properly. Result:" + result); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Tests comparing two comparable beans using the default natural order Comparator, but the inverse |
| 208 | + */ |
| 209 | + @Test |
| 210 | + public void testNaturalCompareInverse() { |
| 211 | + final BeanComparator<String, ?> beanComparator = new BeanComparator<>(); |
| 212 | + final int result = beanComparator.compare("string2", "string1"); |
| 213 | + assertEquals(1, result, () -> "Comparator did not sort properly. Result:" + result); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Tests comparing two beans via their name using the default Comparator |
| 218 | + */ |
| 219 | + @Test |
| 220 | + public void testWithCustomComparator() { |
| 221 | + final Comparator<AlphaBean> comparator = Comparator.comparing(AlphaBean::getName); |
| 222 | + |
| 223 | + final BeanComparator<AlphaBean, ?> beanComparator = new BeanComparator<>(null, comparator); |
| 224 | + final int result = beanComparator.compare(alphaBean1, alphaBean2); |
| 225 | + assertEquals(-1, result, () -> "Comparator did not sort properly. Result:" + result); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Tests comparing two beans via their name using the default Comparator |
| 230 | + */ |
| 231 | + @Test |
| 232 | + public void testWithCustomComparatorInverse() { |
| 233 | + final Comparator<AlphaBean> comparator = Comparator.comparing(AlphaBean::getName); |
| 234 | + |
| 235 | + final BeanComparator<AlphaBean, ?> beanComparator = new BeanComparator<>(null, comparator); |
| 236 | + final int result = beanComparator.compare(alphaBean2, alphaBean1); |
| 237 | + assertEquals(1, result, () -> "Comparator did not sort properly. Result:" + result); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void testEquals() { |
| 242 | + final Comparator<AlphaBean> comparator = Comparator.comparing(AlphaBean::getName); |
| 243 | + final BeanComparator<AlphaBean, String> nameComparator = new BeanComparator<>("name"); |
| 244 | + final BeanComparator<AlphaBean, String> nameComparator2 = new BeanComparator<>("name"); |
| 245 | + final BeanComparator<AlphaBean, String> nullPropertyComparator1 = new BeanComparator<>(null); |
| 246 | + final BeanComparator<AlphaBean, String> nullPropertyComparator2 = new BeanComparator<>(null); |
| 247 | + final BeanComparator<AlphaBean, String> nameComparatorDifferentComparator = new BeanComparator<>("name", |
| 248 | + String::compareTo); |
| 249 | + final BeanComparator<AlphaBean, Boolean> booleanComparator = new BeanComparator<>("booleanProperty"); |
| 250 | + assertAll( |
| 251 | + () -> assertEquals(nameComparator, nameComparator, |
| 252 | + "an instance should be equal to itself"), |
| 253 | + () -> assertNotEquals(nameComparator, comparator, |
| 254 | + "an instance should not be equal to a non-BeanComparator"), |
| 255 | + () -> assertNotEquals(nameComparator, nameComparatorDifferentComparator, |
| 256 | + "an instance should not be equal to a BeanComparator using different comparator"), |
| 257 | + () -> assertNotEquals(nameComparator, booleanComparator, |
| 258 | + "an instance should not be equal to a BeanComparator using different property"), |
| 259 | + () -> assertEquals(nameComparator, nameComparator2, |
| 260 | + "an instance should be equal to a BeanComparator with same comparator and property"), |
| 261 | + () -> assertNotEquals(nullPropertyComparator1, booleanComparator, |
| 262 | + "an instance with null property comparator should not be equal to a BeanComparator using a property"), |
| 263 | + () -> assertEquals(nullPropertyComparator1, nullPropertyComparator2, |
| 264 | + "an instance with null property comparator should be equal to a BeanComparator with same comparator and a null property") |
| 265 | + ); |
| 266 | + } |
171 | 267 | }
|
0 commit comments