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