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   * @see MutableBoolean
33   */
34  public class MutableBooleanTest extends AbstractLangTest {
35  
36      @Test
37      public 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      public void testCompareToNull() {
49          final MutableBoolean mutBool = new MutableBoolean(false);
50          assertThrows(NullPointerException.class, () -> mutBool.compareTo(null));
51      }
52  
53      @Test
54      public void testConstructorNull() {
55          assertThrows(NullPointerException.class, () -> new MutableBoolean(null));
56      }
57  
58      @Test
59      public 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      public 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      public void testGetSet() {
90          assertFalse(new MutableBoolean().booleanValue());
91          assertEquals(Boolean.FALSE, new MutableBoolean().getValue());
92  
93          final MutableBoolean mutBool = new MutableBoolean(false);
94          assertEquals(Boolean.FALSE, mutBool.toBoolean());
95          assertFalse(mutBool.booleanValue());
96          assertTrue(mutBool.isFalse());
97          assertFalse(mutBool.isTrue());
98  
99          mutBool.setValue(Boolean.TRUE);
100         assertEquals(Boolean.TRUE, mutBool.toBoolean());
101         assertTrue(mutBool.booleanValue());
102         assertFalse(mutBool.isFalse());
103         assertTrue(mutBool.isTrue());
104 
105         mutBool.setValue(false);
106         assertFalse(mutBool.booleanValue());
107 
108         mutBool.setValue(true);
109         assertTrue(mutBool.booleanValue());
110 
111         mutBool.setFalse();
112         assertFalse(mutBool.booleanValue());
113 
114         mutBool.setTrue();
115         assertTrue(mutBool.booleanValue());
116 
117     }
118 
119     @Test
120     public void testHashCode() {
121         final MutableBoolean mutBoolA = new MutableBoolean(false);
122         final MutableBoolean mutBoolB = new MutableBoolean(false);
123         final MutableBoolean mutBoolC = new MutableBoolean(true);
124 
125         assertEquals(mutBoolA.hashCode(), mutBoolA.hashCode());
126         assertEquals(mutBoolA.hashCode(), mutBoolB.hashCode());
127         assertNotEquals(mutBoolA.hashCode(), mutBoolC.hashCode());
128         assertEquals(mutBoolA.hashCode(), Boolean.FALSE.hashCode());
129         assertEquals(mutBoolC.hashCode(), Boolean.TRUE.hashCode());
130     }
131 
132     @Test
133     public void testSetNull() {
134         final MutableBoolean mutBool = new MutableBoolean(false);
135         assertThrows(NullPointerException.class, () -> mutBool.setValue(null));
136     }
137 
138     @Test
139     public void testToString() {
140         assertEquals(Boolean.FALSE.toString(), new MutableBoolean(false).toString());
141         assertEquals(Boolean.TRUE.toString(), new MutableBoolean(true).toString());
142     }
143 
144 }