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  
18  package org.apache.commons.lang3.mutable;
19  
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.assertNotEquals;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import org.apache.commons.lang3.AbstractLangTest;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * JUnit tests.
31   *
32   * @since 2.2
33   * @see MutableBoolean
34   */
35  public class MutableBooleanTest extends AbstractLangTest {
36  
37      @Test
38      public void testCompareTo() {
39          final MutableBoolean mutBool = new MutableBoolean(false);
40  
41          assertEquals(0, mutBool.compareTo(new MutableBoolean(false)));
42          assertEquals(-1, mutBool.compareTo(new MutableBoolean(true)));
43          mutBool.setValue(true);
44          assertEquals(+1, mutBool.compareTo(new MutableBoolean(false)));
45          assertEquals(0, mutBool.compareTo(new MutableBoolean(true)));
46      }
47  
48      @Test
49      public void testCompareToNull() {
50          final MutableBoolean mutBool = new MutableBoolean(false);
51          assertThrows(NullPointerException.class, () -> mutBool.compareTo(null));
52      }
53  
54      @Test
55      public void testConstructorNull() {
56          assertThrows(NullPointerException.class, () -> new MutableBoolean(null));
57      }
58  
59      @Test
60      public void testConstructors() {
61          assertFalse(new MutableBoolean().booleanValue());
62  
63          assertTrue(new MutableBoolean(true).booleanValue());
64          assertFalse(new MutableBoolean(false).booleanValue());
65  
66          assertTrue(new MutableBoolean(Boolean.TRUE).booleanValue());
67          assertFalse(new MutableBoolean(Boolean.FALSE).booleanValue());
68  
69      }
70  
71      @Test
72      public void testEquals() {
73          final MutableBoolean mutBoolA = new MutableBoolean(false);
74          final MutableBoolean mutBoolB = new MutableBoolean(false);
75          final MutableBoolean mutBoolC = new MutableBoolean(true);
76  
77          assertEquals(mutBoolA, mutBoolA);
78          assertEquals(mutBoolA, mutBoolB);
79          assertEquals(mutBoolB, mutBoolA);
80          assertEquals(mutBoolB, mutBoolB);
81          assertNotEquals(mutBoolA, mutBoolC);
82          assertNotEquals(mutBoolB, mutBoolC);
83          assertEquals(mutBoolC, mutBoolC);
84          assertNotEquals(null, mutBoolA);
85          assertNotEquals(mutBoolA, Boolean.FALSE);
86          assertNotEquals("false", mutBoolA);
87      }
88  
89      @Test
90      public void testGetSet() {
91          assertFalse(new MutableBoolean().booleanValue());
92          assertEquals(Boolean.FALSE, new MutableBoolean().getValue());
93  
94          final MutableBoolean mutBool = new MutableBoolean(false);
95          assertEquals(Boolean.FALSE, mutBool.toBoolean());
96          assertFalse(mutBool.booleanValue());
97          assertTrue(mutBool.isFalse());
98          assertFalse(mutBool.isTrue());
99  
100         mutBool.setValue(Boolean.TRUE);
101         assertEquals(Boolean.TRUE, mutBool.toBoolean());
102         assertTrue(mutBool.booleanValue());
103         assertFalse(mutBool.isFalse());
104         assertTrue(mutBool.isTrue());
105 
106         mutBool.setValue(false);
107         assertFalse(mutBool.booleanValue());
108 
109         mutBool.setValue(true);
110         assertTrue(mutBool.booleanValue());
111 
112         mutBool.setFalse();
113         assertFalse(mutBool.booleanValue());
114 
115         mutBool.setTrue();
116         assertTrue(mutBool.booleanValue());
117 
118     }
119 
120     @Test
121     public void testHashCode() {
122         final MutableBoolean mutBoolA = new MutableBoolean(false);
123         final MutableBoolean mutBoolB = new MutableBoolean(false);
124         final MutableBoolean mutBoolC = new MutableBoolean(true);
125 
126         assertEquals(mutBoolA.hashCode(), mutBoolA.hashCode());
127         assertEquals(mutBoolA.hashCode(), mutBoolB.hashCode());
128         assertNotEquals(mutBoolA.hashCode(), mutBoolC.hashCode());
129         assertEquals(mutBoolA.hashCode(), Boolean.FALSE.hashCode());
130         assertEquals(mutBoolC.hashCode(), Boolean.TRUE.hashCode());
131     }
132 
133     @Test
134     public void testSetNull() {
135         final MutableBoolean mutBool = new MutableBoolean(false);
136         assertThrows(NullPointerException.class, () -> mutBool.setValue(null));
137     }
138 
139     @Test
140     public void testToString() {
141         assertEquals(Boolean.FALSE.toString(), new MutableBoolean(false).toString());
142         assertEquals(Boolean.TRUE.toString(), new MutableBoolean(true).toString());
143     }
144 
145 }