001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.lang.mutable;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024:
025: /**
026: * JUnit tests.
027: *
028: * @since 2.2
029: * @see MutableBoolean
030: * @author Apache Software Foundation
031: * @version $Id: MutableBooleanTest.java 437554 2006-08-28 06:21:41Z bayard $
032: */
033: public class MutableBooleanTest extends TestCase {
034:
035: public static void main(String[] args) {
036: TestRunner.run(suite());
037: }
038:
039: public static Test suite() {
040: return new TestSuite(MutableBooleanTest.class);
041: }
042:
043: public MutableBooleanTest(String testName) {
044: super (testName);
045: }
046:
047: public void testCompareTo() {
048: final MutableBoolean mutBool = new MutableBoolean(false);
049:
050: assertEquals(0, mutBool.compareTo(new MutableBoolean(false)));
051: assertEquals(-1, mutBool.compareTo(new MutableBoolean(true)));
052: mutBool.setValue(true);
053: assertEquals(+1, mutBool.compareTo(new MutableBoolean(false)));
054: assertEquals(0, mutBool.compareTo(new MutableBoolean(true)));
055:
056: try {
057: mutBool.compareTo(null);
058: fail();
059: } catch (NullPointerException ex) {
060: }
061: try {
062: mutBool.compareTo(Boolean.FALSE);
063: fail();
064: } catch (ClassCastException ex) {
065: }
066: try {
067: mutBool.compareTo("false");
068: fail();
069: } catch (ClassCastException ex) {
070: }
071: }
072:
073: // ----------------------------------------------------------------
074: public void testConstructors() {
075: assertEquals(false, new MutableBoolean().booleanValue());
076:
077: assertEquals(true, new MutableBoolean(true).booleanValue());
078: assertEquals(false, new MutableBoolean(false).booleanValue());
079:
080: assertEquals(true, new MutableBoolean(Boolean.TRUE)
081: .booleanValue());
082: assertEquals(false, new MutableBoolean(Boolean.FALSE)
083: .booleanValue());
084:
085: try {
086: new MutableBoolean(null);
087: fail();
088: } catch (NullPointerException ex) {
089: }
090: }
091:
092: public void testEquals() {
093: final MutableBoolean mutBoolA = new MutableBoolean(false);
094: final MutableBoolean mutBoolB = new MutableBoolean(false);
095: final MutableBoolean mutBoolC = new MutableBoolean(true);
096:
097: assertEquals(true, mutBoolA.equals(mutBoolA));
098: assertEquals(true, mutBoolA.equals(mutBoolB));
099: assertEquals(true, mutBoolB.equals(mutBoolA));
100: assertEquals(true, mutBoolB.equals(mutBoolB));
101: assertEquals(false, mutBoolA.equals(mutBoolC));
102: assertEquals(false, mutBoolB.equals(mutBoolC));
103: assertEquals(true, mutBoolC.equals(mutBoolC));
104: assertEquals(false, mutBoolA.equals(null));
105: assertEquals(false, mutBoolA.equals(Boolean.FALSE));
106: assertEquals(false, mutBoolA.equals("false"));
107: }
108:
109: public void testGetSet() {
110: final MutableBoolean mutBool = new MutableBoolean(false);
111: assertEquals(false, new MutableBoolean().booleanValue());
112:
113: mutBool.setValue(Boolean.TRUE);
114: assertEquals(true, mutBool.booleanValue());
115:
116: mutBool.setValue(false);
117: assertEquals(false, mutBool.booleanValue());
118:
119: mutBool.setValue(true);
120: assertEquals(true, mutBool.booleanValue());
121:
122: try {
123: mutBool.setValue(null);
124: fail();
125: } catch (NullPointerException ex) {
126: }
127: try {
128: mutBool.setValue("false");
129: fail();
130: } catch (ClassCastException ex) {
131: }
132: }
133:
134: public void testHashCode() {
135: final MutableBoolean mutBoolA = new MutableBoolean(false);
136: final MutableBoolean mutBoolB = new MutableBoolean(false);
137: final MutableBoolean mutBoolC = new MutableBoolean(true);
138:
139: assertEquals(true, mutBoolA.hashCode() == mutBoolA.hashCode());
140: assertEquals(true, mutBoolA.hashCode() == mutBoolB.hashCode());
141: assertEquals(false, mutBoolA.hashCode() == mutBoolC.hashCode());
142: assertEquals(true, mutBoolA.hashCode() == Boolean.FALSE
143: .hashCode());
144: assertEquals(true, mutBoolC.hashCode() == Boolean.TRUE
145: .hashCode());
146: }
147:
148: public void testToString() {
149: assertEquals(Boolean.FALSE.toString(),
150: new MutableBoolean(false).toString());
151: assertEquals(Boolean.TRUE.toString(), new MutableBoolean(true)
152: .toString());
153: }
154:
155: }
|