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