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  
18  package org.apache.commons.lang3.mutable;
19  
20  import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotEquals;
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   * @see MutableBoolean
33   */
34  class MutableBooleanTest extends AbstractLangTest {
35  
36      @Test
37      void testCompareTo() {
38          final MutableBoolean mutBool = new MutableBoolean(false);
39  
40          assertEquals(0, mutBool.compareTo(new MutableBoolean(false)));
41          assertEquals(-1, mutBool.compareTo(new MutableBoolean(true)));
42          mutBool.setValue(true);
43          assertEquals(+1, mutBool.compareTo(new MutableBoolean(false)));
44          assertEquals(0, mutBool.compareTo(new MutableBoolean(true)));
45      }
46  
47      @Test
48      void testCompareToNull() {
49          final MutableBoolean mutBool = new MutableBoolean(false);
50          assertNullPointerException(() -> mutBool.compareTo(null));
51      }
52  
53      @Test
54      void testConstructorNull() {
55          assertNullPointerException(() -> new MutableBoolean(null));
56      }
57  
58      @Test
59      void testConstructors() {
60          assertFalse(new MutableBoolean().booleanValue());
61  
62          assertTrue(new MutableBoolean(true).booleanValue());
63          assertFalse(new MutableBoolean(false).booleanValue());
64  
65          assertTrue(new MutableBoolean(Boolean.TRUE).booleanValue());
66          assertFalse(new MutableBoolean(Boolean.FALSE).booleanValue());
67  
68      }
69  
70      @Test
71      void testEquals() {
72          final MutableBoolean mutBoolA = new MutableBoolean(false);
73          final MutableBoolean mutBoolB = new MutableBoolean(false);
74          final MutableBoolean mutBoolC = new MutableBoolean(true);
75  
76          assertEquals(mutBoolA, mutBoolA);
77          assertEquals(mutBoolA, mutBoolB);
78          assertEquals(mutBoolB, mutBoolA);
79          assertEquals(mutBoolB, mutBoolB);
80          assertNotEquals(mutBoolA, mutBoolC);
81          assertNotEquals(mutBoolB, mutBoolC);
82          assertEquals(mutBoolC, mutBoolC);
83          assertNotEquals(null, mutBoolA);
84          assertNotEquals(mutBoolA, Boolean.FALSE);
85          assertNotEquals("false", mutBoolA);
86      }
87  
88      @Test
89      void testGetSet() {
90          assertFalse(new MutableBoolean().booleanValue());
91          assertEquals(Boolean.FALSE, new MutableBoolean().get());
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     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     void testSetNull() {
135         final MutableBoolean mutBool = new MutableBoolean(false);
136         assertNullPointerException(() -> mutBool.setValue(null));
137     }
138 
139     @Test
140     void testToString() {
141         assertEquals(Boolean.FALSE.toString(), new MutableBoolean(false).toString());
142         assertEquals(Boolean.TRUE.toString(), new MutableBoolean(true).toString());
143     }
144 
145 }