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.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER; 20 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 21 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.annotations.J2ktIncompatible; 25 import com.google.common.collect.testing.AbstractCollectionTester; 26 import com.google.common.collect.testing.Helpers; 27 import com.google.common.collect.testing.WrongType; 28 import com.google.common.collect.testing.features.CollectionFeature; 29 import com.google.common.collect.testing.features.CollectionSize; 30 import java.lang.reflect.Method; 31 import java.util.Arrays; 32 import java.util.Collection; 33 import java.util.List; 34 import org.junit.Ignore; 35 36 /** 37 * A generic JUnit test which tests {@code toArray()} operations on a collection. Can't be invoked 38 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 39 * 40 * @author Kevin Bourrillion 41 * @author Chris Povirk 42 */ 43 @GwtCompatible(emulated = true) 44 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 45 @SuppressWarnings("JUnit4ClassUsedInJUnit3") 46 public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> { testToArray_noArgs()47 public void testToArray_noArgs() { 48 Object[] array = collection.toArray(); 49 expectArrayContentsAnyOrder(createSamplesArray(), array); 50 } 51 52 /** 53 * {@link Collection#toArray(Object[])} says: "Note that {@code toArray(new Object[0])} is 54 * identical in function to {@code toArray()}." 55 * 56 * <p>For maximum effect, the collection under test should be created from an element array of a 57 * type other than {@code Object[]}. 58 */ testToArray_isPlainObjectArray()59 public void testToArray_isPlainObjectArray() { 60 Object[] array = collection.toArray(); 61 assertEquals(Object[].class, array.getClass()); 62 } 63 testToArray_emptyArray()64 public void testToArray_emptyArray() { 65 E[] empty = getSubjectGenerator().createArray(0); 66 E[] array = collection.toArray(empty); 67 assertEquals( 68 "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 69 assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 70 expectArrayContentsAnyOrder(createSamplesArray(), array); 71 } 72 73 @CollectionFeature.Require(KNOWN_ORDER) testToArray_emptyArray_ordered()74 public void testToArray_emptyArray_ordered() { 75 E[] empty = getSubjectGenerator().createArray(0); 76 E[] array = collection.toArray(empty); 77 assertEquals( 78 "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 79 assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 80 expectArrayContentsInOrder(getOrderedElements(), array); 81 } 82 testToArray_emptyArrayOfObject()83 public void testToArray_emptyArrayOfObject() { 84 Object[] in = new Object[0]; 85 Object[] array = collection.toArray(in); 86 assertEquals( 87 "toArray(emptyObject[]) should return an array of type Object", 88 Object[].class, 89 array.getClass()); 90 assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length); 91 expectArrayContentsAnyOrder(createSamplesArray(), array); 92 } 93 testToArray_rightSizedArray()94 public void testToArray_rightSizedArray() { 95 E[] array = getSubjectGenerator().createArray(getNumElements()); 96 assertSame( 97 "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); 98 expectArrayContentsAnyOrder(createSamplesArray(), array); 99 } 100 101 @CollectionFeature.Require(KNOWN_ORDER) testToArray_rightSizedArray_ordered()102 public void testToArray_rightSizedArray_ordered() { 103 E[] array = getSubjectGenerator().createArray(getNumElements()); 104 assertSame( 105 "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); 106 expectArrayContentsInOrder(getOrderedElements(), array); 107 } 108 testToArray_rightSizedArrayOfObject()109 public void testToArray_rightSizedArrayOfObject() { 110 Object[] array = new Object[getNumElements()]; 111 assertSame( 112 "toArray(sameSizeObject[]) should return the given array", 113 array, 114 collection.toArray(array)); 115 expectArrayContentsAnyOrder(createSamplesArray(), array); 116 } 117 118 @CollectionFeature.Require(KNOWN_ORDER) testToArray_rightSizedArrayOfObject_ordered()119 public void testToArray_rightSizedArrayOfObject_ordered() { 120 Object[] array = new Object[getNumElements()]; 121 assertSame( 122 "toArray(sameSizeObject[]) should return the given array", 123 array, 124 collection.toArray(array)); 125 expectArrayContentsInOrder(getOrderedElements(), array); 126 } 127 testToArray_oversizedArray()128 public void testToArray_oversizedArray() { 129 E[] array = getSubjectGenerator().createArray(getNumElements() + 2); 130 array[getNumElements()] = e3(); 131 array[getNumElements() + 1] = e3(); 132 assertSame( 133 "toArray(overSizedE[]) should return the given array", array, collection.toArray(array)); 134 135 List<E> subArray = Arrays.asList(array).subList(0, getNumElements()); 136 E[] expectedSubArray = createSamplesArray(); 137 for (int i = 0; i < getNumElements(); i++) { 138 assertTrue( 139 "toArray(overSizedE[]) should contain element " + expectedSubArray[i], 140 subArray.contains(expectedSubArray[i])); 141 } 142 assertNull( 143 "The array element immediately following the end of the collection should be nulled", 144 array[getNumElements()]); 145 // array[getNumElements() + 1] might or might not have been nulled 146 } 147 148 @CollectionFeature.Require(KNOWN_ORDER) testToArray_oversizedArray_ordered()149 public void testToArray_oversizedArray_ordered() { 150 E[] array = getSubjectGenerator().createArray(getNumElements() + 2); 151 array[getNumElements()] = e3(); 152 array[getNumElements() + 1] = e3(); 153 assertSame( 154 "toArray(overSizedE[]) should return the given array", array, collection.toArray(array)); 155 156 List<E> expected = getOrderedElements(); 157 for (int i = 0; i < getNumElements(); i++) { 158 assertEquals(expected.get(i), array[i]); 159 } 160 assertNull( 161 "The array element immediately following the end of the collection should be nulled", 162 array[getNumElements()]); 163 // array[getNumElements() + 1] might or might not have been nulled 164 } 165 166 @CollectionSize.Require(absent = ZERO) testToArray_emptyArrayOfWrongTypeForNonEmptyCollection()167 public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() { 168 try { 169 WrongType[] array = new WrongType[0]; 170 collection.toArray(array); 171 fail("toArray(notAssignableTo[]) should throw"); 172 } catch (ArrayStoreException expected) { 173 } 174 } 175 176 @CollectionSize.Require(ZERO) testToArray_emptyArrayOfWrongTypeForEmptyCollection()177 public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() { 178 WrongType[] array = new WrongType[0]; 179 assertSame( 180 "toArray(sameSizeNotAssignableTo[]) should return the given array", 181 array, 182 collection.toArray(array)); 183 } 184 expectArrayContentsAnyOrder(Object[] expected, Object[] actual)185 private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) { 186 Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual)); 187 } 188 expectArrayContentsInOrder(List<E> expected, Object[] actual)189 private void expectArrayContentsInOrder(List<E> expected, Object[] actual) { 190 assertEquals("toArray() ordered contents: ", expected, Arrays.asList(actual)); 191 } 192 193 /** 194 * Returns the {@link Method} instance for {@link #testToArray_isPlainObjectArray()} so that tests 195 * of {@link Arrays#asList(Object[])} can suppress it with {@code 196 * FeatureSpecificTestSuiteBuilder.suppressing()} until <a 197 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6260652">Sun bug 6260652</a> is fixed. 198 */ 199 @J2ktIncompatible 200 @GwtIncompatible // reflection getToArrayIsPlainObjectArrayMethod()201 public static Method getToArrayIsPlainObjectArrayMethod() { 202 return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray"); 203 } 204 } 205