From 43f0424827dfcbc3a301007b5d5ca0e412473948 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 25 Feb 2021 08:53:59 -0500 Subject: [PATCH] Sort members. --- .../apache/commons/lang3/ObjectUtilsTest.java | 1124 ++++++++--------- 1 file changed, 562 insertions(+), 562 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index e9e9d99e2..b61ec8cef 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -55,170 +55,84 @@ import org.junit.jupiter.api.Test; */ @SuppressWarnings("deprecation") // deliberate use of deprecated code public class ObjectUtilsTest { + static final class CharSequenceComparator implements Comparator { + + @Override + public int compare(final CharSequence o1, final CharSequence o2) { + return o1.toString().compareTo(o2.toString()); + } + + } + /** + * String that is cloneable. + */ + static final class CloneableString extends MutableObject implements Cloneable { + private static final long serialVersionUID = 1L; + CloneableString(final String s) { + super(s); + } + + @Override + public CloneableString clone() throws CloneNotSupportedException { + return (CloneableString) super.clone(); + } + } + static final class NonComparableCharSequence implements CharSequence { + final String value; + + /** + * Create a new NonComparableCharSequence instance. + * + * @param value the CharSequence value + */ + NonComparableCharSequence(final String value) { + Validate.notNull(value); + this.value = value; + } + + @Override + public char charAt(final int arg0) { + return value.charAt(arg0); + } + + @Override + public int length() { + return value.length(); + } + + @Override + public CharSequence subSequence(final int arg0, final int arg1) { + return value.subSequence(arg0, arg1); + } + + @Override + public String toString() { + return value; + } + } + /** + * String that is not cloneable. + */ + static final class UncloneableString extends MutableObject implements Cloneable { + private static final long serialVersionUID = 1L; + UncloneableString(final String s) { + super(s); + } + } private static final String FOO = "foo"; private static final String BAR = "bar"; private static final String[] NON_EMPTY_ARRAY = new String[] { FOO, BAR, }; + private static final List NON_EMPTY_LIST = Arrays.asList(NON_EMPTY_ARRAY); + private static final Set NON_EMPTY_SET = new HashSet<>(NON_EMPTY_LIST); + private static final Map NON_EMPTY_MAP = new HashMap<>(); + static { NON_EMPTY_MAP.put(FOO, 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 testIsEmpty() { - assertTrue(ObjectUtils.isEmpty(null)); - assertTrue(ObjectUtils.isEmpty("")); - assertTrue(ObjectUtils.isEmpty(new int[] {})); - assertTrue(ObjectUtils.isEmpty(Collections.emptyList())); - assertTrue(ObjectUtils.isEmpty(Collections.emptySet())); - assertTrue(ObjectUtils.isEmpty(Collections.emptyMap())); - - assertFalse(ObjectUtils.isEmpty(" ")); - assertFalse(ObjectUtils.isEmpty("ab")); - assertFalse(ObjectUtils.isEmpty(NON_EMPTY_ARRAY)); - assertFalse(ObjectUtils.isEmpty(NON_EMPTY_LIST)); - assertFalse(ObjectUtils.isEmpty(NON_EMPTY_SET)); - assertFalse(ObjectUtils.isEmpty(NON_EMPTY_MAP)); - } - - @Test - public void testIsNotEmpty() { - assertFalse(ObjectUtils.isNotEmpty(null)); - assertFalse(ObjectUtils.isNotEmpty("")); - assertFalse(ObjectUtils.isNotEmpty(new int[] {})); - assertFalse(ObjectUtils.isNotEmpty(Collections.emptyList())); - assertFalse(ObjectUtils.isNotEmpty(Collections.emptySet())); - assertFalse(ObjectUtils.isNotEmpty(Collections.emptyMap())); - - assertTrue(ObjectUtils.isNotEmpty(" ")); - assertTrue(ObjectUtils.isNotEmpty("ab")); - assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_ARRAY)); - assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_LIST)); - assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_SET)); - assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_MAP)); - } - - //----------------------------------------------------------------------- - @Test - public void testDefaultIfNull() { - final Object o = FOO; - final Object dflt = BAR; - assertSame(dflt, ObjectUtils.defaultIfNull(null, dflt), "dflt was not returned when o was null"); - assertSame(o, ObjectUtils.defaultIfNull(o, dflt), "dflt was returned when o was not null"); - assertSame(dflt, ObjectUtils.getIfNull(null, () -> dflt), "dflt was not returned when o was null"); - assertSame(o, ObjectUtils.getIfNull(o, () -> dflt), "dflt was returned when o was not null"); - assertSame(o, ObjectUtils.getIfNull(FOO, () -> dflt), "dflt was returned when o was not null"); - assertSame(o, ObjectUtils.getIfNull("foo", () -> dflt), "dflt was returned when o was not null"); - final MutableInt callsCounter = new MutableInt(0); - final Supplier countingDefaultSupplier = () -> { - callsCounter.increment(); - return dflt; - }; - ObjectUtils.getIfNull(o, countingDefaultSupplier); - assertEquals(0, callsCounter.getValue()); - ObjectUtils.getIfNull(null, countingDefaultSupplier); - assertEquals(1, callsCounter.getValue()); - } - - @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()); - - // Cast to Object in line below ensures compiler doesn't complain of unchecked generic array creation - assertNull(ObjectUtils.firstNonNull(null, null)); - - assertNull(ObjectUtils.firstNonNull((Object) null)); - assertNull(ObjectUtils.firstNonNull((Object[]) null)); - } - - @Test - public void testGetFirstNonNull() { - // first non null - assertEquals("", ObjectUtils.getFirstNonNull(() -> null, () -> "")); - // first encountered value is used - assertEquals("1", ObjectUtils.getFirstNonNull(() -> null, () -> "1", () -> "2", () -> null)); - assertEquals("123", ObjectUtils.getFirstNonNull(() -> "123", () -> null, () -> "456")); - // don't evaluate suppliers after first value is found - assertEquals("123", ObjectUtils.getFirstNonNull(() -> null, () -> "123", () -> fail("Supplier after first non-null value should not be evaluated"))); - // supplier returning null and null supplier both result in null - assertNull(ObjectUtils.getFirstNonNull(null, () -> null)); - // Explicitly pass in an empty array of Object type to ensure compiler doesn't complain of unchecked generic array creation - assertNull(ObjectUtils.getFirstNonNull()); - // supplier is null - assertNull(ObjectUtils.getFirstNonNull((Supplier) null)); - // varargs array itself is null - assertNull(ObjectUtils.getFirstNonNull((Supplier[]) null)); - // test different types - assertEquals(1, ObjectUtils.getFirstNonNull(() -> null, () -> 1)); - assertEquals(Boolean.TRUE, ObjectUtils.getFirstNonNull(() -> null, () -> Boolean.TRUE)); - } - - /** - * Tests {@link ObjectUtils#anyNull(Object...)}. - */ - @Test - public void testAnyNull() { - assertTrue(ObjectUtils.anyNull((Object) null)); - assertTrue(ObjectUtils.anyNull(null, null, null)); - assertTrue(ObjectUtils.anyNull(null, FOO, BAR)); - assertTrue(ObjectUtils.anyNull(FOO, BAR, null)); - assertTrue(ObjectUtils.anyNull(FOO, BAR, null, FOO, BAR)); - - assertFalse(ObjectUtils.anyNull()); - assertFalse(ObjectUtils.anyNull(FOO)); - assertFalse(ObjectUtils.anyNull(FOO, BAR, 1, Boolean.TRUE, new Object(), new Object[]{})); - } - - /** - * 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#allNull(Object...)}. - */ - @Test - public void testAllNull() { - assertTrue(ObjectUtils.allNull()); - assertTrue(ObjectUtils.allNull((Object) null)); - assertTrue(ObjectUtils.allNull((Object[]) null)); - assertTrue(ObjectUtils.allNull(null, null, null)); - - assertFalse(ObjectUtils.allNull(FOO)); - assertFalse(ObjectUtils.allNull(null, FOO, null)); - assertFalse(ObjectUtils.allNull(null, null, null, null, FOO, BAR)); - } - /** * Tests {@link ObjectUtils#allNotNull(Object...)}. */ @@ -236,323 +150,50 @@ public class ObjectUtilsTest { assertTrue(ObjectUtils.allNotNull(FOO, BAR, 1, Boolean.TRUE, new Object(), new Object[]{})); } - //----------------------------------------------------------------------- + /** + * Tests {@link ObjectUtils#allNull(Object...)}. + */ @Test - public void testEquals() { - assertTrue(ObjectUtils.equals(null, null), "ObjectUtils.equals(null, null) returned false"); - assertTrue(!ObjectUtils.equals(FOO, null), "ObjectUtils.equals(\"foo\", null) returned true"); - assertTrue(!ObjectUtils.equals(null, BAR), "ObjectUtils.equals(null, \"bar\") returned true"); - assertTrue(!ObjectUtils.equals(FOO, BAR), "ObjectUtils.equals(\"foo\", \"bar\") returned true"); - assertTrue(ObjectUtils.equals(FOO, FOO), "ObjectUtils.equals(\"foo\", \"foo\") returned false"); - } + public void testAllNull() { + assertTrue(ObjectUtils.allNull()); + assertTrue(ObjectUtils.allNull((Object) null)); + assertTrue(ObjectUtils.allNull((Object[]) null)); + assertTrue(ObjectUtils.allNull(null, null, null)); - @Test - public void testNotEqual() { - assertFalse(ObjectUtils.notEqual(null, null), "ObjectUtils.notEqual(null, null) returned false"); - assertTrue(ObjectUtils.notEqual(FOO, null), "ObjectUtils.notEqual(\"foo\", null) returned true"); - assertTrue(ObjectUtils.notEqual(null, BAR), "ObjectUtils.notEqual(null, \"bar\") returned true"); - assertTrue(ObjectUtils.notEqual(FOO, BAR), "ObjectUtils.notEqual(\"foo\", \"bar\") returned true"); - assertFalse(ObjectUtils.notEqual(FOO, FOO), "ObjectUtils.notEqual(\"foo\", \"foo\") returned false"); - } - - @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 list0 = new ArrayList<>(Collections.emptyList()); - assertEquals(list0.hashCode(), ObjectUtils.hashCodeMulti()); - - final List list1 = new ArrayList<>(Collections.singletonList("a")); - assertEquals(list1.hashCode(), ObjectUtils.hashCodeMulti("a")); - - final List list2 = new ArrayList<>(Arrays.asList("a", "b")); - assertEquals(list2.hashCode(), ObjectUtils.hashCodeMulti("a", "b")); - - final List list3 = new ArrayList<>(Arrays.asList("a", "b", "c")); - assertEquals(list3.hashCode(), ObjectUtils.hashCodeMulti("a", "b", "c")); - } - - @Test - public void testIdentityToStringStringBuffer() { - final Integer i = Integer.valueOf(45); - final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); - - final StringBuffer buffer = new StringBuffer(); - ObjectUtils.identityToString(buffer, i); - assertEquals(expected, buffer.toString()); - - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((StringBuffer) null, "tmp")); - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString(new StringBuffer(), null)); - } - - @Test - public void testIdentityToStringObjectNull() { - assertNull(ObjectUtils.identityToString(null)); - } - - @Test - public void testIdentityToStringInteger() { - final Integer i = Integer.valueOf(90); - final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); - - assertEquals(expected, ObjectUtils.identityToString(i)); - } - - @Test - public void testIdentityToStringString() { - assertEquals( - "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), - ObjectUtils.identityToString(FOO)); - } - - @Test - public void testIdentityToStringStringBuilder() { - final Integer i = Integer.valueOf(90); - final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); - - final StringBuilder builder = new StringBuilder(); - ObjectUtils.identityToString(builder, i); - assertEquals(expected, builder.toString()); - } - - @Test - public void testIdentityToStringStringBuilderInUse() { - final Integer i = Integer.valueOf(90); - final String expected = "ABC = java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); - - final StringBuilder builder = new StringBuilder("ABC = "); - ObjectUtils.identityToString(builder, i); - assertEquals(expected, builder.toString()); - } - - @Test - public void testIdentityToStringStringBuilderNullValue() { - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString(new StringBuilder(), null)); - } - - @Test - public void testIdentityToStringStringBuilderNullStringBuilder() { - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((StringBuilder) null, "tmp")); - } - - @Test - public void testIdentityToStringStrBuilder() { - final Integer i = Integer.valueOf(102); - final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); - - final StrBuilder builder = new StrBuilder(); - ObjectUtils.identityToString(builder, i); - assertEquals(expected, builder.toString()); - - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((StrBuilder) null, "tmp")); - - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString(new StrBuilder(), null)); - } - - @Test - public void testIdentityToStringAppendable() throws IOException { - final Integer i = Integer.valueOf(121); - final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); - - final Appendable appendable = new StringBuilder(); - ObjectUtils.identityToString(appendable, i); - assertEquals(expected, appendable.toString()); - - assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((Appendable) null, "tmp")); - - assertThrows( - NullPointerException.class, - () -> ObjectUtils.identityToString((Appendable) (new StringBuilder()), null)); - } - - @Test - public void testToString_Object() { - assertEquals("", ObjectUtils.toString(null) ); - assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) ); - } - - @Test - public void testToString_ObjectString() { - assertEquals(BAR, ObjectUtils.toString(null, BAR) ); - assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); - } - - @Test - public void testToString_SupplierString() { - assertEquals(null, ObjectUtils.toString(null, (Supplier) null)); - assertEquals(null, ObjectUtils.toString(null, () -> null)); - // Pretend computing BAR is expensive. - assertEquals(BAR, ObjectUtils.toString(null, () -> BAR)); - assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, () -> BAR)); - } - - @SuppressWarnings("cast") // 1 OK, because we are checking for code change - @Test - public void testNull() { - assertNotNull(ObjectUtils.NULL); - // 1 Check that NULL really is a Null i.e. the definition has not been changed - assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null); - assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL)); - } - - @Test - public void testMax() { - final Calendar calendar = Calendar.getInstance(); - final Date nonNullComparable1 = calendar.getTime(); - final Date nonNullComparable2 = calendar.getTime(); - final String[] nullArray = null; - - calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 ); - final Date minComparable = calendar.getTime(); - - assertNotSame( nonNullComparable1, nonNullComparable2 ); - - assertNull(ObjectUtils.max( (String) null ) ); - assertNull(ObjectUtils.max( nullArray ) ); - assertSame( nonNullComparable1, ObjectUtils.max( null, nonNullComparable1 ) ); - assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, null ) ); - assertSame( nonNullComparable1, ObjectUtils.max( null, 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 ) ); - assertSame( nonNullComparable1, ObjectUtils.max( null, minComparable, null, nonNullComparable1 ) ); - - assertNull( ObjectUtils.max(null, null) ); - } - - @Test - public void testMin() { - final Calendar calendar = Calendar.getInstance(); - final Date nonNullComparable1 = calendar.getTime(); - final Date nonNullComparable2 = calendar.getTime(); - final String[] nullArray = null; - - calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 ); - final Date minComparable = calendar.getTime(); - - assertNotSame( nonNullComparable1, nonNullComparable2 ); - - assertNull(ObjectUtils.min( (String) null ) ); - assertNull(ObjectUtils.min( nullArray ) ); - assertSame( nonNullComparable1, ObjectUtils.min( null, nonNullComparable1 ) ); - assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, null ) ); - assertSame( nonNullComparable1, ObjectUtils.min( null, 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 ) ); - assertSame( minComparable, ObjectUtils.min( null, nonNullComparable1, null, minComparable ) ); - - assertNull( ObjectUtils.min(null, null) ); + assertFalse(ObjectUtils.allNull(FOO)); + assertFalse(ObjectUtils.allNull(null, FOO, null)); + assertFalse(ObjectUtils.allNull(null, null, null, null, FOO, BAR)); } /** - * Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}. + * Tests {@link ObjectUtils#anyNotNull(Object...)}. */ @Test - public void testCompare() { - final Integer one = Integer.valueOf(1); - final Integer two = Integer.valueOf(2); - final Integer nullValue = null; + public void testAnyNotNull() { + assertFalse(ObjectUtils.anyNotNull()); + assertFalse(ObjectUtils.anyNotNull((Object) null)); + assertFalse(ObjectUtils.anyNotNull((Object[]) null)); + assertFalse(ObjectUtils.anyNotNull(null, null, null)); - assertEquals(0, ObjectUtils.compare(nullValue, nullValue), "Null Null false"); - assertEquals(0, ObjectUtils.compare(nullValue, nullValue, true), "Null Null true"); - - assertEquals(-1, ObjectUtils.compare(nullValue, one), "Null one false"); - assertEquals(1, ObjectUtils.compare(nullValue, one, true), "Null one true"); - - assertEquals(1, ObjectUtils.compare(one, nullValue), "one Null false"); - assertEquals(-1, ObjectUtils.compare(one, nullValue, true), "one Null true"); - - assertEquals(-1, ObjectUtils.compare(one, two), "one two false"); - assertEquals(-1, ObjectUtils.compare(one, two, true), "one two true"); + assertTrue(ObjectUtils.anyNotNull(FOO)); + assertTrue(ObjectUtils.anyNotNull(null, FOO, null)); + assertTrue(ObjectUtils.anyNotNull(null, null, null, null, FOO, BAR)); } + /** + * Tests {@link ObjectUtils#anyNull(Object...)}. + */ @Test - public void testMedian() { - assertEquals("foo", ObjectUtils.median("foo")); - assertEquals("bar", ObjectUtils.median("foo", "bar")); - assertEquals("baz", ObjectUtils.median("foo", "bar", "baz")); - assertEquals("baz", ObjectUtils.median("foo", "bar", "baz", "blah")); - assertEquals("blah", ObjectUtils.median("foo", "bar", "baz", "blah", "wah")); - assertEquals(Integer.valueOf(5), - ObjectUtils.median(Integer.valueOf(1), Integer.valueOf(5), Integer.valueOf(10))); - assertEquals( - Integer.valueOf(7), - ObjectUtils.median(Integer.valueOf(5), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8), - Integer.valueOf(9))); - assertEquals(Integer.valueOf(6), - ObjectUtils.median(Integer.valueOf(5), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8))); - } + public void testAnyNull() { + assertTrue(ObjectUtils.anyNull((Object) null)); + assertTrue(ObjectUtils.anyNull(null, null, null)); + assertTrue(ObjectUtils.anyNull(null, FOO, BAR)); + assertTrue(ObjectUtils.anyNull(FOO, BAR, null)); + assertTrue(ObjectUtils.anyNull(FOO, BAR, null, FOO, BAR)); - @Test - public void testMedian_nullItems() { - assertThrows(NullPointerException.class, () -> ObjectUtils.median((String[]) null)); - } - - @Test - public void testMedian_emptyItems() { - assertThrows(IllegalArgumentException.class, ObjectUtils::median); - } - - @Test - public void testComparatorMedian() { - final CharSequenceComparator cmp = new CharSequenceComparator(); - final NonComparableCharSequence foo = new NonComparableCharSequence("foo"); - final NonComparableCharSequence bar = new NonComparableCharSequence("bar"); - final NonComparableCharSequence baz = new NonComparableCharSequence("baz"); - final NonComparableCharSequence blah = new NonComparableCharSequence("blah"); - final NonComparableCharSequence wah = new NonComparableCharSequence("wah"); - assertSame(foo, ObjectUtils.median(cmp, foo)); - assertSame(bar, ObjectUtils.median(cmp, foo, bar)); - assertSame(baz, ObjectUtils.median(cmp, foo, bar, baz)); - assertSame(baz, ObjectUtils.median(cmp, foo, bar, baz, blah)); - assertSame(blah, ObjectUtils.median(cmp, foo, bar, baz, blah, wah)); - } - - @Test - public void testComparatorMedian_nullComparator() { - assertThrows(NullPointerException.class, - () -> ObjectUtils.median((Comparator) null, new NonComparableCharSequence("foo"))); - } - - @Test - public void testComparatorMedian_nullItems() { - assertThrows(NullPointerException.class, - () -> ObjectUtils.median(new CharSequenceComparator(), (CharSequence[]) null)); - } - - @Test - public void testComparatorMedian_emptyItems() { - assertThrows(IllegalArgumentException.class, () -> ObjectUtils.median(new CharSequenceComparator())); - } - - @Test - public void testMode() { - assertNull(ObjectUtils.mode((Object[]) null)); - assertNull(ObjectUtils.mode()); - assertNull(ObjectUtils.mode("foo", "bar", "baz")); - assertNull(ObjectUtils.mode("foo", "bar", "baz", "foo", "bar")); - assertEquals("foo", ObjectUtils.mode("foo", "bar", "baz", "foo")); - assertEquals(Integer.valueOf(9), - ObjectUtils.mode("foo", "bar", "baz", Integer.valueOf(9), Integer.valueOf(10), Integer.valueOf(9))); + assertFalse(ObjectUtils.anyNull()); + assertFalse(ObjectUtils.anyNull(FOO)); + assertFalse(ObjectUtils.anyNull(FOO, BAR, 1, Boolean.TRUE, new Object(), new Object[]{})); } /** @@ -575,13 +216,11 @@ public class ObjectUtilsTest { } /** - * Tests {@link ObjectUtils#clone(Object)} with an uncloneable object. + * Tests {@link ObjectUtils#clone(Object)} with an array of primitives. */ @Test - public void testCloneOfUncloneable() { - final UncloneableString string = new UncloneableString("apache"); - final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.clone(string)); - assertEquals(NoSuchMethodException.class, e.getCause().getClass()); + public void testCloneOfPrimitiveArray() { + assertArrayEquals(new int[]{1}, ObjectUtils.clone(new int[]{1})); } /** @@ -594,42 +233,69 @@ public class ObjectUtilsTest { } /** - * Tests {@link ObjectUtils#clone(Object)} with an array of primitives. + * Tests {@link ObjectUtils#clone(Object)} with an uncloneable object. */ @Test - public void testCloneOfPrimitiveArray() { - assertArrayEquals(new int[]{1}, ObjectUtils.clone(new int[]{1})); - } - - /** - * Tests {@link ObjectUtils#cloneIfPossible(Object)} with a cloneable object. - */ - @Test - 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. - */ - @Test - public void testPossibleCloneOfNotCloneable() { - final String string = "apache"; - assertSame(string, ObjectUtils.cloneIfPossible(string)); - } - - /** - * Tests {@link ObjectUtils#cloneIfPossible(Object)} with an uncloneable object. - */ - @Test - public void testPossibleCloneOfUncloneable() { + public void testCloneOfUncloneable() { final UncloneableString string = new UncloneableString("apache"); - final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.cloneIfPossible(string)); + final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.clone(string)); assertEquals(NoSuchMethodException.class, e.getCause().getClass()); } + @Test + public void testComparatorMedian() { + final CharSequenceComparator cmp = new CharSequenceComparator(); + final NonComparableCharSequence foo = new NonComparableCharSequence("foo"); + final NonComparableCharSequence bar = new NonComparableCharSequence("bar"); + final NonComparableCharSequence baz = new NonComparableCharSequence("baz"); + final NonComparableCharSequence blah = new NonComparableCharSequence("blah"); + final NonComparableCharSequence wah = new NonComparableCharSequence("wah"); + assertSame(foo, ObjectUtils.median(cmp, foo)); + assertSame(bar, ObjectUtils.median(cmp, foo, bar)); + assertSame(baz, ObjectUtils.median(cmp, foo, bar, baz)); + assertSame(baz, ObjectUtils.median(cmp, foo, bar, baz, blah)); + assertSame(blah, ObjectUtils.median(cmp, foo, bar, baz, blah, wah)); + } + + @Test + public void testComparatorMedian_emptyItems() { + assertThrows(IllegalArgumentException.class, () -> ObjectUtils.median(new CharSequenceComparator())); + } + + @Test + public void testComparatorMedian_nullComparator() { + assertThrows(NullPointerException.class, + () -> ObjectUtils.median((Comparator) null, new NonComparableCharSequence("foo"))); + } + + @Test + public void testComparatorMedian_nullItems() { + assertThrows(NullPointerException.class, + () -> ObjectUtils.median(new CharSequenceComparator(), (CharSequence[]) null)); + } + + /** + * Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}. + */ + @Test + public void testCompare() { + final Integer one = Integer.valueOf(1); + final Integer two = Integer.valueOf(2); + final Integer nullValue = null; + + assertEquals(0, ObjectUtils.compare(nullValue, nullValue), "Null Null false"); + assertEquals(0, ObjectUtils.compare(nullValue, nullValue, true), "Null Null true"); + + assertEquals(-1, ObjectUtils.compare(nullValue, one), "Null one false"); + assertEquals(1, ObjectUtils.compare(nullValue, one, true), "Null one true"); + + assertEquals(1, ObjectUtils.compare(one, nullValue), "one Null false"); + assertEquals(-1, ObjectUtils.compare(one, nullValue, true), "one Null true"); + + assertEquals(-1, ObjectUtils.compare(one, two), "one two false"); + assertEquals(-1, ObjectUtils.compare(one, two, true), "one two true"); + } + @Test public void testConstMethods() { @@ -697,77 +363,411 @@ public class ObjectUtilsTest { "CONST_SHORT(32768): IllegalArgumentException should have been thrown."); } + //----------------------------------------------------------------------- + @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 testDefaultIfNull() { + final Object o = FOO; + final Object dflt = BAR; + assertSame(dflt, ObjectUtils.defaultIfNull(null, dflt), "dflt was not returned when o was null"); + assertSame(o, ObjectUtils.defaultIfNull(o, dflt), "dflt was returned when o was not null"); + assertSame(dflt, ObjectUtils.getIfNull(null, () -> dflt), "dflt was not returned when o was null"); + assertSame(o, ObjectUtils.getIfNull(o, () -> dflt), "dflt was returned when o was not null"); + assertSame(o, ObjectUtils.getIfNull(FOO, () -> dflt), "dflt was returned when o was not null"); + assertSame(o, ObjectUtils.getIfNull("foo", () -> dflt), "dflt was returned when o was not null"); + final MutableInt callsCounter = new MutableInt(0); + final Supplier countingDefaultSupplier = () -> { + callsCounter.increment(); + return dflt; + }; + ObjectUtils.getIfNull(o, countingDefaultSupplier); + assertEquals(0, callsCounter.getValue()); + ObjectUtils.getIfNull(null, countingDefaultSupplier); + assertEquals(1, callsCounter.getValue()); + } + + //----------------------------------------------------------------------- + @Test + public void testEquals() { + assertTrue(ObjectUtils.equals(null, null), "ObjectUtils.equals(null, null) returned false"); + assertTrue(!ObjectUtils.equals(FOO, null), "ObjectUtils.equals(\"foo\", null) returned true"); + assertTrue(!ObjectUtils.equals(null, BAR), "ObjectUtils.equals(null, \"bar\") returned true"); + assertTrue(!ObjectUtils.equals(FOO, BAR), "ObjectUtils.equals(\"foo\", \"bar\") returned true"); + assertTrue(ObjectUtils.equals(FOO, FOO), "ObjectUtils.equals(\"foo\", \"foo\") returned false"); + } + + @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()); + + // Cast to Object in line below ensures compiler doesn't complain of unchecked generic array creation + assertNull(ObjectUtils.firstNonNull(null, null)); + + assertNull(ObjectUtils.firstNonNull((Object) null)); + assertNull(ObjectUtils.firstNonNull((Object[]) null)); + } + + @Test + public void testGetFirstNonNull() { + // first non null + assertEquals("", ObjectUtils.getFirstNonNull(() -> null, () -> "")); + // first encountered value is used + assertEquals("1", ObjectUtils.getFirstNonNull(() -> null, () -> "1", () -> "2", () -> null)); + assertEquals("123", ObjectUtils.getFirstNonNull(() -> "123", () -> null, () -> "456")); + // don't evaluate suppliers after first value is found + assertEquals("123", ObjectUtils.getFirstNonNull(() -> null, () -> "123", () -> fail("Supplier after first non-null value should not be evaluated"))); + // supplier returning null and null supplier both result in null + assertNull(ObjectUtils.getFirstNonNull(null, () -> null)); + // Explicitly pass in an empty array of Object type to ensure compiler doesn't complain of unchecked generic array creation + assertNull(ObjectUtils.getFirstNonNull()); + // supplier is null + assertNull(ObjectUtils.getFirstNonNull((Supplier) null)); + // varargs array itself is null + assertNull(ObjectUtils.getFirstNonNull((Supplier[]) null)); + // test different types + assertEquals(1, ObjectUtils.getFirstNonNull(() -> null, () -> 1)); + assertEquals(Boolean.TRUE, ObjectUtils.getFirstNonNull(() -> null, () -> Boolean.TRUE)); + } + + @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_likeList() { + final List list0 = new ArrayList<>(Collections.emptyList()); + assertEquals(list0.hashCode(), ObjectUtils.hashCodeMulti()); + + final List list1 = new ArrayList<>(Collections.singletonList("a")); + assertEquals(list1.hashCode(), ObjectUtils.hashCodeMulti("a")); + + final List list2 = new ArrayList<>(Arrays.asList("a", "b")); + assertEquals(list2.hashCode(), ObjectUtils.hashCodeMulti("a", "b")); + + final List list3 = new ArrayList<>(Arrays.asList("a", "b", "c")); + assertEquals(list3.hashCode(), ObjectUtils.hashCodeMulti("a", "b", "c")); + } + + @Test + public void testHashCodeMulti_multiple_nullArray() { + final Object[] array = null; + assertEquals(1, ObjectUtils.hashCodeMulti(array)); + } + + @Test + public void testIdentityToStringAppendable() throws IOException { + final Integer i = Integer.valueOf(121); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final Appendable appendable = new StringBuilder(); + ObjectUtils.identityToString(appendable, i); + assertEquals(expected, appendable.toString()); + + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((Appendable) null, "tmp")); + + assertThrows( + NullPointerException.class, + () -> ObjectUtils.identityToString((Appendable) (new StringBuilder()), null)); + } + + @Test + public void testIdentityToStringInteger() { + final Integer i = Integer.valueOf(90); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + assertEquals(expected, ObjectUtils.identityToString(i)); + } + + @Test + public void testIdentityToStringObjectNull() { + assertNull(ObjectUtils.identityToString(null)); + } + + @Test + public void testIdentityToStringStrBuilder() { + final Integer i = Integer.valueOf(102); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final StrBuilder builder = new StrBuilder(); + ObjectUtils.identityToString(builder, i); + assertEquals(expected, builder.toString()); + + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((StrBuilder) null, "tmp")); + + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString(new StrBuilder(), null)); + } + + @Test + public void testIdentityToStringString() { + assertEquals( + "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)), + ObjectUtils.identityToString(FOO)); + } + + @Test + public void testIdentityToStringStringBuffer() { + final Integer i = Integer.valueOf(45); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final StringBuffer buffer = new StringBuffer(); + ObjectUtils.identityToString(buffer, i); + assertEquals(expected, buffer.toString()); + + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((StringBuffer) null, "tmp")); + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString(new StringBuffer(), null)); + } + + @Test + public void testIdentityToStringStringBuilder() { + final Integer i = Integer.valueOf(90); + final String expected = "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final StringBuilder builder = new StringBuilder(); + ObjectUtils.identityToString(builder, i); + assertEquals(expected, builder.toString()); + } + + @Test + public void testIdentityToStringStringBuilderInUse() { + final Integer i = Integer.valueOf(90); + final String expected = "ABC = java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)); + + final StringBuilder builder = new StringBuilder("ABC = "); + ObjectUtils.identityToString(builder, i); + assertEquals(expected, builder.toString()); + } + + @Test + public void testIdentityToStringStringBuilderNullStringBuilder() { + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString((StringBuilder) null, "tmp")); + } + + @Test + public void testIdentityToStringStringBuilderNullValue() { + assertThrows(NullPointerException.class, () -> ObjectUtils.identityToString(new StringBuilder(), null)); + } + + //----------------------------------------------------------------------- + @Test + public void testIsEmpty() { + assertTrue(ObjectUtils.isEmpty(null)); + assertTrue(ObjectUtils.isEmpty("")); + assertTrue(ObjectUtils.isEmpty(new int[] {})); + assertTrue(ObjectUtils.isEmpty(Collections.emptyList())); + assertTrue(ObjectUtils.isEmpty(Collections.emptySet())); + assertTrue(ObjectUtils.isEmpty(Collections.emptyMap())); + + assertFalse(ObjectUtils.isEmpty(" ")); + assertFalse(ObjectUtils.isEmpty("ab")); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_ARRAY)); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_LIST)); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_SET)); + assertFalse(ObjectUtils.isEmpty(NON_EMPTY_MAP)); + } + + @Test + public void testIsNotEmpty() { + assertFalse(ObjectUtils.isNotEmpty(null)); + assertFalse(ObjectUtils.isNotEmpty("")); + assertFalse(ObjectUtils.isNotEmpty(new int[] {})); + assertFalse(ObjectUtils.isNotEmpty(Collections.emptyList())); + assertFalse(ObjectUtils.isNotEmpty(Collections.emptySet())); + assertFalse(ObjectUtils.isNotEmpty(Collections.emptyMap())); + + assertTrue(ObjectUtils.isNotEmpty(" ")); + assertTrue(ObjectUtils.isNotEmpty("ab")); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_ARRAY)); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_LIST)); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_SET)); + assertTrue(ObjectUtils.isNotEmpty(NON_EMPTY_MAP)); + } + + @Test + public void testMax() { + final Calendar calendar = Calendar.getInstance(); + final Date nonNullComparable1 = calendar.getTime(); + final Date nonNullComparable2 = calendar.getTime(); + final String[] nullArray = null; + + calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 ); + final Date minComparable = calendar.getTime(); + + assertNotSame( nonNullComparable1, nonNullComparable2 ); + + assertNull(ObjectUtils.max( (String) null ) ); + assertNull(ObjectUtils.max( nullArray ) ); + assertSame( nonNullComparable1, ObjectUtils.max( null, nonNullComparable1 ) ); + assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, null ) ); + assertSame( nonNullComparable1, ObjectUtils.max( null, 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 ) ); + assertSame( nonNullComparable1, ObjectUtils.max( null, minComparable, null, nonNullComparable1 ) ); + + assertNull( ObjectUtils.max(null, null) ); + } + + @Test + public void testMedian() { + assertEquals("foo", ObjectUtils.median("foo")); + assertEquals("bar", ObjectUtils.median("foo", "bar")); + assertEquals("baz", ObjectUtils.median("foo", "bar", "baz")); + assertEquals("baz", ObjectUtils.median("foo", "bar", "baz", "blah")); + assertEquals("blah", ObjectUtils.median("foo", "bar", "baz", "blah", "wah")); + assertEquals(Integer.valueOf(5), + ObjectUtils.median(Integer.valueOf(1), Integer.valueOf(5), Integer.valueOf(10))); + assertEquals( + Integer.valueOf(7), + ObjectUtils.median(Integer.valueOf(5), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8), + Integer.valueOf(9))); + assertEquals(Integer.valueOf(6), + ObjectUtils.median(Integer.valueOf(5), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8))); + } + + @Test + public void testMedian_emptyItems() { + assertThrows(IllegalArgumentException.class, ObjectUtils::median); + } + + @Test + public void testMedian_nullItems() { + assertThrows(NullPointerException.class, () -> ObjectUtils.median((String[]) null)); + } + + @Test + public void testMin() { + final Calendar calendar = Calendar.getInstance(); + final Date nonNullComparable1 = calendar.getTime(); + final Date nonNullComparable2 = calendar.getTime(); + final String[] nullArray = null; + + calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 ); + final Date minComparable = calendar.getTime(); + + assertNotSame( nonNullComparable1, nonNullComparable2 ); + + assertNull(ObjectUtils.min( (String) null ) ); + assertNull(ObjectUtils.min( nullArray ) ); + assertSame( nonNullComparable1, ObjectUtils.min( null, nonNullComparable1 ) ); + assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, null ) ); + assertSame( nonNullComparable1, ObjectUtils.min( null, 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 ) ); + assertSame( minComparable, ObjectUtils.min( null, nonNullComparable1, null, minComparable ) ); + + assertNull( ObjectUtils.min(null, null) ); + } + + @Test + public void testMode() { + assertNull(ObjectUtils.mode((Object[]) null)); + assertNull(ObjectUtils.mode()); + assertNull(ObjectUtils.mode("foo", "bar", "baz")); + assertNull(ObjectUtils.mode("foo", "bar", "baz", "foo", "bar")); + assertEquals("foo", ObjectUtils.mode("foo", "bar", "baz", "foo")); + assertEquals(Integer.valueOf(9), + ObjectUtils.mode("foo", "bar", "baz", Integer.valueOf(9), Integer.valueOf(10), Integer.valueOf(9))); + } + + @Test + public void testNotEqual() { + assertFalse(ObjectUtils.notEqual(null, null), "ObjectUtils.notEqual(null, null) returned false"); + assertTrue(ObjectUtils.notEqual(FOO, null), "ObjectUtils.notEqual(\"foo\", null) returned true"); + assertTrue(ObjectUtils.notEqual(null, BAR), "ObjectUtils.notEqual(null, \"bar\") returned true"); + assertTrue(ObjectUtils.notEqual(FOO, BAR), "ObjectUtils.notEqual(\"foo\", \"bar\") returned true"); + assertFalse(ObjectUtils.notEqual(FOO, FOO), "ObjectUtils.notEqual(\"foo\", \"foo\") returned false"); + } + + @SuppressWarnings("cast") // 1 OK, because we are checking for code change + @Test + public void testNull() { + assertNotNull(ObjectUtils.NULL); + // 1 Check that NULL really is a Null i.e. the definition has not been changed + assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null); + assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL)); + } + + /** + * Tests {@link ObjectUtils#cloneIfPossible(Object)} with a cloneable object. + */ + @Test + 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. + */ + @Test + public void testPossibleCloneOfNotCloneable() { + final String string = "apache"; + assertSame(string, ObjectUtils.cloneIfPossible(string)); + } + + /** + * Tests {@link ObjectUtils#cloneIfPossible(Object)} with an uncloneable object. + */ + @Test + public void testPossibleCloneOfUncloneable() { + final UncloneableString string = new UncloneableString("apache"); + final CloneFailedException e = assertThrows(CloneFailedException.class, () -> ObjectUtils.cloneIfPossible(string)); + assertEquals(NoSuchMethodException.class, e.getCause().getClass()); + } + + @Test + public void testToString_Object() { + assertEquals("", ObjectUtils.toString(null) ); + assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) ); + } + + @Test + public void testToString_ObjectString() { + assertEquals(BAR, ObjectUtils.toString(null, BAR) ); + assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) ); + } + + @Test + public void testToString_SupplierString() { + assertEquals(null, ObjectUtils.toString(null, (Supplier) null)); + assertEquals(null, ObjectUtils.toString(null, () -> null)); + // Pretend computing BAR is expensive. + assertEquals(BAR, ObjectUtils.toString(null, () -> BAR)); + assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, () -> BAR)); + } + @Test public void testWaitDuration() { assertThrows(IllegalMonitorStateException.class, () -> ObjectUtils.wait(new Object(), Duration.ZERO)); } - /** - * String that is cloneable. - */ - static final class CloneableString extends MutableObject 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 implements Cloneable { - private static final long serialVersionUID = 1L; - UncloneableString(final String s) { - super(s); - } - } - - static final class NonComparableCharSequence implements CharSequence { - final String value; - - /** - * Create a new NonComparableCharSequence instance. - * - * @param value the CharSequence value - */ - NonComparableCharSequence(final String value) { - Validate.notNull(value); - this.value = value; - } - - @Override - public char charAt(final int arg0) { - return value.charAt(arg0); - } - - @Override - public int length() { - return value.length(); - } - - @Override - public CharSequence subSequence(final int arg0, final int arg1) { - return value.subSequence(arg0, arg1); - } - - @Override - public String toString() { - return value; - } - } - - static final class CharSequenceComparator implements Comparator { - - @Override - public int compare(final CharSequence o1, final CharSequence o2) { - return o1.toString().compareTo(o2.toString()); - } - - } - }