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.features.CollectionFeature.KNOWN_ORDER; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.annotations.GwtIncompatible; 024import com.google.common.annotations.J2ktIncompatible; 025import com.google.common.collect.testing.AbstractCollectionTester; 026import com.google.common.collect.testing.Helpers; 027import com.google.common.collect.testing.WrongType; 028import com.google.common.collect.testing.features.CollectionFeature; 029import com.google.common.collect.testing.features.CollectionSize; 030import java.lang.reflect.Method; 031import java.util.Arrays; 032import java.util.Collection; 033import java.util.List; 034import org.junit.Ignore; 035 036/** 037 * A generic JUnit test which tests {@code toArray()} operations on a collection. Can't be invoked 038 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 039 * 040 * @author Kevin Bourrillion 041 * @author Chris Povirk 042 */ 043@GwtCompatible(emulated = true) 044@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 045@SuppressWarnings("JUnit4ClassUsedInJUnit3") 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 = 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) 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 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) 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 185 private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) { 186 Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual)); 187 } 188 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 201 public static Method getToArrayIsPlainObjectArrayMethod() { 202 return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray"); 203 } 204}