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.collections4.comparators;
18  
19  import static org.junit.jupiter.api.Assertions.assertAll;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Comparator;
30  import java.util.List;
31  
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Tests for {@link BooleanComparator}.
36   */
37  @SuppressWarnings("boxing")
38  public class BooleanComparatorTest extends AbstractComparatorTest<Boolean> {
39  
40      public BooleanComparatorTest() {
41          super(BooleanComparatorTest.class.getSimpleName());
42      }
43  
44      protected void allTests(final boolean trueFirst, final BooleanComparator comp) {
45          orderIndependentTests(comp);
46          if (trueFirst) {
47              trueFirstTests(comp);
48          } else {
49              falseFirstTests(comp);
50          }
51      }
52  
53      protected void falseFirstTests(final BooleanComparator comp) {
54          assertNotNull(comp);
55          assertEquals(0, comp.compare(true, true));
56          assertEquals(0, comp.compare(false, false));
57          assertTrue(comp.compare(false, true) < 0);
58          assertTrue(comp.compare(true, false) > 0);
59      }
60  
61      @Override
62      public List<Boolean> getComparableObjectsOrdered() {
63          return new ArrayList<>(Arrays.asList(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE,
64                  true));
65      }
66  
67  //    public void testCreate() throws Exception {
68  //        writeExternalFormToDisk((java.io.Serializable) makeObject(), "src/test/resources/data/test/BooleanComparator.version4.obj");
69  //    }
70  
71      @Override
72      public String getCompatibilityVersion() {
73          return "4";
74      }
75  
76      @Override
77      public Comparator<Boolean> makeObject() {
78          return new BooleanComparator();
79      }
80  
81      protected void nullArgumentTests(final BooleanComparator comp) {
82          assertNotNull(comp);
83          assertAll(
84                  () -> assertThrows(NullPointerException.class, () -> comp.compare(null, null), "Expected NullPointerException"),
85                  () -> assertThrows(NullPointerException.class, () -> comp.compare(Boolean.TRUE, null), "Expected NullPointerException"),
86                  () -> assertThrows(NullPointerException.class, () -> comp.compare(Boolean.FALSE, null), "Expected NullPointerException"),
87                  () -> assertThrows(NullPointerException.class, () -> comp.compare(null, Boolean.TRUE), "Expected NullPointerException"),
88                  () -> assertThrows(NullPointerException.class, () -> comp.compare(null, Boolean.FALSE), "Expected NullPointerException")
89          );
90      }
91  
92      protected void orderIndependentTests(final BooleanComparator comp) {
93          nullArgumentTests(comp);
94      }
95  
96      @Test
97      public void testConstructors() {
98          allTests(false, new BooleanComparator());
99          allTests(false, new BooleanComparator(false));
100         allTests(true, new BooleanComparator(true));
101     }
102 
103     @Test
104     public void testEqualsCompatibleInstance() {
105         assertEquals(new BooleanComparator(), new BooleanComparator(false));
106         assertEquals(new BooleanComparator(false), new BooleanComparator(false));
107         assertEquals(new BooleanComparator(false), BooleanComparator.getFalseFirstComparator());
108         assertSame(BooleanComparator.getFalseFirstComparator(), BooleanComparator.booleanComparator(false));
109 
110         assertEquals(new BooleanComparator(true), new BooleanComparator(true));
111         assertEquals(new BooleanComparator(true), BooleanComparator.getTrueFirstComparator());
112         assertSame(BooleanComparator.getTrueFirstComparator(), BooleanComparator.booleanComparator(true));
113 
114         assertNotEquals(new BooleanComparator(), new BooleanComparator(true));
115         assertNotEquals(new BooleanComparator(true), new BooleanComparator(false));
116     }
117 
118     @Test
119     public void testStaticFactoryMethods() {
120         allTests(false, BooleanComparator.getFalseFirstComparator());
121         allTests(false, BooleanComparator.booleanComparator(false));
122         allTests(true, BooleanComparator.getTrueFirstComparator());
123         allTests(true, BooleanComparator.booleanComparator(true));
124     }
125 
126     protected void trueFirstTests(final BooleanComparator comp) {
127         assertNotNull(comp);
128         assertEquals(0, comp.compare(true, true));
129         assertEquals(0, comp.compare(false, false));
130         assertTrue(comp.compare(false, true) > 0);
131         assertTrue(comp.compare(true, false) < 0);
132     }
133 
134 }