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