guava
203 строки · 7.9 Кб
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.
45public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> {
46public void testToArray_noArgs() {
47Object[] array = collection.toArray();
48expectArrayContentsAnyOrder(createSamplesArray(), array);
49}
50
51/**
52* {@link Collection#toArray(Object[])} says: "Note that {@code toArray(new Object[0])} is
53* identical in function to {@code toArray()}."
54*
55* <p>For maximum effect, the collection under test should be created from an element array of a
56* type other than {@code Object[]}.
57*/
58public void testToArray_isPlainObjectArray() {
59Object[] array = collection.toArray();
60assertEquals(Object[].class, array.getClass());
61}
62
63public void testToArray_emptyArray() {
64E[] empty = getSubjectGenerator().createArray(0);
65E[] array = collection.toArray(empty);
66assertEquals(
67"toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass());
68assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
69expectArrayContentsAnyOrder(createSamplesArray(), array);
70}
71
72@CollectionFeature.Require(KNOWN_ORDER)
73public void testToArray_emptyArray_ordered() {
74E[] empty = getSubjectGenerator().createArray(0);
75E[] array = collection.toArray(empty);
76assertEquals(
77"toArray(emptyT[]) should return an array of type T", empty.getClass(), array.getClass());
78assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
79expectArrayContentsInOrder(getOrderedElements(), array);
80}
81
82public void testToArray_emptyArrayOfObject() {
83Object[] in = new Object[0];
84Object[] array = collection.toArray(in);
85assertEquals(
86"toArray(emptyObject[]) should return an array of type Object",
87Object[].class,
88array.getClass());
89assertEquals("toArray(emptyObject[]).length", getNumElements(), array.length);
90expectArrayContentsAnyOrder(createSamplesArray(), array);
91}
92
93public void testToArray_rightSizedArray() {
94E[] array = getSubjectGenerator().createArray(getNumElements());
95assertSame(
96"toArray(sameSizeE[]) should return the given array", array, collection.toArray(array));
97expectArrayContentsAnyOrder(createSamplesArray(), array);
98}
99
100@CollectionFeature.Require(KNOWN_ORDER)
101public void testToArray_rightSizedArray_ordered() {
102E[] array = getSubjectGenerator().createArray(getNumElements());
103assertSame(
104"toArray(sameSizeE[]) should return the given array", array, collection.toArray(array));
105expectArrayContentsInOrder(getOrderedElements(), array);
106}
107
108public void testToArray_rightSizedArrayOfObject() {
109Object[] array = new Object[getNumElements()];
110assertSame(
111"toArray(sameSizeObject[]) should return the given array",
112array,
113collection.toArray(array));
114expectArrayContentsAnyOrder(createSamplesArray(), array);
115}
116
117@CollectionFeature.Require(KNOWN_ORDER)
118public void testToArray_rightSizedArrayOfObject_ordered() {
119Object[] array = new Object[getNumElements()];
120assertSame(
121"toArray(sameSizeObject[]) should return the given array",
122array,
123collection.toArray(array));
124expectArrayContentsInOrder(getOrderedElements(), array);
125}
126
127public void testToArray_oversizedArray() {
128E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
129array[getNumElements()] = e3();
130array[getNumElements() + 1] = e3();
131assertSame(
132"toArray(overSizedE[]) should return the given array", array, collection.toArray(array));
133
134List<E> subArray = Arrays.asList(array).subList(0, getNumElements());
135E[] expectedSubArray = createSamplesArray();
136for (int i = 0; i < getNumElements(); i++) {
137assertTrue(
138"toArray(overSizedE[]) should contain element " + expectedSubArray[i],
139subArray.contains(expectedSubArray[i]));
140}
141assertNull(
142"The array element immediately following the end of the collection should be nulled",
143array[getNumElements()]);
144// array[getNumElements() + 1] might or might not have been nulled
145}
146
147@CollectionFeature.Require(KNOWN_ORDER)
148public void testToArray_oversizedArray_ordered() {
149E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
150array[getNumElements()] = e3();
151array[getNumElements() + 1] = e3();
152assertSame(
153"toArray(overSizedE[]) should return the given array", array, collection.toArray(array));
154
155List<E> expected = getOrderedElements();
156for (int i = 0; i < getNumElements(); i++) {
157assertEquals(expected.get(i), array[i]);
158}
159assertNull(
160"The array element immediately following the end of the collection should be nulled",
161array[getNumElements()]);
162// array[getNumElements() + 1] might or might not have been nulled
163}
164
165@CollectionSize.Require(absent = ZERO)
166public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() {
167try {
168WrongType[] array = new WrongType[0];
169collection.toArray(array);
170fail("toArray(notAssignableTo[]) should throw");
171} catch (ArrayStoreException expected) {
172}
173}
174
175@CollectionSize.Require(ZERO)
176public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() {
177WrongType[] array = new WrongType[0];
178assertSame(
179"toArray(sameSizeNotAssignableTo[]) should return the given array",
180array,
181collection.toArray(array));
182}
183
184private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) {
185Helpers.assertEqualIgnoringOrder(Arrays.asList(expected), Arrays.asList(actual));
186}
187
188private void expectArrayContentsInOrder(List<E> expected, Object[] actual) {
189assertEquals("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
200public static Method getToArrayIsPlainObjectArrayMethod() {
201return Helpers.getMethod(CollectionToArrayTester.class, "testToArray_isPlainObjectArray");
202}
203}
204