junit4ify ObjectUtilsTest

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1147521 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2011-07-17 03:46:44 +00:00
parent afa12a7960
commit aee1b76fb6

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import static org.junit.Assert.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
@ -24,25 +26,21 @@
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import junit.framework.TestCase;
import org.apache.commons.lang3.exception.CloneFailedException; import org.apache.commons.lang3.exception.CloneFailedException;
import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.mutable.MutableObject;
import org.junit.Test;
/** /**
* Unit tests {@link org.apache.commons.lang3.ObjectUtils}. * Unit tests {@link org.apache.commons.lang3.ObjectUtils}.
* *
* @version $Id$ * @version $Id$
*/ */
public class ObjectUtilsTest extends TestCase { public class ObjectUtilsTest {
private static final String FOO = "foo"; private static final String FOO = "foo";
private static final String BAR = "bar"; private static final String BAR = "bar";
public ObjectUtilsTest(String name) {
super(name);
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Test
public void testConstructor() { public void testConstructor() {
assertNotNull(new ObjectUtils()); assertNotNull(new ObjectUtils());
Constructor<?>[] cons = ObjectUtils.class.getDeclaredConstructors(); Constructor<?>[] cons = ObjectUtils.class.getDeclaredConstructors();
@ -53,6 +51,7 @@ public void testConstructor() {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Test
public void testIsNull() { public void testIsNull() {
Object o = FOO; Object o = FOO;
Object dflt = BAR; Object dflt = BAR;
@ -60,6 +59,7 @@ public void testIsNull() {
assertSame("dflt was returned when o was not null", o, ObjectUtils.defaultIfNull(o, dflt)); assertSame("dflt was returned when o was not null", o, ObjectUtils.defaultIfNull(o, dflt));
} }
@Test
public void testFirstNonNull() { public void testFirstNonNull() {
assertEquals(null, ObjectUtils.firstNonNull(null, null)); assertEquals(null, ObjectUtils.firstNonNull(null, null));
assertEquals("", ObjectUtils.firstNonNull(null, "")); assertEquals("", ObjectUtils.firstNonNull(null, ""));
@ -78,6 +78,7 @@ public void testFirstNonNull() {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@Test
public void testEquals() { public void testEquals() {
assertTrue("ObjectUtils.equals(null, null) returned false", ObjectUtils.equals(null, null)); assertTrue("ObjectUtils.equals(null, null) returned false", ObjectUtils.equals(null, null));
assertTrue("ObjectUtils.equals(\"foo\", null) returned true", !ObjectUtils.equals(FOO, null)); assertTrue("ObjectUtils.equals(\"foo\", null) returned true", !ObjectUtils.equals(FOO, null));
@ -86,6 +87,7 @@ public void testEquals() {
assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO)); assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO));
} }
@Test
public void testNotEqual() { public void testNotEqual() {
assertFalse("ObjectUtils.notEqual(null, null) returned false", ObjectUtils.notEqual(null, null)); assertFalse("ObjectUtils.notEqual(null, null) returned false", ObjectUtils.notEqual(null, null));
assertTrue("ObjectUtils.notEqual(\"foo\", null) returned true", ObjectUtils.notEqual(FOO, null)); assertTrue("ObjectUtils.notEqual(\"foo\", null) returned true", ObjectUtils.notEqual(FOO, null));
@ -94,21 +96,25 @@ public void testNotEqual() {
assertFalse("ObjectUtils.notEqual(\"foo\", \"foo\") returned false", ObjectUtils.notEqual(FOO, FOO)); assertFalse("ObjectUtils.notEqual(\"foo\", \"foo\") returned false", ObjectUtils.notEqual(FOO, FOO));
} }
@Test
public void testHashCode() { public void testHashCode() {
assertEquals(0, ObjectUtils.hashCode(null)); assertEquals(0, ObjectUtils.hashCode(null));
assertEquals("a".hashCode(), ObjectUtils.hashCode("a")); assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
} }
@Test
public void testHashCodeMulti_multiple_emptyArray() { public void testHashCodeMulti_multiple_emptyArray() {
Object[] array = new Object[0]; Object[] array = new Object[0];
assertEquals(1, ObjectUtils.hashCodeMulti(array)); assertEquals(1, ObjectUtils.hashCodeMulti(array));
} }
@Test
public void testHashCodeMulti_multiple_nullArray() { public void testHashCodeMulti_multiple_nullArray() {
Object[] array = null; Object[] array = null;
assertEquals(1, ObjectUtils.hashCodeMulti(array)); assertEquals(1, ObjectUtils.hashCodeMulti(array));
} }
@Test
public void testHashCodeMulti_multiple_likeList() { public void testHashCodeMulti_multiple_likeList() {
List<Object> list0 = new ArrayList<Object>(Arrays.asList()); List<Object> list0 = new ArrayList<Object>(Arrays.asList());
assertEquals(list0.hashCode(), ObjectUtils.hashCodeMulti()); assertEquals(list0.hashCode(), ObjectUtils.hashCodeMulti());
@ -164,6 +170,7 @@ public void testHashCodeMulti_multiple_likeList() {
// //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp)); // //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp));
// } // }
@Test
public void testIdentityToString() { public void testIdentityToString() {
assertEquals(null, ObjectUtils.identityToString(null)); assertEquals(null, ObjectUtils.identityToString(null));
assertEquals( assertEquals(
@ -188,24 +195,26 @@ public void testIdentityToString() {
} }
} }
@Test
public void testToString_Object() { public void testToString_Object() {
assertEquals("", ObjectUtils.toString((Object) null) ); assertEquals("", ObjectUtils.toString((Object) null) );
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) ); assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) );
} }
@Test
public void testToString_ObjectString() { public void testToString_ObjectString() {
assertEquals(BAR, ObjectUtils.toString((Object) null, BAR) ); assertEquals(BAR, ObjectUtils.toString((Object) null, BAR) );
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) );
} }
@Test
public void testNull() { public void testNull() {
assertNotNull(ObjectUtils.NULL); assertNotNull(ObjectUtils.NULL);
assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null); assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL)); assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL));
} }
@Test
public void testMax() { public void testMax() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Date nonNullComparable1 = calendar.getTime(); Date nonNullComparable1 = calendar.getTime();
@ -231,6 +240,7 @@ public void testMax() {
assertNull( ObjectUtils.max((String)null, (String)null) ); assertNull( ObjectUtils.max((String)null, (String)null) );
} }
@Test
public void testMin() { public void testMin() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Date nonNullComparable1 = calendar.getTime(); Date nonNullComparable1 = calendar.getTime();
@ -259,6 +269,7 @@ public void testMin() {
/** /**
* Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}. * Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}.
*/ */
@Test
public void testCompare() { public void testCompare() {
Integer one = Integer.valueOf(1); Integer one = Integer.valueOf(1);
Integer two = Integer.valueOf(2); Integer two = Integer.valueOf(2);
@ -280,6 +291,7 @@ public void testCompare() {
/** /**
* Tests {@link ObjectUtils#clone(Object)} with a cloneable object. * Tests {@link ObjectUtils#clone(Object)} with a cloneable object.
*/ */
@Test
public void testCloneOfCloneable() { public void testCloneOfCloneable() {
final CloneableString string = new CloneableString("apache"); final CloneableString string = new CloneableString("apache");
final CloneableString stringClone = ObjectUtils.clone(string); final CloneableString stringClone = ObjectUtils.clone(string);
@ -289,6 +301,7 @@ public void testCloneOfCloneable() {
/** /**
* Tests {@link ObjectUtils#clone(Object)} with a not cloneable object. * Tests {@link ObjectUtils#clone(Object)} with a not cloneable object.
*/ */
@Test
public void testCloneOfNotCloneable() { public void testCloneOfNotCloneable() {
final String string = new String("apache"); final String string = new String("apache");
assertNull(ObjectUtils.clone(string)); assertNull(ObjectUtils.clone(string));
@ -297,19 +310,21 @@ public void testCloneOfNotCloneable() {
/** /**
* Tests {@link ObjectUtils#clone(Object)} with an uncloneable object. * Tests {@link ObjectUtils#clone(Object)} with an uncloneable object.
*/ */
public void testCloneOfUncloneable() { @Test(expected = NoSuchMethodException.class)
public void testCloneOfUncloneable() throws Throwable {
final UncloneableString string = new UncloneableString("apache"); final UncloneableString string = new UncloneableString("apache");
try { try {
ObjectUtils.clone(string); ObjectUtils.clone(string);
fail("Thrown " + CloneFailedException.class.getName() + " expected"); fail("Thrown " + CloneFailedException.class.getName() + " expected");
} catch (final CloneFailedException e) { } catch (final CloneFailedException e) {
assertEquals(NoSuchMethodException.class, e.getCause().getClass()); throw e.getCause();
} }
} }
/** /**
* Tests {@link ObjectUtils#clone(Object)} with an object array. * Tests {@link ObjectUtils#clone(Object)} with an object array.
*/ */
@Test
public void testCloneOfStringArray() { public void testCloneOfStringArray() {
assertTrue(Arrays.deepEquals( assertTrue(Arrays.deepEquals(
new String[]{"string"}, ObjectUtils.clone(new String[]{"string"}))); new String[]{"string"}, ObjectUtils.clone(new String[]{"string"})));
@ -318,6 +333,7 @@ public void testCloneOfStringArray() {
/** /**
* Tests {@link ObjectUtils#clone(Object)} with an array of primitives. * Tests {@link ObjectUtils#clone(Object)} with an array of primitives.
*/ */
@Test
public void testCloneOfPrimitiveArray() { public void testCloneOfPrimitiveArray() {
assertTrue(Arrays.equals(new int[]{1}, ObjectUtils.clone(new int[]{1}))); assertTrue(Arrays.equals(new int[]{1}, ObjectUtils.clone(new int[]{1})));
} }
@ -325,6 +341,7 @@ public void testCloneOfPrimitiveArray() {
/** /**
* Tests {@link ObjectUtils#cloneIfPossible(Object)} with a cloneable object. * Tests {@link ObjectUtils#cloneIfPossible(Object)} with a cloneable object.
*/ */
@Test
public void testPossibleCloneOfCloneable() { public void testPossibleCloneOfCloneable() {
final CloneableString string = new CloneableString("apache"); final CloneableString string = new CloneableString("apache");
final CloneableString stringClone = ObjectUtils.cloneIfPossible(string); final CloneableString stringClone = ObjectUtils.cloneIfPossible(string);
@ -334,6 +351,7 @@ public void testPossibleCloneOfCloneable() {
/** /**
* Tests {@link ObjectUtils#cloneIfPossible(Object)} with a not cloneable object. * Tests {@link ObjectUtils#cloneIfPossible(Object)} with a not cloneable object.
*/ */
@Test
public void testPossibleCloneOfNotCloneable() { public void testPossibleCloneOfNotCloneable() {
final String string = new String("apache"); final String string = new String("apache");
assertSame(string, ObjectUtils.cloneIfPossible(string)); assertSame(string, ObjectUtils.cloneIfPossible(string));
@ -342,13 +360,14 @@ public void testPossibleCloneOfNotCloneable() {
/** /**
* Tests {@link ObjectUtils#cloneIfPossible(Object)} with an uncloneable object. * Tests {@link ObjectUtils#cloneIfPossible(Object)} with an uncloneable object.
*/ */
public void testPossibleCloneOfUncloneable() { @Test(expected = NoSuchMethodException.class)
public void testPossibleCloneOfUncloneable() throws Throwable {
final UncloneableString string = new UncloneableString("apache"); final UncloneableString string = new UncloneableString("apache");
try { try {
ObjectUtils.cloneIfPossible(string); ObjectUtils.cloneIfPossible(string);
fail("Thrown " + CloneFailedException.class.getName() + " expected"); fail("Thrown " + CloneFailedException.class.getName() + " expected");
} catch (final CloneFailedException e) { } catch (final CloneFailedException e) {
assertEquals(NoSuchMethodException.class, e.getCause().getClass()); throw e.getCause();
} }
} }