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