Coverage Summary for Class: CollectionToArrayTester (com.google.common.collect.testing.testers)

Class Class, % Method, % Line, %
CollectionToArrayTester 100% (1/1) 100% (17/17) 97.7% (85/87)


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.collect.testing.AbstractCollectionTester; 25 import com.google.common.collect.testing.Helpers; 26 import com.google.common.collect.testing.WrongType; 27 import com.google.common.collect.testing.features.CollectionFeature; 28 import com.google.common.collect.testing.features.CollectionSize; 29 import java.lang.reflect.Method; 30 import java.util.Arrays; 31 import java.util.Collection; 32 import java.util.List; 33 import org.junit.Ignore; 34  35 /** 36  * A generic JUnit test which tests {@code toArray()} operations on a collection. Can't be invoked 37  * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 38  * 39  * @author Kevin Bourrillion 40  * @author Chris Povirk 41  */ 42 @GwtCompatible(emulated = true) 43 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 44 public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> { 45  public void testToArray_noArgs() { 46  Object[] array = collection.toArray(); 47  expectArrayContentsAnyOrder(createSamplesArray(), array); 48  } 49  50  /** 51  * {@link Collection#toArray(Object[])} says: "Note that <tt>toArray(new Object[0])</tt> is 52  * identical in function to <tt>toArray()</tt>." 53  * 54  * <p>For maximum effect, the collection under test should be created from an element array of a 55  * type other than {@code Object[]}. 56  */ 57  public void testToArray_isPlainObjectArray() { 58  Object[] array = collection.toArray(); 59  assertEquals(Object[].class, array.getClass()); 60  } 61  62  public void testToArray_emptyArray() { 63  E[] empty = getSubjectGenerator().createArray(0); 64  E[] array = collection.toArray(empty); 65  assertEquals( 66  "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 67  assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 68  expectArrayContentsAnyOrder(createSamplesArray(), array); 69  } 70  71  @CollectionFeature.Require(KNOWN_ORDER) 72  public void testToArray_emptyArray_ordered() { 73  E[] empty = getSubjectGenerator().createArray(0); 74  E[] array = collection.toArray(empty); 75  assertEquals( 76  "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass()); 77  assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length); 78  expectArrayContentsInOrder(getOrderedElements(), array); 79  } 80  81  public void testToArray_emptyArrayOfObject() { 82  Object[] in = new Object[0]; 83  Object[] array = collection.toArray(in); 84  assertEquals( 85  "toArray(emptyObject[]) should return an array of type Object", 86  Object[].class, 87  array.getClass()); 88  assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length); 89  expectArrayContentsAnyOrder(createSamplesArray(), array); 90  } 91  92  public void testToArray_rightSizedArray() { 93  E[] array = getSubjectGenerator().createArray(getNumElements()); 94  assertSame( 95  "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array)); 96  expectArrayContentsAnyOrder(createSamplesArray(), array); 97  } 98  99  @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 }