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: MutableObjectTest.java 437554 2006-08-28 06:21:41Z bayard $
028: * @see MutableShort
029: */
030: public class MutableObjectTest extends TestCase {
031:
032: public MutableObjectTest(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(MutableObjectTest.class);
042: }
043:
044: // ----------------------------------------------------------------
045: public void testConstructors() {
046: assertEquals(null, new MutableObject().getValue());
047:
048: Integer i = new Integer(6);
049: assertSame(i, new MutableObject(i).getValue());
050: assertSame("HI", new MutableObject("HI").getValue());
051: assertSame(null, new MutableObject(null).getValue());
052: }
053:
054: public void testGetSet() {
055: final MutableObject mutNum = new MutableObject();
056: assertEquals(null, new MutableObject().getValue());
057:
058: mutNum.setValue("HELLO");
059: assertSame("HELLO", mutNum.getValue());
060:
061: mutNum.setValue(null);
062: assertSame(null, mutNum.getValue());
063: }
064:
065: public void testEquals() {
066: final MutableObject mutNumA = new MutableObject("ALPHA");
067: final MutableObject mutNumB = new MutableObject("ALPHA");
068: final MutableObject mutNumC = new MutableObject("BETA");
069: final MutableObject mutNumD = new MutableObject(null);
070:
071: assertEquals(true, mutNumA.equals(mutNumA));
072: assertEquals(true, mutNumA.equals(mutNumB));
073: assertEquals(true, mutNumB.equals(mutNumA));
074: assertEquals(true, mutNumB.equals(mutNumB));
075: assertEquals(false, mutNumA.equals(mutNumC));
076: assertEquals(false, mutNumB.equals(mutNumC));
077: assertEquals(true, mutNumC.equals(mutNumC));
078: assertEquals(false, mutNumA.equals(mutNumD));
079: assertEquals(true, mutNumD.equals(mutNumD));
080:
081: assertEquals(false, mutNumA.equals(null));
082: assertEquals(false, mutNumA.equals(new Object()));
083: assertEquals(false, mutNumA.equals("0"));
084: }
085:
086: public void testHashCode() {
087: final MutableObject mutNumA = new MutableObject("ALPHA");
088: final MutableObject mutNumB = new MutableObject("ALPHA");
089: final MutableObject mutNumC = new MutableObject("BETA");
090: final MutableObject mutNumD = new MutableObject(null);
091:
092: assertEquals(true, mutNumA.hashCode() == mutNumA.hashCode());
093: assertEquals(true, mutNumA.hashCode() == mutNumB.hashCode());
094: assertEquals(false, mutNumA.hashCode() == mutNumC.hashCode());
095: assertEquals(false, mutNumA.hashCode() == mutNumD.hashCode());
096: assertEquals(true, mutNumA.hashCode() == "ALPHA".hashCode());
097: assertEquals(0, mutNumD.hashCode());
098: }
099:
100: public void testToString() {
101: assertEquals("HI", new MutableObject("HI").toString());
102: assertEquals("10.0", new MutableObject(new Double(10))
103: .toString());
104: assertEquals("null", new MutableObject(null).toString());
105: }
106:
107: }
|