blob: abbf0dc62c38a7bfa24ae26fc5f394c69f326d77 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.exception.CloneFailedException;
import org.apache.commons.lang3.mutable.MutableObject;
import junit.framework.TestCase;
/**
* Unit tests {@link org.apache.commons.lang3.ObjectUtils}.
*
* @author Apache Software Foundation
* @author <a href="mailto:jmcnally@collab.net">John McNally</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id$
*/
public class ObjectUtilsTest extends TestCase {
private static final String FOO = "foo";
private static final String BAR = "bar";
public ObjectUtilsTest(String name) {
super(name);
}
//-----------------------------------------------------------------------
public void testConstructor() {
assertNotNull(new ObjectUtils());
Constructor<?>[] cons = ObjectUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
assertEquals(true, Modifier.isPublic(ObjectUtils.class.getModifiers()));
assertEquals(false, Modifier.isFinal(ObjectUtils.class.getModifiers()));
}
//-----------------------------------------------------------------------
public void testIsNull() {
Object o = FOO;
Object dflt = BAR;
assertSame("dflt was not returned when o was null", dflt, ObjectUtils.defaultIfNull(null, dflt));
assertSame("dflt was returned when o was not null", o, ObjectUtils.defaultIfNull(o, dflt));
}
public void testFirstNonNull() {
assertEquals(null, ObjectUtils.firstNonNull(null, null));
assertEquals("", ObjectUtils.firstNonNull(null, ""));
String firstNonNullGenerics = ObjectUtils.firstNonNull(null, null, "123", "456");
assertEquals("123", firstNonNullGenerics);
assertEquals("123", ObjectUtils.firstNonNull("123", null, "456", null));
assertEquals(null, ObjectUtils.firstNonNull());
assertSame(Boolean.TRUE, ObjectUtils.firstNonNull(Boolean.TRUE));
assertNull(ObjectUtils.firstNonNull());
assertNull(ObjectUtils.firstNonNull(null, null));
// assertSame("123", ObjectUtils.firstNonNull(null, ObjectUtils.NULL, "123", "456"));
// assertSame("456", ObjectUtils.firstNonNull(ObjectUtils.NULL, "456", "123", null));
// assertNull(ObjectUtils.firstNonNull(null, null, ObjectUtils.NULL));
assertNull(ObjectUtils.firstNonNull((Object) null));
assertNull(ObjectUtils.firstNonNull((Object[]) null));
}
//-----------------------------------------------------------------------
public void testEquals() {
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(null, \"bar\") returned true", !ObjectUtils.equals(null, BAR));
assertTrue("ObjectUtils.equals(\"foo\", \"bar\") returned true", !ObjectUtils.equals(FOO, BAR));
assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO));
}
public void testHashCode() {
assertEquals(0, ObjectUtils.hashCode(null));
assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
}
// /**
// * Show that java.util.Date and java.sql.Timestamp are apples and oranges.
// * Prompted by an email discussion.
// *
// * The behavior is different b/w Sun Java 1.3.1_10 and 1.4.2_03.
// */
// public void testDateEqualsJava() {
// long now = 1076957313284L; // Feb 16, 2004 10:49... PST
// java.util.Date date = new java.util.Date(now);
// java.sql.Timestamp realTimestamp = new java.sql.Timestamp(now);
// java.util.Date timestamp = realTimestamp;
// // sanity check 1:
// assertEquals(284000000, realTimestamp.getNanos());
// assertEquals(1076957313284L, date.getTime());
// //
// // On Sun 1.3.1_10:
// //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000>
// //
// //assertEquals(1076957313284L, timestamp.getTime());
// //
// //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000>
// //
// //assertEquals(1076957313284L, realTimestamp.getTime());
// // sanity check 2:
// assertEquals(date.getDay(), realTimestamp.getDay());
// assertEquals(date.getHours(), realTimestamp.getHours());
// assertEquals(date.getMinutes(), realTimestamp.getMinutes());
// assertEquals(date.getMonth(), realTimestamp.getMonth());
// assertEquals(date.getSeconds(), realTimestamp.getSeconds());
// assertEquals(date.getTimezoneOffset(), realTimestamp.getTimezoneOffset());
// assertEquals(date.getYear(), realTimestamp.getYear());
// //
// // Time values are == and equals() on Sun 1.4.2_03 but NOT on Sun 1.3.1_10:
// //
// //assertFalse("Sanity check failed: date.getTime() == timestamp.getTime()", date.getTime() == timestamp.getTime());
// //assertFalse("Sanity check failed: timestamp.equals(date)", timestamp.equals(date));
// //assertFalse("Sanity check failed: date.equals(timestamp)", date.equals(timestamp));
// // real test:
// //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp));
// }
public void testIdentityToString() {
assertEquals(null, ObjectUtils.identityToString(null));
assertEquals(
"java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
ObjectUtils.identityToString(FOO));
Integer i = new Integer(90);
String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i));
assertEquals(expected, ObjectUtils.identityToString(i));
StringBuffer buffer = new StringBuffer();
ObjectUtils.identityToString(buffer, i);
assertEquals(expected, buffer.toString());
try {
ObjectUtils.identityToString(null, "tmp");
fail("NullPointerException expected");
} catch(NullPointerException npe) {
}
try {
ObjectUtils.identityToString(new StringBuffer(), null);
fail("NullPointerException expected");
} catch(NullPointerException npe) {
}
}
public void testToString_Object() {
assertEquals("", ObjectUtils.toString((Object) null) );
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) );
}
public void testToString_ObjectString() {
assertEquals(BAR, ObjectUtils.toString((Object) null, BAR) );
assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) );
}
public void testNull() {
assertNotNull(ObjectUtils.NULL);
assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL));
}
public void testMax() {
Calendar calendar = Calendar.getInstance();
Date nonNullComparable1 = calendar.getTime();
Date nonNullComparable2 = calendar.getTime();
calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 );
Date minComparable = calendar.getTime();
assertNotSame( nonNullComparable1, nonNullComparable2 );
assertSame( nonNullComparable1, ObjectUtils.max( null, nonNullComparable1 ) );
assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, null ) );
assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, nonNullComparable2 ) );
assertSame( nonNullComparable2, ObjectUtils.max( nonNullComparable2, nonNullComparable1 ) );
assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, minComparable ) );
assertSame( nonNullComparable1, ObjectUtils.max( minComparable, nonNullComparable1 ) );
assertNull( ObjectUtils.max((String)null, (String)null) );
}
public void testMin() {
Calendar calendar = Calendar.getInstance();
Date nonNullComparable1 = calendar.getTime();
Date nonNullComparable2 = calendar.getTime();
calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 );
Date minComparable = calendar.getTime();
assertNotSame( nonNullComparable1, nonNullComparable2 );
assertSame( nonNullComparable1, ObjectUtils.min( null, nonNullComparable1 ) );
assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, null ) );
assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, nonNullComparable2 ) );
assertSame( nonNullComparable2, ObjectUtils.min( nonNullComparable2, nonNullComparable1 ) );
assertSame( minComparable, ObjectUtils.min( nonNullComparable1, minComparable ) );
assertSame( minComparable, ObjectUtils.min( minComparable, nonNullComparable1 ) );
assertNull( ObjectUtils.min((String)null, (String)null) );
}
/**
* Tests {@link ObjectUtils#clone(Object)} with a cloneable object.
*/
public void testCloneOfCloneable() {
final CloneableString string = new CloneableString("apache");
final CloneableString stringClone = ObjectUtils.clone(string);
assertEquals("apache", stringClone.getValue());
}
/**
* Tests {@link ObjectUtils#clone(Object)} with a not cloneable object.
*/
public void testCloneOfNotCloneable() {
final String string = new String("apache");
assertNull(ObjectUtils.clone(string));
}
/**
* Tests {@link ObjectUtils#clone(Object)} with an uncloneable object.
*/
public void testCloneOfUncloneable() {
final UncloneableString string = new UncloneableString("apache");
try {
ObjectUtils.clone(string);
fail("Thrown " + CloneFailedException.class.getName() + " expected");
} catch (final CloneFailedException e) {
assertEquals(NoSuchMethodException.class, e.getCause().getClass());
}
}
/**
* Tests {@link ObjectUtils#cloneIfPossible(Object)} with a cloneable object.
*/
public void testPossibleCloneOfCloneable() {
final CloneableString string = new CloneableString("apache");
final CloneableString stringClone = ObjectUtils.cloneIfPossible(string);
assertEquals("apache", stringClone.getValue());
}
/**
* Tests {@link ObjectUtils#cloneIfPossible(Object)} with a not cloneable object.
*/
public void testPossibleCloneOfNotCloneable() {
final String string = new String("apache");
assertSame(string, ObjectUtils.cloneIfPossible(string));
}
/**
* Tests {@link ObjectUtils#cloneIfPossible(Object)} with an uncloneable object.
*/
public void testPossibleCloneOfUncloneable() {
final UncloneableString string = new UncloneableString("apache");
try {
ObjectUtils.cloneIfPossible(string);
fail("Thrown " + CloneFailedException.class.getName() + " expected");
} catch (final CloneFailedException e) {
assertEquals(NoSuchMethodException.class, e.getCause().getClass());
}
}
/**
* String that is cloneable.
*/
static final class CloneableString extends MutableObject<String> implements Cloneable {
private static final long serialVersionUID = 1L;
CloneableString(final String s) {
super(s);
}
@Override
public CloneableString clone() throws CloneNotSupportedException {
return (CloneableString)super.clone();
}
}
/**
* String that is not cloneable.
*/
static final class UncloneableString extends MutableObject<String> implements Cloneable {
private static final long serialVersionUID = 1L;
UncloneableString(final String s) {
super(s);
}
}
}