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