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