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.
045public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> {
046  public void testToArray_noArgs() {
047    Object[] array = collection.toArray();
048    expectArrayContentsAnyOrder(createSamplesArray(), array);
049  }
050
051  /**
052   * {@link Collection#toArray(Object[])} says: "Note that {@code toArray(new Object[0])} is
053   * identical in function to {@code toArray()}."
054   *
055   * <p>For maximum effect, the collection under test should be created from an element array of a
056   * type other than {@code Object[]}.
057   */
058  public void testToArray_isPlainObjectArray() {
059    Object[] array = collection.toArray();
060    assertEquals(Object[].class, array.getClass());
061  }
062
063  public void testToArray_emptyArray() {
064    E[] empty = getSubjectGenerator().createArray(0);
065    E[] array = collection.toArray(empty);
066    assertEquals(
067        "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass());
068    assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
069    expectArrayContentsAnyOrder(createSamplesArray(), array);
070  }
071
072  @CollectionFeature.Require(KNOWN_ORDER)
073  public void testToArray_emptyArray_ordered() {
074    E[] empty = getSubjectGenerator().createArray(0);
075    E[] array = collection.toArray(empty);
076    assertEquals(
077        "toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass());
078    assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
079    expectArrayContentsInOrder(getOrderedElements(), array);
080  }
081
082  public void testToArray_emptyArrayOfObject() {
083    Object[] in = new Object[0];
084    Object[] array = collection.toArray(in);
085    assertEquals(
086        "toArray(emptyObject[]) should return an array of type Object",
087        Object[].class,
088        array.getClass());
089    assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length);
090    expectArrayContentsAnyOrder(createSamplesArray(), array);
091  }
092
093  public void testToArray_rightSizedArray() {
094    E[] array = getSubjectGenerator().createArray(getNumElements());
095    assertSame(
096        "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array));
097    expectArrayContentsAnyOrder(createSamplesArray(), array);
098  }
099
100  @CollectionFeature.Require(KNOWN_ORDER)
101  public void testToArray_rightSizedArray_ordered() {
102    E[] array = getSubjectGenerator().createArray(getNumElements());
103    assertSame(
104        "toArray(sameSizeE[]) should return the given array", array, collection.toArray(array));
105    expectArrayContentsInOrder(getOrderedElements(), array);
106  }
107
108  public void testToArray_rightSizedArrayOfObject() {
109    Object[] array = new Object[getNumElements()];
110    assertSame(
111        "toArray(sameSizeObject[]) should return the given array",
112        array,
113        collection.toArray(array));
114    expectArrayContentsAnyOrder(createSamplesArray(), array);
115  }
116
117  @CollectionFeature.Require(KNOWN_ORDER)
118  public void testToArray_rightSizedArrayOfObject_ordered() {
119    Object[] array = new Object[getNumElements()];
120    assertSame(
121        "toArray(sameSizeObject[]) should return the given array",
122        array,
123        collection.toArray(array));
124    expectArrayContentsInOrder(getOrderedElements(), array);
125  }
126
127  public void testToArray_oversizedArray() {
128    E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
129    array[getNumElements()] = e3();
130    array[getNumElements() + 1] = e3();
131    assertSame(
132        "toArray(overSizedE[]) should return the given array", array, collection.toArray(array));
133
134    List<E> subArray = Arrays.asList(array).subList(0, getNumElements());
135    E[] expectedSubArray = createSamplesArray();
136    for (int i = 0; i < getNumElements(); i++) {
137      assertTrue(
138          "toArray(overSizedE[]) should contain element " + expectedSubArray[i],
139          subArray.contains(expectedSubArray[i]));
140    }
141    assertNull(
142        "The array element immediately following the end of the collection should be nulled",
143        array[getNumElements()]);
144    // array[getNumElements() + 1] might or might not have been nulled
145  }
146
147  @CollectionFeature.Require(KNOWN_ORDER)
148  public void testToArray_oversizedArray_ordered() {
149    E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
150    array[getNumElements()] = e3();
151    array[getNumElements() + 1] = e3();
152    assertSame(
153        "toArray(overSizedE[]) should return the given array", array, collection.toArray(array));
154
155    List<E> expected = getOrderedElements();
156    for (int i = 0; i < getNumElements(); i++) {
157      assertEquals(expected.get(i), array[i]);
158    }
159    assertNull(
160        "The array element immediately following the end of the collection should be nulled",
161        array[getNumElements()]);
162    // array[getNumElements() + 1] might or might not have been nulled
163  }
164
165  @CollectionSize.Require(absent = ZERO)
166  public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() {
167    try {
168      WrongType[] array = new WrongType[0];
169      collection.toArray(array);
170      fail("toArray(notAssignableTo[]) should throw");
171    } catch (ArrayStoreException expected) {
172    }
173  }
174
175  @CollectionSize.Require(ZERO)
176  public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() {
177    WrongType[] array = new WrongType[0];
178    assertSame(
179        "toArray(sameSizeNotAssignableTo[]) should return the given array",
180        array,
181        collection.toArray(array));
182  }
183
184  private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) {
185    Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual));
186  }
187
188  private void expectArrayContentsInOrder(List<E> expected, Object[] actual) {
189    assertEquals("toArray() ordered contents: ", expected, Arrays.asList(actual));
190  }
191
192  /**
193   * Returns the {@link Method} instance for {@link #testToArray_isPlainObjectArray()} so that tests
194   * of {@link Arrays#asList(Object[])} can suppress it with {@code
195   * FeatureSpecificTestSuiteBuilder.suppressing()} until <a
196   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6260652">Sun bug 6260652</a> is fixed.
197   */
198  @J2ktIncompatible
199  @GwtIncompatible // reflection
200  public static Method getToArrayIsPlainObjectArrayMethod() {
201    return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray");
202  }
203}