1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.testing.NullPointerTester; 24 import java.io.Serializable; 25 import java.util.Arrays; 26 import java.util.List; 27 import junit.framework.TestCase; 28 29 /** 30 * Unit test for {@code ObjectArrays}. 31 * 32 * @author Kevin Bourrillion 33 */ 34 @GwtCompatible(emulated = true) 35 public class ObjectArraysTest extends TestCase { 36 37 @GwtIncompatible // NullPointerTester testNullPointerExceptions()38 public void testNullPointerExceptions() { 39 NullPointerTester tester = new NullPointerTester(); 40 tester.testAllPublicStaticMethods(ObjectArrays.class); 41 } 42 43 @GwtIncompatible // ObjectArrays.newArray(Class, int) testNewArray_fromClass_Empty()44 public void testNewArray_fromClass_Empty() { 45 String[] empty = ObjectArrays.newArray(String.class, 0); 46 assertEquals(String[].class, empty.getClass()); 47 assertThat(empty).isEmpty(); 48 } 49 50 @GwtIncompatible // ObjectArrays.newArray(Class, int) testNewArray_fromClass_Nonempty()51 public void testNewArray_fromClass_Nonempty() { 52 String[] array = ObjectArrays.newArray(String.class, 2); 53 assertEquals(String[].class, array.getClass()); 54 assertThat(array).hasLength(2); 55 assertNull(array[0]); 56 } 57 58 @GwtIncompatible // ObjectArrays.newArray(Class, int) testNewArray_fromClass_OfArray()59 public void testNewArray_fromClass_OfArray() { 60 String[][] array = ObjectArrays.newArray(String[].class, 1); 61 assertEquals(String[][].class, array.getClass()); 62 assertThat(array).hasLength(1); 63 assertNull(array[0]); 64 } 65 testNewArray_fromArray_Empty()66 public void testNewArray_fromArray_Empty() { 67 String[] in = new String[0]; 68 String[] empty = ObjectArrays.newArray(in, 0); 69 assertThat(empty).isEmpty(); 70 } 71 testNewArray_fromArray_Nonempty()72 public void testNewArray_fromArray_Nonempty() { 73 String[] array = ObjectArrays.newArray(new String[0], 2); 74 assertEquals(String[].class, array.getClass()); 75 assertThat(array).hasLength(2); 76 assertNull(array[0]); 77 } 78 testNewArray_fromArray_OfArray()79 public void testNewArray_fromArray_OfArray() { 80 String[][] array = ObjectArrays.newArray(new String[0][0], 1); 81 assertEquals(String[][].class, array.getClass()); 82 assertThat(array).hasLength(1); 83 assertNull(array[0]); 84 } 85 86 @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) testConcatEmptyEmpty()87 public void testConcatEmptyEmpty() { 88 String[] result = ObjectArrays.concat(new String[0], new String[0], String.class); 89 assertEquals(String[].class, result.getClass()); 90 assertThat(result).isEmpty(); 91 } 92 93 @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) testConcatEmptyNonempty()94 public void testConcatEmptyNonempty() { 95 String[] result = ObjectArrays.concat(new String[0], new String[] {"a", "b"}, String.class); 96 assertEquals(String[].class, result.getClass()); 97 assertThat(result).asList().containsExactly("a", "b").inOrder(); 98 } 99 100 @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) testConcatNonemptyEmpty()101 public void testConcatNonemptyEmpty() { 102 String[] result = ObjectArrays.concat(new String[] {"a", "b"}, new String[0], String.class); 103 assertEquals(String[].class, result.getClass()); 104 assertThat(result).asList().containsExactly("a", "b").inOrder(); 105 } 106 107 @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) testConcatBasic()108 public void testConcatBasic() { 109 String[] result = 110 ObjectArrays.concat(new String[] {"a", "b"}, new String[] {"c", "d"}, String.class); 111 assertEquals(String[].class, result.getClass()); 112 assertThat(result).asList().containsExactly("a", "b", "c", "d").inOrder(); 113 } 114 115 @GwtIncompatible // ObjectArrays.concat(Object[], Object[], Class) testConcatWithMoreGeneralType()116 public void testConcatWithMoreGeneralType() { 117 Serializable[] result = ObjectArrays.concat(new String[0], new String[0], Serializable.class); 118 assertEquals(Serializable[].class, result.getClass()); 119 } 120 testToArrayImpl1()121 public void testToArrayImpl1() { 122 doTestToArrayImpl1(Lists.<Integer>newArrayList()); 123 doTestToArrayImpl1(Lists.newArrayList(1)); 124 doTestToArrayImpl1(Lists.newArrayList(1, null, 3)); 125 } 126 doTestToArrayImpl1(List<Integer> list)127 private void doTestToArrayImpl1(List<Integer> list) { 128 Object[] reference = list.toArray(); 129 Object[] target = ObjectArrays.toArrayImpl(list); 130 assertEquals(reference.getClass(), target.getClass()); 131 assertTrue(Arrays.equals(reference, target)); 132 } 133 testToArrayImpl2()134 public void testToArrayImpl2() { 135 doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[0], false); 136 doTestToArrayImpl2(Lists.<Integer>newArrayList(), new Integer[1], true); 137 138 doTestToArrayImpl2(Lists.newArrayList(1), new Integer[0], false); 139 doTestToArrayImpl2(Lists.newArrayList(1), new Integer[1], true); 140 doTestToArrayImpl2(Lists.newArrayList(1), new Integer[] {2, 3}, true); 141 142 doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[0], false); 143 doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[2], false); 144 doTestToArrayImpl2(Lists.newArrayList(1, null, 3), new Integer[3], true); 145 } 146 doTestToArrayImpl2(List<Integer> list, Integer[] array1, boolean expectModify)147 private void doTestToArrayImpl2(List<Integer> list, Integer[] array1, boolean expectModify) { 148 Integer[] starting = Arrays.copyOf(array1, array1.length); 149 Integer[] array2 = Arrays.copyOf(array1, array1.length); 150 Object[] reference = list.toArray(array1); 151 152 Object[] target = ObjectArrays.toArrayImpl(list, array2); 153 154 assertEquals(reference.getClass(), target.getClass()); 155 assertTrue(Arrays.equals(reference, target)); 156 assertTrue(Arrays.equals(reference, target)); 157 158 Object[] expectedArray1 = expectModify ? reference : starting; 159 Object[] expectedArray2 = expectModify ? target : starting; 160 assertTrue(Arrays.equals(expectedArray1, array1)); 161 assertTrue(Arrays.equals(expectedArray2, array2)); 162 } 163 testPrependZeroElements()164 public void testPrependZeroElements() { 165 String[] result = ObjectArrays.concat("foo", new String[] {}); 166 assertThat(result).asList().contains("foo"); 167 } 168 testPrependOneElement()169 public void testPrependOneElement() { 170 String[] result = ObjectArrays.concat("foo", new String[] {"bar"}); 171 assertThat(result).asList().containsExactly("foo", "bar").inOrder(); 172 } 173 testPrependTwoElements()174 public void testPrependTwoElements() { 175 String[] result = ObjectArrays.concat("foo", new String[] {"bar", "baz"}); 176 assertThat(result).asList().containsExactly("foo", "bar", "baz").inOrder(); 177 } 178 testAppendZeroElements()179 public void testAppendZeroElements() { 180 String[] result = ObjectArrays.concat(new String[] {}, "foo"); 181 assertThat(result).asList().contains("foo"); 182 } 183 testAppendOneElement()184 public void testAppendOneElement() { 185 String[] result = ObjectArrays.concat(new String[] {"foo"}, "bar"); 186 assertThat(result).asList().containsExactly("foo", "bar").inOrder(); 187 } 188 testAppendTwoElements()189 public void testAppendTwoElements() { 190 String[] result = ObjectArrays.concat(new String[] {"foo", "bar"}, "baz"); 191 assertThat(result).asList().containsExactly("foo", "bar", "baz").inOrder(); 192 } 193 testEmptyArrayToEmpty()194 public void testEmptyArrayToEmpty() { 195 doTestNewArrayEquals(new Object[0], 0); 196 } 197 testEmptyArrayToNonEmpty()198 public void testEmptyArrayToNonEmpty() { 199 checkArrayEquals(new Long[5], ObjectArrays.newArray(new Long[0], 5)); 200 } 201 testNonEmptyToShorter()202 public void testNonEmptyToShorter() { 203 checkArrayEquals(new String[9], ObjectArrays.newArray(new String[10], 9)); 204 } 205 testNonEmptyToSameLength()206 public void testNonEmptyToSameLength() { 207 doTestNewArrayEquals(new String[10], 10); 208 } 209 testNonEmptyToLonger()210 public void testNonEmptyToLonger() { 211 checkArrayEquals( 212 new String[10], ObjectArrays.newArray(new String[] {"a", "b", "c", "d", "e"}, 10)); 213 } 214 checkArrayEquals(Object[] expected, Object[] actual)215 private static void checkArrayEquals(Object[] expected, Object[] actual) { 216 assertTrue( 217 "expected(" 218 + expected.getClass() 219 + "): " 220 + Arrays.toString(expected) 221 + " actual(" 222 + actual.getClass() 223 + "): " 224 + Arrays.toString(actual), 225 arrayEquals(expected, actual)); 226 } 227 arrayEquals(Object[] array1, Object[] array2)228 private static boolean arrayEquals(Object[] array1, Object[] array2) { 229 assertSame(array1.getClass(), array2.getClass()); 230 return Arrays.equals(array1, array2); 231 } 232 doTestNewArrayEquals(Object[] expected, int length)233 private static void doTestNewArrayEquals(Object[] expected, int length) { 234 checkArrayEquals(expected, ObjectArrays.newArray(expected, length)); 235 } 236 } 237