/* * Copyright (C) 2007 The Guava Authors * * Licensed 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 com.google.common.collect; import static org.junit.contrib.truth.Truth.ASSERT; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; import java.io.Serializable; import java.util.Arrays; import java.util.List; /** * Unit test for {@code ObjectArrays}. * * @author Kevin Bourrillion */ @GwtCompatible(emulated = true) public class ObjectArraysTest extends TestCase { @GwtIncompatible("NullPointerTester") public void testNullPointerExceptions() throws Exception { NullPointerTester tester = new NullPointerTester(); tester.testAllPublicStaticMethods(ObjectArrays.class); } @GwtIncompatible("ObjectArrays.newArray(Class, int)") public void testNewArray_fromClass_Empty() { String[] empty = ObjectArrays.newArray(String.class, 0); assertEquals(String[].class, empty.getClass()); assertEquals(0, empty.length); } @GwtIncompatible("ObjectArrays.newArray(Class, int)") public void testNewArray_fromClass_Nonempty() { String[] array = ObjectArrays.newArray(String.class, 2); assertEquals(String[].class, array.getClass()); assertEquals(2, array.length); assertNull(array[0]); } @GwtIncompatible("ObjectArrays.newArray(Class, int)") public void testNewArray_fromClass_OfArray() { String[][] array = ObjectArrays.newArray(String[].class, 1); assertEquals(String[][].class, array.getClass()); assertEquals(1, array.length); assertNull(array[0]); } public void testNewArray_fromArray_Empty() { String[] in = new String[0]; String[] empty = ObjectArrays.newArray(in, 0); assertEquals(0, empty.length); } public void testNewArray_fromArray_Nonempty() { String[] array = ObjectArrays.newArray(new String[0], 2); assertEquals(String[].class, array.getClass()); assertEquals(2, array.length); assertNull(array[0]); } public void testNewArray_fromArray_OfArray() { String[][] array = ObjectArrays.newArray(new String[0][0], 1); assertEquals(String[][].class, array.getClass()); assertEquals(1, array.length); assertNull(array[0]); } @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)") public void testConcatEmptyEmpty() { String[] result = ObjectArrays.concat(new String[0], new String[0], String.class); assertEquals(String[].class, result.getClass()); assertEquals(0, result.length); } @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)") public void testConcatEmptyNonempty() { String[] result = ObjectArrays.concat( new String[0], new String[] { "a", "b" }, String.class); assertEquals(String[].class, result.getClass()); ASSERT.that(result).hasContentsInOrder("a", "b"); } @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)") public void testConcatNonemptyEmpty() { String[] result = ObjectArrays.concat( new String[] { "a", "b" }, new String[0], String.class); assertEquals(String[].class, result.getClass()); ASSERT.that(result).hasContentsInOrder("a", "b"); } @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)") public void testConcatBasic() { String[] result = ObjectArrays.concat( new String[] { "a", "b" }, new String[] { "c", "d" }, String.class); assertEquals(String[].class, result.getClass()); ASSERT.that(result).hasContentsInOrder("a", "b", "c", "d"); } @GwtIncompatible("ObjectArrays.concat(Object[], Object[], Class)") public void testConcatWithMoreGeneralType() { Serializable[] result = ObjectArrays.concat(new String[0], new String[0], Serializable.class); assertEquals(Serializable[].class, result.getClass()); } public void testToArrayImpl1() { doTestToArrayImpl1(Lists.<Integer>newArrayList()); doTestToArrayImpl1(Lists.newArrayList(1)); doTestToArrayImpl1(Lists.newArrayList(1, null, 3)); } private void doTestToArrayImpl1(List<Integer> list) { Object[] reference = list.toArray(); Object[] target = ObjectArrays.toArrayImpl(list); assertEquals(reference.getClass(), target.getClass()); assertTrue(Arrays.equals(reference, target)); } public void testToArrayImpl2() { doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[0], false); doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[1], true); doTestToArrayImpl2(Lists.newArrayList(1), new Integer[0], false); doTestToArrayImpl2(Lists.newArrayList(1), new Integer[1], true); doTestToArrayImpl2(Lists.newArrayList(1), new Integer[] { 2, 3 }, true); doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[0], false); doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[2], false); doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[3], true); } private void doTestToArrayImpl2(List<Integer> list, Integer[] array1, boolean expectModify) { Integer[] starting = Platform.clone(array1); Integer[] array2 = Platform.clone(array1); Object[] reference = list.toArray(array1); Object[] target = ObjectArrays.toArrayImpl(list, array2); assertEquals(reference.getClass(), target.getClass()); assertTrue(Arrays.equals(reference, target)); assertTrue(Arrays.equals(reference, target)); Object[] expectedArray1 = expectModify ? reference : starting; Object[] expectedArray2 = expectModify ? target : starting; assertTrue(Arrays.equals(expectedArray1, array1)); assertTrue(Arrays.equals(expectedArray2, array2)); } public void testPrependZeroElements() { String[] result = ObjectArrays.concat("foo", new String[] {}); ASSERT.that(result).hasContentsInOrder("foo"); } public void testPrependOneElement() { String[] result = ObjectArrays.concat("foo", new String[]{ "bar" }); ASSERT.that(result).hasContentsInOrder("foo", "bar"); } public void testPrependTwoElements() { String[] result = ObjectArrays.concat("foo", new String[]{ "bar", "baz" }); ASSERT.that(result).hasContentsInOrder("foo", "bar", "baz"); } public void testAppendZeroElements() { String[] result = ObjectArrays.concat(new String[] {}, "foo"); ASSERT.that(result).hasContentsInOrder("foo"); } public void testAppendOneElement() { String[] result = ObjectArrays.concat(new String[]{ "foo" }, "bar"); ASSERT.that(result).hasContentsInOrder("foo", "bar"); } public void testAppendTwoElements() { String[] result = ObjectArrays.concat(new String[]{ "foo", "bar" }, "baz"); ASSERT.that(result).hasContentsInOrder("foo", "bar", "baz"); } }