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