1 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 29 public class MutableObjectTest extends TestCase { 30 31 public MutableObjectTest(String testName) { 32 super(testName); 33 } 34 35 public static void main(String [] args) { 36 TestRunner.run(suite()); 37 } 38 39 public static Test suite() { 40 return new TestSuite(MutableObjectTest.class); 41 } 42 43 public void testConstructors() { 45 assertEquals(null, new MutableObject().getValue()); 46 47 Integer i = new Integer (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 ())); 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 (10)).toString()); 102 assertEquals("null", new MutableObject(null).toString()); 103 } 104 105 } 106 | Popular Tags |