001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.Helpers.assertEqualIgnoringOrder; 020import static com.google.common.collect.testing.Helpers.getMethod; 021import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static java.util.Arrays.asList; 024import static org.junit.Assert.assertThrows; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.annotations.GwtIncompatible; 028import com.google.common.annotations.J2ktIncompatible; 029import com.google.common.collect.testing.AbstractCollectionTester; 030import com.google.common.collect.testing.WrongType; 031import com.google.common.collect.testing.features.CollectionFeature; 032import com.google.common.collect.testing.features.CollectionSize; 033import java.lang.reflect.Method; 034import java.util.Arrays; 035import java.util.Collection; 036import java.util.List; 037 038/** 039 * A generic JUnit test which tests {@code toArray()} operations on a collection. Can't be invoked 040 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 041 * 042 * @author Kevin Bourrillion 043 * @author Chris Povirk 044 */ 045@GwtCompatible 046public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> { 047 public void testToArray_noArgs() { 048 Object[] array = collection.toArray(); 049 expectArrayContentsAnyOrder(createSamplesArray(), array); 050 } 051 052 /** 053 * {@link Collection#toArray(Object[])} says: "Note that {@code toArray(new Object[0])} is 054 * identical in function to {@code toArray()}." 055 * 056 * <p>For maximum effect, the collection under test should be created from an element array of a 057 * type other than {@code Object[]}. 058 */ 059 public void testToArray_isPlainObjectArray() { 060 Object[] array = collection.toArray(); 061 assertEquals(Object[].class, array.getClass()); 062 } 063 064 public void testToArray_emptyArray() { 065 E[] empty = getSubjectGenerator().createArray(0); 066 E[] array = collection.toArray(empty); 067 assertEquals( 068 "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 069 assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 070 expectArrayContentsAnyOrder(createSamplesArray(), array); 071 } 072 073 @CollectionFeature.Require(KNOWN_ORDER) 074 public void testToArray_emptyArray_ordered() { 075 E[] empty = getSubjectGenerator().createArray(0); 076 E[] array = collection.toArray(empty); 077 assertEquals( 078 "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 079 assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 080 expectArrayContentsInOrder(getOrderedElements(), array); 081 } 082 083 public void testToArray_emptyArrayOfObject() { 084 Object[] in = new Object[0]; 085 Object[] array = collection.toArray(in); 086 assertEquals( 087 "toArray(emptyObject[]) should return an array of type Object", 088 Object[].class, 089 array.getClass()); 090 assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length); 091 expectArrayContentsAnyOrder(createSamplesArray(), array); 092 } 093 094 public void testToArray_rightSizedArray() { 095 E[] array = getSubjectGenerator().createArray(getNumElements()); 096 assertSame( 097 "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); 098 expectArrayContentsAnyOrder(createSamplesArray(), array); 099 } 100 101 @CollectionFeature.Require(KNOWN_ORDER) 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 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) 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 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 = 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) 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) 167 public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() { 168 WrongType[] array = new WrongType[0]; 169 assertThrows(ArrayStoreException.class, () -> collection.toArray(array)); 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, 178 collection.toArray(array)); 179 } 180 181 private static void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) { 182 assertEqualIgnoringOrder(asList(expected), asList(actual)); 183 } 184 185 private void expectArrayContentsInOrder(List<E> expected, Object[] actual) { 186 assertEquals("toArray() ordered contents: ", expected, asList(actual)); 187 } 188 189 /** 190 * Returns the {@link Method} instance for {@link #testToArray_isPlainObjectArray()} so that tests 191 * of {@link Arrays#asList(Object[])} can suppress it with {@code 192 * FeatureSpecificTestSuiteBuilder.suppressing()} until <a 193 * href="https://bugs.openjdk.org/browse/JDK-6260652">JDK-6260652</a> is fixed. 194 */ 195 @J2ktIncompatible 196 @GwtIncompatible // reflection 197 public static Method getToArrayIsPlainObjectArrayMethod() { 198 return getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray"); 199 } 200}