1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Modifier ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 35 public class ObjectUtilsTest extends TestCase { 36 private static final String FOO = "foo"; 37 private static final String BAR = "bar"; 38 39 public ObjectUtilsTest(String name) { 40 super(name); 41 } 42 43 public static void main(String [] args) { 44 TestRunner.run(suite()); 45 } 46 47 public static Test suite() { 48 TestSuite suite = new TestSuite(ObjectUtilsTest.class); 49 suite.setName("ObjectUtils Tests"); 50 return suite; 51 } 52 53 protected void setUp() throws Exception { 54 super.setUp(); 55 } 56 57 protected void tearDown() throws Exception { 58 super.tearDown(); 59 } 60 61 public void testConstructor() { 63 assertNotNull(new ObjectUtils()); 64 Constructor [] cons = ObjectUtils.class.getDeclaredConstructors(); 65 assertEquals(1, cons.length); 66 assertEquals(true, Modifier.isPublic(cons[0].getModifiers())); 67 assertEquals(true, Modifier.isPublic(ObjectUtils.class.getModifiers())); 68 assertEquals(false, Modifier.isFinal(ObjectUtils.class.getModifiers())); 69 } 70 71 public void testIsNull() { 73 Object o = FOO; 74 Object dflt = BAR; 75 assertSame("dflt was not returned when o was null", dflt, ObjectUtils.defaultIfNull(null, dflt)); 76 assertSame("dflt was returned when o was not null", o, ObjectUtils.defaultIfNull(o, dflt)); 77 } 78 79 public void testEquals() { 80 assertTrue("ObjectUtils.equals(null, null) returned false", ObjectUtils.equals(null, null)); 81 assertTrue("ObjectUtils.equals(\"foo\", null) returned true", !ObjectUtils.equals(FOO, null)); 82 assertTrue("ObjectUtils.equals(null, \"bar\") returned true", !ObjectUtils.equals(null, BAR)); 83 assertTrue("ObjectUtils.equals(\"foo\", \"bar\") returned true", !ObjectUtils.equals(FOO, BAR)); 84 assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO)); 85 } 86 87 public void testHashCode() { 88 assertEquals(0, ObjectUtils.hashCode(null)); 89 assertEquals("a".hashCode(), ObjectUtils.hashCode("a")); 90 } 91 92 133 public void testIdentityToString() { 134 assertEquals(null, ObjectUtils.identityToString(null)); 135 assertEquals( 136 "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), 137 ObjectUtils.identityToString(FOO)); 138 Integer i = new Integer (90); 139 assertEquals( 140 "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)), 141 ObjectUtils.identityToString(i)); 142 } 143 144 public void testAppendIdentityToString() { 145 assertEquals(null, ObjectUtils.appendIdentityToString(null, null)); 146 assertEquals(null, ObjectUtils.appendIdentityToString(new StringBuffer (), null)); 147 assertEquals( 148 "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), 149 ObjectUtils.appendIdentityToString(null, FOO).toString()); 150 assertEquals( 151 "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), 152 ObjectUtils.appendIdentityToString(new StringBuffer (), FOO).toString()); 153 Integer val = new Integer (90); 154 assertEquals( 155 "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(val)), 156 ObjectUtils.appendIdentityToString(null, val).toString()); 157 assertEquals( 158 "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(val)), 159 ObjectUtils.appendIdentityToString(new StringBuffer (), val).toString()); 160 } 161 162 public void testToString_Object() { 163 assertEquals("", ObjectUtils.toString((Object ) null) ); 164 assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) ); 165 } 166 167 public void testToString_ObjectString() { 168 assertEquals(BAR, ObjectUtils.toString((Object ) null, BAR) ); 169 assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); 170 } 171 172 public void testNull() { 173 assertTrue(ObjectUtils.NULL != null); 174 assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null); 175 assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL)); 176 } 177 178 } 179 | Popular Tags |