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: package org.apache.commons.lang.mutable;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * JUnit tests.
026: *
027: * @version $Id: MutableByteTest.java 437554 2006-08-28 06:21:41Z bayard $
028: * @see MutableByte
029: */
030: public class MutableByteTest extends TestCase {
031:
032: public MutableByteTest(String testName) {
033: super (testName);
034: }
035:
036: public static void main(String[] args) {
037: TestRunner.run(suite());
038: }
039:
040: public static Test suite() {
041: return new TestSuite(MutableByteTest.class);
042: }
043:
044: // ----------------------------------------------------------------
045: public void testConstructors() {
046: assertEquals((byte) 0, new MutableByte().byteValue());
047:
048: assertEquals((byte) 1, new MutableByte((byte) 1).byteValue());
049:
050: assertEquals((byte) 2, new MutableByte(new Byte((byte) 2))
051: .byteValue());
052: assertEquals((byte) 3, new MutableByte(
053: new MutableByte((byte) 3)).byteValue());
054: try {
055: new MutableByte(null);
056: fail();
057: } catch (NullPointerException ex) {
058: }
059: }
060:
061: public void testGetSet() {
062: final MutableByte mutNum = new MutableByte((byte) 0);
063: assertEquals((byte) 0, new MutableByte().byteValue());
064: assertEquals(new Byte((byte) 0), new MutableByte().getValue());
065:
066: mutNum.setValue((byte) 1);
067: assertEquals((byte) 1, mutNum.byteValue());
068: assertEquals(new Byte((byte) 1), mutNum.getValue());
069:
070: mutNum.setValue(new Byte((byte) 2));
071: assertEquals((byte) 2, mutNum.byteValue());
072: assertEquals(new Byte((byte) 2), mutNum.getValue());
073:
074: mutNum.setValue(new MutableByte((byte) 3));
075: assertEquals((byte) 3, mutNum.byteValue());
076: assertEquals(new Byte((byte) 3), mutNum.getValue());
077: try {
078: mutNum.setValue(null);
079: fail();
080: } catch (NullPointerException ex) {
081: }
082: try {
083: mutNum.setValue("0");
084: fail();
085: } catch (ClassCastException ex) {
086: }
087: }
088:
089: public void testEquals() {
090: final MutableByte mutNumA = new MutableByte((byte) 0);
091: final MutableByte mutNumB = new MutableByte((byte) 0);
092: final MutableByte mutNumC = new MutableByte((byte) 1);
093:
094: assertEquals(true, mutNumA.equals(mutNumA));
095: assertEquals(true, mutNumA.equals(mutNumB));
096: assertEquals(true, mutNumB.equals(mutNumA));
097: assertEquals(true, mutNumB.equals(mutNumB));
098: assertEquals(false, mutNumA.equals(mutNumC));
099: assertEquals(false, mutNumB.equals(mutNumC));
100: assertEquals(true, mutNumC.equals(mutNumC));
101: assertEquals(false, mutNumA.equals(null));
102: assertEquals(false, mutNumA.equals(new Byte((byte) 0)));
103: assertEquals(false, mutNumA.equals("0"));
104: }
105:
106: public void testHashCode() {
107: final MutableByte mutNumA = new MutableByte((byte) 0);
108: final MutableByte mutNumB = new MutableByte((byte) 0);
109: final MutableByte mutNumC = new MutableByte((byte) 1);
110:
111: assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
112: assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
113: assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
114: assertEquals(true, mutNumA.hashCode() == new Byte((byte) 0)
115: .hashCode());
116: }
117:
118: public void testCompareTo() {
119: final MutableByte mutNum = new MutableByte((byte) 0);
120:
121: assertEquals((byte) 0, mutNum.compareTo(new MutableByte(
122: (byte) 0)));
123: assertEquals((byte) +1, mutNum.compareTo(new MutableByte(
124: (byte) -1)));
125: assertEquals((byte) -1, mutNum.compareTo(new MutableByte(
126: (byte) 1)));
127: try {
128: mutNum.compareTo(null);
129: fail();
130: } catch (NullPointerException ex) {
131: }
132: try {
133: mutNum.compareTo(new Byte((byte) 0));
134: fail();
135: } catch (ClassCastException ex) {
136: }
137: try {
138: mutNum.compareTo("0");
139: fail();
140: } catch (ClassCastException ex) {
141: }
142: }
143:
144: public void testPrimitiveValues() {
145: MutableByte mutNum = new MutableByte((byte) 1);
146:
147: assertEquals(1.0F, mutNum.floatValue(), 0);
148: assertEquals(1.0, mutNum.doubleValue(), 0);
149: assertEquals((byte) 1, mutNum.byteValue());
150: assertEquals((short) 1, mutNum.shortValue());
151: assertEquals(1, mutNum.intValue());
152: assertEquals(1L, mutNum.longValue());
153: }
154:
155: public void testToByte() {
156: assertEquals(new Byte((byte) 0), new MutableByte((byte) 0)
157: .toByte());
158: assertEquals(new Byte((byte) 123), new MutableByte((byte) 123)
159: .toByte());
160: }
161:
162: public void testIncrement() {
163: MutableByte mutNum = new MutableByte((byte) 1);
164: mutNum.increment();
165:
166: assertEquals(2, mutNum.intValue());
167: assertEquals(2L, mutNum.longValue());
168: }
169:
170: public void testDecrement() {
171: MutableByte mutNum = new MutableByte((byte) 1);
172: mutNum.decrement();
173:
174: assertEquals(0, mutNum.intValue());
175: assertEquals(0L, mutNum.longValue());
176: }
177:
178: public void testAddValuePrimitive() {
179: MutableByte mutNum = new MutableByte((byte) 1);
180: mutNum.add((byte) 1);
181:
182: assertEquals((byte) 2, mutNum.byteValue());
183: }
184:
185: public void testAddValueObject() {
186: MutableByte mutNum = new MutableByte((byte) 1);
187: mutNum.add(new Integer(1));
188:
189: assertEquals((byte) 2, mutNum.byteValue());
190: }
191:
192: public void testSubtractValuePrimitive() {
193: MutableByte mutNum = new MutableByte((byte) 1);
194: mutNum.subtract((byte) 1);
195:
196: assertEquals((byte) 0, mutNum.byteValue());
197: }
198:
199: public void testSubtractValueObject() {
200: MutableByte mutNum = new MutableByte((byte) 1);
201: mutNum.subtract(new Integer(1));
202:
203: assertEquals((byte) 0, mutNum.byteValue());
204: }
205:
206: public void testToString() {
207: assertEquals("0", new MutableByte((byte) 0).toString());
208: assertEquals("10", new MutableByte((byte) 10).toString());
209: assertEquals("-123", new MutableByte((byte) -123).toString());
210: }
211:
212: }
|