1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang.mutable;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  
24  /**
25   * JUnit tests.
26   * 
27   * @version $Id: MutableObjectTest.java 437554 2006-08-28 06:21:41Z bayard $
28   * @see MutableShort
29   */
30  public class MutableObjectTest extends TestCase {
31  
32      public MutableObjectTest(String testName) {
33          super(testName);
34      }
35  
36      public static void main(String[] args) {
37          TestRunner.run(suite());
38      }
39  
40      public static Test suite() {
41          return new TestSuite(MutableObjectTest.class);
42      }
43  
44      // ----------------------------------------------------------------
45      public void testConstructors() {
46          assertEquals(null, new MutableObject().getValue());
47          
48          Integer i = new Integer(6);
49          assertSame(i, new MutableObject(i).getValue());
50          assertSame("HI", new MutableObject("HI").getValue());
51          assertSame(null, new MutableObject(null).getValue());
52      }
53  
54      public void testGetSet() {
55          final MutableObject mutNum = new MutableObject();
56          assertEquals(null, new MutableObject().getValue());
57          
58          mutNum.setValue("HELLO");
59          assertSame("HELLO", mutNum.getValue());
60          
61          mutNum.setValue(null);
62          assertSame(null, mutNum.getValue());
63      }
64  
65      public void testEquals() {
66          final MutableObject mutNumA = new MutableObject("ALPHA");
67          final MutableObject mutNumB = new MutableObject("ALPHA");
68          final MutableObject mutNumC = new MutableObject("BETA");
69          final MutableObject mutNumD = new MutableObject(null);
70  
71          assertEquals(true, mutNumA.equals(mutNumA));
72          assertEquals(true, mutNumA.equals(mutNumB));
73          assertEquals(true, mutNumB.equals(mutNumA));
74          assertEquals(true, mutNumB.equals(mutNumB));
75          assertEquals(false, mutNumA.equals(mutNumC));
76          assertEquals(false, mutNumB.equals(mutNumC));
77          assertEquals(true, mutNumC.equals(mutNumC));
78          assertEquals(false, mutNumA.equals(mutNumD));
79          assertEquals(true, mutNumD.equals(mutNumD));
80          
81          assertEquals(false, mutNumA.equals(null));
82          assertEquals(false, mutNumA.equals(new Object()));
83          assertEquals(false, mutNumA.equals("0"));
84      }
85  
86      public void testHashCode() {
87          final MutableObject mutNumA = new MutableObject("ALPHA");
88          final MutableObject mutNumB = new MutableObject("ALPHA");
89          final MutableObject mutNumC = new MutableObject("BETA");
90          final MutableObject mutNumD = new MutableObject(null);
91  
92          assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
93          assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
94          assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
95          assertEquals(false, mutNumA.hashCode() == mutNumD.hashCode());
96          assertEquals(true, mutNumA.hashCode() == "ALPHA".hashCode());
97          assertEquals(0, mutNumD.hashCode());
98      }
99  
100     public void testToString() {
101         assertEquals("HI", new MutableObject("HI").toString());
102         assertEquals("10.0", new MutableObject(new Double(10)).toString());
103         assertEquals("null", new MutableObject(null).toString());
104     }
105 
106 }