KickJava   Java API By Example, From Geeks To Geeks.

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


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: MutableObjectTest.java 161244 2005-04-14 06:16:36Z ggregory $
27  * @see MutableShort
28  */

29 public class MutableObjectTest extends TestCase {
30
31     public MutableObjectTest(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(MutableObjectTest.class);
41     }
42
43     // ----------------------------------------------------------------
44
public void testConstructors() {
45         assertEquals(null, new MutableObject().getValue());
46         
47         Integer JavaDoc i = new Integer JavaDoc(6);
48         assertSame(i, new MutableObject(i).getValue());
49         assertSame("HI", new MutableObject("HI").getValue());
50         assertSame(null, new MutableObject(null).getValue());
51     }
52
53     public void testGetSet() {
54         final MutableObject mutNum = new MutableObject();
55         assertEquals(null, new MutableObject().getValue());
56         
57         mutNum.setValue("HELLO");
58         assertSame("HELLO", mutNum.getValue());
59         
60         mutNum.setValue(null);
61         assertSame(null, mutNum.getValue());
62     }
63
64     public void testEquals() {
65         final MutableObject mutNumA = new MutableObject("ALPHA");
66         final MutableObject mutNumB = new MutableObject("ALPHA");
67         final MutableObject mutNumC = new MutableObject("BETA");
68         final MutableObject mutNumD = new MutableObject(null);
69
70         assertEquals(true, mutNumA.equals(mutNumA));
71         assertEquals(true, mutNumA.equals(mutNumB));
72         assertEquals(true, mutNumB.equals(mutNumA));
73         assertEquals(true, mutNumB.equals(mutNumB));
74         assertEquals(false, mutNumA.equals(mutNumC));
75         assertEquals(false, mutNumB.equals(mutNumC));
76         assertEquals(true, mutNumC.equals(mutNumC));
77         assertEquals(false, mutNumA.equals(mutNumD));
78         assertEquals(true, mutNumD.equals(mutNumD));
79         
80         assertEquals(false, mutNumA.equals(null));
81         assertEquals(false, mutNumA.equals(new Object JavaDoc()));
82         assertEquals(false, mutNumA.equals("0"));
83     }
84
85     public void testHashCode() {
86         final MutableObject mutNumA = new MutableObject("ALPHA");
87         final MutableObject mutNumB = new MutableObject("ALPHA");
88         final MutableObject mutNumC = new MutableObject("BETA");
89         final MutableObject mutNumD = new MutableObject(null);
90
91         assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
92         assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
93         assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
94         assertEquals(false, mutNumA.hashCode() == mutNumD.hashCode());
95         assertEquals(true, mutNumA.hashCode() == "ALPHA".hashCode());
96         assertEquals(0, mutNumD.hashCode());
97     }
98
99     public void testToString() {
100         assertEquals("HI", new MutableObject("HI").toString());
101         assertEquals("10.0", new MutableObject(new Double JavaDoc(10)).toString());
102         assertEquals("null", new MutableObject(null).toString());
103     }
104
105 }
106
Popular Tags