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