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  package org.apache.commons.lang3.compare;
18  
19  import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.math.BigDecimal;
25  import java.time.Instant;
26  
27  import org.apache.commons.lang3.AbstractLangTest;
28  import org.junit.jupiter.api.Nested;
29  import org.junit.jupiter.api.Test;
30  
31  class ComparableUtilsTest extends AbstractLangTest {
32  
33      @Nested
34      final class A_is_1 {
35  
36          /** B is 0 (B < A) */
37          @Nested
38          final class B_is_0 {
39  
40              /** C is 0 ([B=C] < A) */
41              @Nested
42              final class C_is_0 {
43  
44                  BigDecimal c = BigDecimal.ZERO;
45  
46                  @Test
47                  void between_returns_false() {
48                      assertFalse(ComparableUtils.is(a).between(b, c));
49                  }
50  
51                  @Test
52                  void betweenExclusive_returns_false() {
53                      assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
54                  }
55  
56                  @Test
57                  void static_between_returns_false() {
58                      assertFalse(ComparableUtils.between(b, c).test(a));
59                  }
60  
61                  @Test
62                  void static_betweenExclusive_returns_false() {
63                      assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
64                  }
65  
66              }
67  
68              /** C is 1 (B < A = C) */
69              @Nested
70              final class C_is_1 {
71  
72                  BigDecimal c = BigDecimal.ONE;
73  
74                  @Test
75                  void between_returns_true() {
76                      assertTrue(ComparableUtils.is(a).between(b, c));
77                  }
78  
79                  @Test
80                  void betweenExclusive_returns_false() {
81                      assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
82                  }
83  
84                  @Test
85                  void static_between_returns_true() {
86                      assertTrue(ComparableUtils.between(b, c).test(a));
87                  }
88  
89                  @Test
90                  void static_betweenExclusive_returns_false() {
91                      assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
92                  }
93              }
94  
95              /** C is 10 (B < A < C) */
96              @Nested
97              final class C_is_10 {
98  
99                  BigDecimal c = BigDecimal.TEN;
100 
101                 @Test
102                 void between_returns_true() {
103                     assertTrue(ComparableUtils.is(a).between(b, c));
104                 }
105 
106                 @Test
107                 void betweenExclusive_returns_true() {
108                     assertTrue(ComparableUtils.is(a).betweenExclusive(b, c));
109                 }
110 
111                 @Test
112                 void static_between_returns_true() {
113                     assertTrue(ComparableUtils.between(b, c).test(a));
114                 }
115 
116                 @Test
117                 void static_betweenExclusive_returns_true() {
118                     assertTrue(ComparableUtils.betweenExclusive(b, c).test(a));
119                 }
120             }
121 
122             BigDecimal b = BigDecimal.ZERO;
123 
124             @Test
125             void equalTo_returns_false() {
126                 assertFalse(ComparableUtils.is(a).equalTo(b));
127             }
128 
129             @Test
130             void greaterThan_returns_true() {
131                 assertTrue(ComparableUtils.is(a).greaterThan(b));
132             }
133 
134             @Test
135             void greaterThanOrEqualTo_returns_true() {
136                 assertTrue(ComparableUtils.is(a).greaterThanOrEqualTo(b));
137             }
138 
139             @Test
140             void lessThan_returns_false() {
141                 assertFalse(ComparableUtils.is(a).lessThan(b));
142             }
143 
144             @Test
145             void lessThanOrEqualTo_returns_false() {
146                 assertFalse(ComparableUtils.is(a).lessThanOrEqualTo(b));
147             }
148 
149             @Test
150             void static_ge_returns_true() {
151                 assertTrue(ComparableUtils.ge(b).test(a));
152             }
153 
154             @Test
155             void static_gt_returns_true() {
156                 assertTrue(ComparableUtils.gt(b).test(a));
157             }
158 
159             @Test
160             void static_le_returns_false() {
161                 assertFalse(ComparableUtils.le(b).test(a));
162             }
163 
164             @Test
165             void static_lt_returns_false() {
166                 assertFalse(ComparableUtils.lt(b).test(a));
167             }
168         }
169 
170         /** B is 1 (B = A) */
171         @Nested
172         final class B_is_1 {
173 
174             /** C is 0 (B = A > C) */
175             @Nested
176             final class C_is_0 {
177 
178                 BigDecimal c = BigDecimal.ZERO;
179 
180                 @Test
181                 void between_returns_true() {
182                     assertTrue(ComparableUtils.is(a).between(b, c));
183                 }
184 
185                 @Test
186                 void betweenExclusive_returns_false() {
187                     assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
188                 }
189 
190                 @Test
191                 void static_between_returns_true() {
192                     assertTrue(ComparableUtils.between(b, c).test(a));
193                 }
194 
195                 @Test
196                 void static_betweenExclusive_returns_false() {
197                     assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
198                 }
199             }
200 
201             /** C is 1 (B = A = C) */
202             @Nested
203             final class C_is_1 {
204 
205                 BigDecimal c = BigDecimal.ONE;
206 
207                 @Test
208                 void between_returns_true() {
209                     assertTrue(ComparableUtils.is(a).between(b, c));
210                 }
211 
212                 @Test
213                 void betweenExclusive_returns_false() {
214                     assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
215                 }
216 
217                 @Test
218                 void static_between_returns_true() {
219                     assertTrue(ComparableUtils.between(b, c).test(a));
220                 }
221 
222                 @Test
223                 void static_betweenExclusive_returns_false() {
224                     assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
225                 }
226             }
227 
228             /** C is 10 (B = A < C) */
229             @Nested
230             final class C_is_10 {
231 
232                 BigDecimal c = BigDecimal.TEN;
233 
234                 @Test
235                 void between_returns_true() {
236                     assertTrue(ComparableUtils.is(a).between(b, c));
237                 }
238 
239                 @Test
240                 void betweenExclusive_returns_false() {
241                     assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
242                 }
243 
244                 @Test
245                 void static_between_returns_true() {
246                     assertTrue(ComparableUtils.between(b, c).test(a));
247                 }
248 
249                 @Test
250                 void static_betweenExclusive_returns_false() {
251                     assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
252                 }
253             }
254 
255             BigDecimal b = BigDecimal.ONE;
256 
257             @Test
258             void equalTo_returns_true() {
259                 assertTrue(ComparableUtils.is(a).equalTo(b));
260             }
261 
262             @Test
263             void greaterThan_returns_false() {
264                 assertFalse(ComparableUtils.is(a).greaterThan(b));
265             }
266 
267             @Test
268             void greaterThanOrEqualTo_returns_true() {
269                 assertTrue(ComparableUtils.is(a).greaterThanOrEqualTo(b));
270             }
271 
272             @Test
273             void lessThan_returns_false() {
274                 assertFalse(ComparableUtils.is(a).lessThan(b));
275             }
276 
277             @Test
278             void lessThanOrEqualTo_returns_true() {
279                 assertTrue(ComparableUtils.is(a).lessThanOrEqualTo(b));
280             }
281 
282             @Test
283             void static_ge_returns_true() {
284                 assertTrue(ComparableUtils.ge(b).test(a));
285             }
286 
287             @Test
288             void static_gt_returns_false() {
289                 assertFalse(ComparableUtils.gt(b).test(a));
290             }
291 
292             @Test
293             void static_le_returns_true() {
294                 assertTrue(ComparableUtils.le(b).test(a));
295             }
296 
297             @Test
298             void static_lt_returns_false() {
299                 assertFalse(ComparableUtils.lt(b).test(a));
300             }
301         }
302 
303         /** B is 10 (B > A) */
304         @Nested
305         final class B_is_10 {
306 
307             /** C is 0 (B > A > C) */
308             @Nested
309             final class C_is_0 {
310 
311                 BigDecimal c = BigDecimal.ZERO;
312 
313                 @Test
314                 void between_returns_true() {
315                     assertTrue(ComparableUtils.is(a).between(b, c));
316                 }
317 
318                 @Test
319                 void betweenExclusive_returns_true() {
320                     assertTrue(ComparableUtils.is(a).betweenExclusive(b, c));
321                 }
322 
323                 @Test
324                 void static_between_returns_true() {
325                     assertTrue(ComparableUtils.between(b, c).test(a));
326                 }
327 
328                 @Test
329                 void static_betweenExclusive_returns_true() {
330                     assertTrue(ComparableUtils.betweenExclusive(b, c).test(a));
331                 }
332             }
333 
334             /** C is 1 (B > A = C) */
335             @Nested
336             final class C_is_1 {
337 
338                 BigDecimal c = BigDecimal.ONE;
339 
340                 @Test
341                 void between_returns_true() {
342                     assertTrue(ComparableUtils.is(a).between(b, c));
343                 }
344 
345                 @Test
346                 void betweenExclusive_returns_false() {
347                     assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
348                 }
349 
350                 @Test
351                 void static_between_returns_true() {
352                     assertTrue(ComparableUtils.between(b, c).test(a));
353                 }
354 
355                 @Test
356                 void static_betweenExclusive_returns_false() {
357                     assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
358                 }
359             }
360 
361             /** C is 10 ([B,C] > A) */
362             @Nested
363             final class C_is_10 {
364 
365                 BigDecimal c = BigDecimal.TEN;
366 
367                 @Test
368                 void between_returns_false() {
369                     assertFalse(ComparableUtils.is(a).between(b, c));
370                 }
371 
372                 @Test
373                 void betweenExclusive_returns_false() {
374                     assertFalse(ComparableUtils.is(a).betweenExclusive(b, c));
375                 }
376 
377                 @Test
378                 void static_between_returns_false() {
379                     assertFalse(ComparableUtils.between(b, c).test(a));
380                 }
381 
382                 @Test
383                 void static_betweenExclusive_returns_false() {
384                     assertFalse(ComparableUtils.betweenExclusive(b, c).test(a));
385                 }
386             }
387 
388             BigDecimal b = BigDecimal.TEN;
389 
390             @Test
391             void equalTo_returns_false() {
392                 assertFalse(ComparableUtils.is(a).equalTo(b));
393             }
394 
395             @Test
396             void greaterThan_returns_false() {
397                 assertFalse(ComparableUtils.is(a).greaterThan(b));
398             }
399 
400             @Test
401             void greaterThanOrEqualTo_returns_false() {
402                 assertFalse(ComparableUtils.is(a).greaterThanOrEqualTo(b));
403             }
404 
405             @Test
406             void lessThan_returns_true() {
407                 assertTrue(ComparableUtils.is(a).lessThan(b));
408             }
409 
410             @Test
411             void lessThanOrEqualTo_returns_true() {
412                 assertTrue(ComparableUtils.is(a).lessThanOrEqualTo(b));
413             }
414 
415             @Test
416             void static_ge_returns_false() {
417                 assertFalse(ComparableUtils.ge(b).test(a));
418             }
419 
420             @Test
421             void static_gt_returns_false() {
422                 assertFalse(ComparableUtils.gt(b).test(a));
423             }
424 
425             @Test
426             void static_le_returns_true() {
427                 assertTrue(ComparableUtils.le(b).test(a));
428             }
429 
430             @Test
431             void static_lt_returns_true() {
432                 assertTrue(ComparableUtils.lt(b).test(a));
433             }
434         }
435 
436         BigDecimal a = BigDecimal.ONE;
437     }
438 
439     private static class MyComparable implements Comparable<MyComparable> {
440 
441         private final int i;
442 
443         MyComparable(final int i) {
444             this.i = i;
445         }
446 
447         @Override
448         public int compareTo(final MyComparable o) {
449             return Integer.compare(i, o != null ? o.i : 0);
450         }
451 
452     }
453 
454     @Test
455     void testIsEqualTo() {
456         assertNullPointerException(() -> ComparableUtils.is("a").equalTo(null));
457         assertFalse(ComparableUtils.is(new MyComparable(1)).equalTo(null));
458         assertFalse(ComparableUtils.is((String) null).equalTo("a"));
459         assertTrue(ComparableUtils.is("a").equalTo("a"));
460     }
461 
462     @Test
463     void testIsGreaterThan() {
464         assertNullPointerException(() -> ComparableUtils.is("a").greaterThan(null));
465         assertTrue(ComparableUtils.is(new MyComparable(1)).greaterThan(null));
466         assertFalse(ComparableUtils.is((String) null).greaterThan("a"));
467         assertFalse(ComparableUtils.is("a").greaterThan("a"));
468     }
469 
470     @Test
471     void testIsGreaterThanOrEqualTo() {
472         assertNullPointerException(() -> ComparableUtils.is("a").greaterThanOrEqualTo(null));
473         assertTrue(ComparableUtils.is(new MyComparable(1)).greaterThanOrEqualTo(null));
474         assertFalse(ComparableUtils.is((String) null).greaterThanOrEqualTo("a"));
475         assertTrue(ComparableUtils.is("a").greaterThanOrEqualTo("a"));
476     }
477 
478     @Test
479     void testIsLessThan() {
480         assertNullPointerException(() -> ComparableUtils.is("a").lessThan(null));
481         assertFalse(ComparableUtils.is(new MyComparable(1)).lessThan(null));
482         assertFalse(ComparableUtils.is((String) null).lessThan("a"));
483         assertFalse(ComparableUtils.is("a").lessThan("a"));
484     }
485 
486     @Test
487     void testIsLessThanOrEqualTo() {
488         assertNullPointerException(() -> ComparableUtils.is("a").greaterThanOrEqualTo(null));
489         assertTrue(ComparableUtils.is(new MyComparable(1)).greaterThanOrEqualTo(null));
490         assertFalse(ComparableUtils.is((String) null).lessThanOrEqualTo("a"));
491         assertTrue(ComparableUtils.is("a").lessThanOrEqualTo("a"));
492     }
493 
494     @Test
495     void testMax() {
496         assertEquals(Instant.MAX, ComparableUtils.max(Instant.MAX, Instant.MAX));
497         assertEquals(Instant.MIN, ComparableUtils.max(Instant.MIN, Instant.MIN));
498         assertEquals(Instant.MAX, ComparableUtils.max(Instant.MIN, Instant.MAX));
499         assertEquals(Instant.MAX, ComparableUtils.max(Instant.MAX, Instant.MIN));
500         //
501         assertEquals(Integer.MIN_VALUE, ComparableUtils.max(Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MIN_VALUE)));
502         assertEquals(Integer.MAX_VALUE, ComparableUtils.max(Integer.valueOf(Integer.MAX_VALUE), Integer.valueOf(Integer.MAX_VALUE)));
503         assertEquals(Integer.MAX_VALUE, ComparableUtils.max(Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MAX_VALUE)));
504         assertEquals(Integer.MAX_VALUE, ComparableUtils.max(Integer.valueOf(Integer.MAX_VALUE), Integer.valueOf(Integer.MIN_VALUE)));
505         //
506         assertEquals(Instant.MAX, ComparableUtils.max(null, Instant.MAX));
507         assertEquals(Instant.MAX, ComparableUtils.max(Instant.MAX, null));
508     }
509 
510     @Test
511     void testMin() {
512         assertEquals(Instant.MAX, ComparableUtils.min(Instant.MAX, Instant.MAX));
513         assertEquals(Instant.MIN, ComparableUtils.min(Instant.MIN, Instant.MIN));
514         assertEquals(Instant.MIN, ComparableUtils.min(Instant.MIN, Instant.MAX));
515         assertEquals(Instant.MIN, ComparableUtils.min(Instant.MAX, Instant.MIN));
516         //
517         assertEquals(Integer.MIN_VALUE, ComparableUtils.min(Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MIN_VALUE)));
518         assertEquals(Integer.MAX_VALUE, ComparableUtils.min(Integer.valueOf(Integer.MAX_VALUE), Integer.valueOf(Integer.MAX_VALUE)));
519         assertEquals(Integer.MIN_VALUE, ComparableUtils.min(Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MAX_VALUE)));
520         assertEquals(Integer.MIN_VALUE, ComparableUtils.min(Integer.valueOf(Integer.MAX_VALUE), Integer.valueOf(Integer.MIN_VALUE)));
521         //
522         assertEquals(Instant.MAX, ComparableUtils.min(null, Instant.MAX));
523         assertEquals(Instant.MAX, ComparableUtils.min(Instant.MAX, null));
524     }
525 }