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