KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > mutable > MutableIntTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.mutable;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24  * JUnit tests.
25  *
26  * @version $Id: MutableIntTest.java 161244 2005-04-14 06:16:36Z ggregory $
27  * @see MutableInt
28  */

29 public class MutableIntTest extends TestCase {
30
31     public MutableIntTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     public static void main(String JavaDoc[] args) {
36         TestRunner.run(suite());
37     }
38
39     public static Test suite() {
40         return new TestSuite(MutableIntTest.class);
41     }
42
43     // ----------------------------------------------------------------
44
public void testConstructors() {
45         assertEquals(0, new MutableInt().intValue());
46         
47         assertEquals(1, new MutableInt(1).intValue());
48         
49         assertEquals(2, new MutableInt(new Integer JavaDoc(2)).intValue());
50         assertEquals(3, new MutableInt(new MutableLong(3)).intValue());
51         try {
52             new MutableInt(null);
53             fail();
54         } catch (NullPointerException JavaDoc ex) {}
55     }
56
57     public void testGetSet() {
58         final MutableInt mutNum = new MutableInt(0);
59         assertEquals(0, new MutableInt().intValue());
60         assertEquals(new Integer JavaDoc(0), new MutableInt().getValue());
61         
62         mutNum.setValue(1);
63         assertEquals(1, mutNum.intValue());
64         assertEquals(new Integer JavaDoc(1), mutNum.getValue());
65         
66         mutNum.setValue(new Integer JavaDoc(2));
67         assertEquals(2, mutNum.intValue());
68         assertEquals(new Integer JavaDoc(2), mutNum.getValue());
69         
70         mutNum.setValue(new MutableLong(3));
71         assertEquals(3, mutNum.intValue());
72         assertEquals(new Integer JavaDoc(3), mutNum.getValue());
73         try {
74             mutNum.setValue(null);
75             fail();
76         } catch (NullPointerException JavaDoc ex) {}
77         try {
78             mutNum.setValue("0");
79             fail();
80         } catch (ClassCastException JavaDoc ex) {}
81     }
82
83     public void testEquals() {
84         final MutableInt mutNumA = new MutableInt(0);
85         final MutableInt mutNumB = new MutableInt(0);
86         final MutableInt mutNumC = new MutableInt(1);
87
88         assertEquals(true, mutNumA.equals(mutNumA));
89         assertEquals(true, mutNumA.equals(mutNumB));
90         assertEquals(true, mutNumB.equals(mutNumA));
91         assertEquals(true, mutNumB.equals(mutNumB));
92         assertEquals(false, mutNumA.equals(mutNumC));
93         assertEquals(false, mutNumB.equals(mutNumC));
94         assertEquals(true, mutNumC.equals(mutNumC));
95         assertEquals(false, mutNumA.equals(null));
96         assertEquals(false, mutNumA.equals(new Integer JavaDoc(0)));
97         assertEquals(false, mutNumA.equals("0"));
98     }
99
100     public void testHashCode() {
101         final MutableInt mutNumA = new MutableInt(0);
102         final MutableInt mutNumB = new MutableInt(0);
103         final MutableInt mutNumC = new MutableInt(1);
104
105         assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
106         assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
107         assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
108         assertEquals(true, mutNumA.hashCode() == new Integer JavaDoc(0).hashCode());
109     }
110
111     public void testCompareTo() {
112         final MutableInt mutNum = new MutableInt(0);
113
114         assertEquals(0, mutNum.compareTo(new MutableInt(0)));
115         assertEquals(+1, mutNum.compareTo(new MutableInt(-1)));
116         assertEquals(-1, mutNum.compareTo(new MutableInt(1)));
117         try {
118             mutNum.compareTo(null);
119             fail();
120         } catch (NullPointerException JavaDoc ex) {}
121         try {
122             mutNum.compareTo(new Integer JavaDoc(0));
123             fail();
124         } catch (ClassCastException JavaDoc ex) {}
125         try {
126             mutNum.compareTo("0");
127             fail();
128         } catch (ClassCastException JavaDoc ex) {}
129     }
130
131     public void testPrimitiveValues() {
132         MutableInt mutNum = new MutableInt(1);
133         
134         assertEquals( (byte) 1, mutNum.byteValue() );
135         assertEquals( (short) 1, mutNum.shortValue() );
136         assertEquals( 1.0F, mutNum.floatValue(), 0 );
137         assertEquals( 1.0, mutNum.doubleValue(), 0 );
138         assertEquals( 1L, mutNum.longValue() );
139     }
140
141     public void testToString() {
142         assertEquals("0", new MutableInt(0).toString());
143         assertEquals("10", new MutableInt(10).toString());
144         assertEquals("-123", new MutableInt(-123).toString());
145     }
146
147 }
148
Popular Tags