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.collect.testing.AbstractCollectionTester; 025import com.google.common.collect.testing.Helpers; 026import com.google.common.collect.testing.WrongType; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import java.lang.reflect.Method; 030import java.util.Arrays; 031import java.util.Collection; 032import java.util.List; 033import org.junit.Ignore; 034 035/** 036 * A generic JUnit test which tests {@code toArray()} operations on a collection. Can't be invoked 037 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 038 * 039 * @author Kevin Bourrillion 040 * @author Chris Povirk 041 */ 042@GwtCompatible(emulated = true) 043@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 044public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> { 045 public void testToArray_noArgs() { 046 Object[] array = collection.toArray(); 047 expectArrayContentsAnyOrder(createSamplesArray(), array); 048 } 049 050 /** 051 * {@link Collection#toArray(Object[])} says: "Note that {@code toArray(new Object[0])} is 052 * identical in function to {@code toArray()}." 053 * 054 * <p>For maximum effect, the collection under test should be created from an element array of a 055 * type other than {@code Object[]}. 056 */ 057 public void testToArray_isPlainObjectArray() { 058 Object[] array = collection.toArray(); 059 assertEquals(Object[].class, array.getClass()); 060 } 061 062 public void testToArray_emptyArray() { 063 E[] empty = getSubjectGenerator().createArray(0); 064 E[] array = collection.toArray(empty); 065 assertEquals( 066 "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 067 assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 068 expectArrayContentsAnyOrder(createSamplesArray(), array); 069 } 070 071 @CollectionFeature.Require(KNOWN_ORDER) 072 public void testToArray_emptyArray_ordered() { 073 E[] empty = getSubjectGenerator().createArray(0); 074 E[] array = collection.toArray(empty); 075 assertEquals( 076 "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 077 assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 078 expectArrayContentsInOrder(getOrderedElements(), array); 079 } 080 081 public void testToArray_emptyArrayOfObject() { 082 Object[] in = new Object[0]; 083 Object[] array = collection.toArray(in); 084 assertEquals( 085 "toArray(emptyObject[]) should return an array of type Object", 086 Object[].class, 087 array.getClass()); 088 assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length); 089 expectArrayContentsAnyOrder(createSamplesArray(), array); 090 } 091 092 public void testToArray_rightSizedArray() { 093 E[] array = getSubjectGenerator().createArray(getNumElements()); 094 assertSame( 095 "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); 096 expectArrayContentsAnyOrder(createSamplesArray(), array); 097 } 098 099 @CollectionFeature.Require(KNOWN_ORDER) 100 public void testToArray_rightSizedArray_ordered() { 101 E[] array = getSubjectGenerator().createArray(getNumElements()); 102 assertSame( 103 "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); 104 expectArrayContentsInOrder(getOrderedElements(), array); 105 } 106 107 public void testToArray_rightSizedArrayOfObject() { 108 Object[] array = new Object[getNumElements()]; 109 assertSame( 110 "toArray(sameSizeObject[]) should return the given array", 111 array, 112 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( 120 "toArray(sameSizeObject[]) should return the given array", 121 array, 122 collection.toArray(array)); 123 expectArrayContentsInOrder(getOrderedElements(), array); 124 } 125 126 public void testToArray_oversizedArray() { 127 E[] array = getSubjectGenerator().createArray(getNumElements() + 2); 128 array[getNumElements()] = e3(); 129 array[getNumElements() + 1] = e3(); 130 assertSame( 131 "toArray(overSizedE[]) should return the given array", array, collection.toArray(array)); 132 133 List<E> subArray = Arrays.asList(array).subList(0, getNumElements()); 134 E[] expectedSubArray = createSamplesArray(); 135 for (int i = 0; i < getNumElements(); i++) { 136 assertTrue( 137 "toArray(overSizedE[]) should contain element " + expectedSubArray[i], 138 subArray.contains(expectedSubArray[i])); 139 } 140 assertNull( 141 "The array element immediately following the end of the collection should be nulled", 142 array[getNumElements()]); 143 // array[getNumElements() + 1] might or might not have been nulled 144 } 145 146 @CollectionFeature.Require(KNOWN_ORDER) 147 public void testToArray_oversizedArray_ordered() { 148 E[] array = getSubjectGenerator().createArray(getNumElements() + 2); 149 array[getNumElements()] = e3(); 150 array[getNumElements() + 1] = e3(); 151 assertSame( 152 "toArray(overSizedE[]) should return the given array", array, collection.toArray(array)); 153 154 List<E> expected = getOrderedElements(); 155 for (int i = 0; i < getNumElements(); i++) { 156 assertEquals(expected.get(i), array[i]); 157 } 158 assertNull( 159 "The array element immediately following the end of the collection should be nulled", 160 array[getNumElements()]); 161 // array[getNumElements() + 1] might or might not have been nulled 162 } 163 164 @CollectionSize.Require(absent = ZERO) 165 public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() { 166 try { 167 WrongType[] array = new WrongType[0]; 168 collection.toArray(array); 169 fail("toArray(notAssignableTo[]) should throw"); 170 } catch (ArrayStoreException expected) { 171 } 172 } 173 174 @CollectionSize.Require(ZERO) 175 public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() { 176 WrongType[] array = new WrongType[0]; 177 assertSame( 178 "toArray(sameSizeNotAssignableTo[]) should return the given array", 179 array, 180 collection.toArray(array)); 181 } 182 183 private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) { 184 Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual)); 185 } 186 187 private void expectArrayContentsInOrder(List<E> expected, Object[] actual) { 188 assertEquals("toArray() ordered contents: ", expected, Arrays.asList(actual)); 189 } 190 191 /** 192 * Returns the {@link Method} instance for {@link #testToArray_isPlainObjectArray()} so that tests 193 * of {@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 6260652</a> is fixed. 196 */ 197 @GwtIncompatible // reflection 198 public static Method getToArrayIsPlainObjectArrayMethod() { 199 return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray"); 200 } 201}