Java example source code file (ObjectUtilsTest.java)
This example Java source code file (ObjectUtilsTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.exception.CloneFailedException;
import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.text.StrBuilder;
import org.junit.Test;
/**
* Unit tests {@link org.apache.commons.lang3.ObjectUtils}.
*/
@SuppressWarnings("deprecation") // deliberate use of deprecated code
public class ObjectUtilsTest {
private static final String FOO = "foo";
private static final String BAR = "bar";
//-----------------------------------------------------------------------
@Test
public void testConstructor() {
assertNotNull(new ObjectUtils());
final Constructor<?>[] cons = ObjectUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertTrue(Modifier.isPublic(cons[0].getModifiers()));
assertTrue(Modifier.isPublic(ObjectUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(ObjectUtils.class.getModifiers()));
}
//-----------------------------------------------------------------------
@Test
public void testIsNull() {
final Object o = FOO;
final 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));
}
@Test
public void testFirstNonNull() {
assertEquals("", ObjectUtils.firstNonNull(null, ""));
final String firstNonNullGenerics = ObjectUtils.firstNonNull(null, null, "123", "456");
assertEquals("123", firstNonNullGenerics);
assertEquals("123", ObjectUtils.firstNonNull("123", null, "456", null));
assertSame(Boolean.TRUE, ObjectUtils.firstNonNull(Boolean.TRUE));
// Explicitly pass in an empty array of Object type to ensure compiler doesn't complain of unchecked generic array creation
assertNull(ObjectUtils.firstNonNull(new Object[0]));
// Cast to Object in line below ensures compiler doesn't complain of unchecked generic array creation
assertNull(ObjectUtils.firstNonNull((Object) null, (Object) 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));
}
/**
* Tests {@link ObjectUtils#anyNotNull(Object...)}.
*/
@Test
public void testAnyNotNull() {
assertFalse(ObjectUtils.anyNotNull());
assertFalse(ObjectUtils.anyNotNull((Object) null));
assertFalse(ObjectUtils.anyNotNull((Object[]) null));
assertFalse(ObjectUtils.anyNotNull(null, null, null));
assertTrue(ObjectUtils.anyNotNull(FOO));
assertTrue(ObjectUtils.anyNotNull(null, FOO, null));
assertTrue(ObjectUtils.anyNotNull(null, null, null, null, FOO, BAR));
}
/**
* Tests {@link ObjectUtils#allNotNull(Object...)}.
*/
@Test
public void testAllNotNull() {
assertFalse(ObjectUtils.allNotNull((Object) null));
assertFalse(ObjectUtils.allNotNull((Object[]) null));
assertFalse(ObjectUtils.allNotNull(null, null, null));
assertFalse(ObjectUtils.allNotNull(null, FOO, BAR));
assertFalse(ObjectUtils.allNotNull(FOO, BAR, null));
assertFalse(ObjectUtils.allNotNull(FOO, BAR, null, FOO, BAR));
assertTrue(ObjectUtils.allNotNull());
assertTrue(ObjectUtils.allNotNull(FOO));
assertTrue(ObjectUtils.allNotNull(FOO, BAR, 1, Boolean.TRUE, new Object(), new Object[]{}));
}
//-----------------------------------------------------------------------
@Test
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));
}
@Test
public void testNotEqual() {
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(null, \"bar\") returned true", ObjectUtils.notEqual(null, BAR));
assertTrue("ObjectUtils.notEqual(\"foo\", \"bar\") returned true", ObjectUtils.notEqual(FOO, BAR));
assertFalse("ObjectUtils.notEqual(\"foo\", \"foo\") returned false", ObjectUtils.notEqual(FOO, FOO));
}
@Test
public void testHashCode() {
assertEquals(0, ObjectUtils.hashCode(null));
assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
}
@Test
public void testHashCodeMulti_multiple_emptyArray() {
final Object[] array = new Object[0];
assertEquals(1, ObjectUtils.hashCodeMulti(array));
}
@Test
public void testHashCodeMulti_multiple_nullArray() {
final Object[] array = null;
assertEquals(1, ObjectUtils.hashCodeMulti(array));
}
@Test
public void testHashCodeMulti_multiple_likeList() {
final List<Object> list0 = new ArrayList
Other Java examples (source code examples)
Here is a short list of links related to this Java ObjectUtilsTest.java source code file: