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