guava
267 строк · 9.4 Кб
1/*
2* Copyright (C) 2009 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;
18
19import com.google.common.annotations.GwtIncompatible;
20import com.google.common.collect.testing.features.CollectionFeature;
21import com.google.common.collect.testing.features.CollectionSize;
22import java.lang.reflect.Method;
23import java.util.ArrayDeque;
24import java.util.Collection;
25import java.util.Collections;
26import java.util.LinkedList;
27import java.util.PriorityQueue;
28import java.util.Queue;
29import java.util.concurrent.ArrayBlockingQueue;
30import java.util.concurrent.ConcurrentLinkedDeque;
31import java.util.concurrent.ConcurrentLinkedQueue;
32import java.util.concurrent.LinkedBlockingDeque;
33import java.util.concurrent.LinkedBlockingQueue;
34import java.util.concurrent.PriorityBlockingQueue;
35import junit.framework.Test;
36import junit.framework.TestSuite;
37
38/**
39* Generates a test suite covering the {@link Queue} implementations in the {@link java.util}
40* package. Can be subclassed to specify tests that should be suppressed.
41*
42* @author Jared Levy
43*/
44@GwtIncompatible
45public class TestsForQueuesInJavaUtil {
46public static Test suite() {
47return new TestsForQueuesInJavaUtil().allTests();
48}
49
50public Test allTests() {
51TestSuite suite = new TestSuite();
52suite.addTest(testsForArrayDeque());
53suite.addTest(testsForLinkedList());
54suite.addTest(testsForArrayBlockingQueue());
55suite.addTest(testsForCheckedQueue());
56suite.addTest(testsForConcurrentLinkedDeque());
57suite.addTest(testsForConcurrentLinkedQueue());
58suite.addTest(testsForLinkedBlockingDeque());
59suite.addTest(testsForLinkedBlockingQueue());
60suite.addTest(testsForPriorityBlockingQueue());
61suite.addTest(testsForPriorityQueue());
62return suite;
63}
64
65protected Collection<Method> suppressForCheckedQueue() {
66return Collections.emptySet();
67}
68
69protected Collection<Method> suppressForArrayDeque() {
70return Collections.emptySet();
71}
72
73protected Collection<Method> suppressForLinkedList() {
74return Collections.emptySet();
75}
76
77protected Collection<Method> suppressForArrayBlockingQueue() {
78return Collections.emptySet();
79}
80
81protected Collection<Method> suppressForConcurrentLinkedDeque() {
82return Collections.emptySet();
83}
84
85protected Collection<Method> suppressForConcurrentLinkedQueue() {
86return Collections.emptySet();
87}
88
89protected Collection<Method> suppressForLinkedBlockingDeque() {
90return Collections.emptySet();
91}
92
93protected Collection<Method> suppressForLinkedBlockingQueue() {
94return Collections.emptySet();
95}
96
97protected Collection<Method> suppressForPriorityBlockingQueue() {
98return Collections.emptySet();
99}
100
101protected Collection<Method> suppressForPriorityQueue() {
102return Collections.emptySet();
103}
104
105public Test testsForCheckedQueue() {
106return QueueTestSuiteBuilder.using(
107new TestStringQueueGenerator() {
108@Override
109public Queue<String> create(String[] elements) {
110Queue<String> queue = new LinkedList<>(MinimalCollection.of(elements));
111return Collections.checkedQueue(queue, String.class);
112}
113})
114.named("checkedQueue/LinkedList")
115.withFeatures(
116CollectionFeature.GENERAL_PURPOSE,
117CollectionFeature.ALLOWS_NULL_VALUES,
118CollectionFeature.KNOWN_ORDER,
119CollectionFeature.RESTRICTS_ELEMENTS,
120CollectionSize.ANY)
121// don't skip collection tests since checkedQueue() is not tested by TestsForListsInJavaUtil
122.suppressing(suppressForCheckedQueue())
123.createTestSuite();
124}
125
126public Test testsForArrayDeque() {
127return QueueTestSuiteBuilder.using(
128new TestStringQueueGenerator() {
129@Override
130public Queue<String> create(String[] elements) {
131return new ArrayDeque<>(MinimalCollection.of(elements));
132}
133})
134.named("ArrayDeque")
135.withFeatures(
136CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
137.suppressing(suppressForArrayDeque())
138.createTestSuite();
139}
140
141public Test testsForLinkedList() {
142return QueueTestSuiteBuilder.using(
143new TestStringQueueGenerator() {
144@Override
145public Queue<String> create(String[] elements) {
146return new LinkedList<>(MinimalCollection.of(elements));
147}
148})
149.named("LinkedList")
150.withFeatures(
151CollectionFeature.GENERAL_PURPOSE,
152CollectionFeature.ALLOWS_NULL_VALUES,
153CollectionFeature.KNOWN_ORDER,
154CollectionSize.ANY)
155.skipCollectionTests() // already covered in TestsForListsInJavaUtil
156.suppressing(suppressForLinkedList())
157.createTestSuite();
158}
159
160public Test testsForArrayBlockingQueue() {
161return QueueTestSuiteBuilder.using(
162new TestStringQueueGenerator() {
163@Override
164public Queue<String> create(String[] elements) {
165return new ArrayBlockingQueue<>(100, false, MinimalCollection.of(elements));
166}
167})
168.named("ArrayBlockingQueue")
169.withFeatures(
170CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
171.suppressing(suppressForArrayBlockingQueue())
172.createTestSuite();
173}
174
175public Test testsForConcurrentLinkedDeque() {
176return QueueTestSuiteBuilder.using(
177new TestStringQueueGenerator() {
178@Override
179public Queue<String> create(String[] elements) {
180return new ConcurrentLinkedDeque<>(MinimalCollection.of(elements));
181}
182})
183.named("ConcurrentLinkedDeque")
184.withFeatures(
185CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
186.suppressing(suppressForConcurrentLinkedDeque())
187.createTestSuite();
188}
189
190public Test testsForConcurrentLinkedQueue() {
191return QueueTestSuiteBuilder.using(
192new TestStringQueueGenerator() {
193@Override
194public Queue<String> create(String[] elements) {
195return new ConcurrentLinkedQueue<>(MinimalCollection.of(elements));
196}
197})
198.named("ConcurrentLinkedQueue")
199.withFeatures(
200CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
201.suppressing(suppressForConcurrentLinkedQueue())
202.createTestSuite();
203}
204
205public Test testsForLinkedBlockingDeque() {
206return QueueTestSuiteBuilder.using(
207new TestStringQueueGenerator() {
208@Override
209public Queue<String> create(String[] elements) {
210return new LinkedBlockingDeque<>(MinimalCollection.of(elements));
211}
212})
213.named("LinkedBlockingDeque")
214.withFeatures(
215CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
216.suppressing(suppressForLinkedBlockingDeque())
217.createTestSuite();
218}
219
220public Test testsForLinkedBlockingQueue() {
221return QueueTestSuiteBuilder.using(
222new TestStringQueueGenerator() {
223@Override
224public Queue<String> create(String[] elements) {
225return new LinkedBlockingQueue<>(MinimalCollection.of(elements));
226}
227})
228.named("LinkedBlockingQueue")
229.withFeatures(
230CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
231.suppressing(suppressForLinkedBlockingQueue())
232.createTestSuite();
233}
234
235// Not specifying KNOWN_ORDER for PriorityQueue and PriorityBlockingQueue
236// even though they do have it, because our tests interpret KNOWN_ORDER to
237// also mean that the iterator returns the head element first, which those
238// don't.
239
240public Test testsForPriorityBlockingQueue() {
241return QueueTestSuiteBuilder.using(
242new TestStringQueueGenerator() {
243@Override
244public Queue<String> create(String[] elements) {
245return new PriorityBlockingQueue<>(MinimalCollection.of(elements));
246}
247})
248.named("PriorityBlockingQueue")
249.withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY)
250.suppressing(suppressForPriorityBlockingQueue())
251.createTestSuite();
252}
253
254public Test testsForPriorityQueue() {
255return QueueTestSuiteBuilder.using(
256new TestStringQueueGenerator() {
257@Override
258public Queue<String> create(String[] elements) {
259return new PriorityQueue<>(MinimalCollection.of(elements));
260}
261})
262.named("PriorityQueue")
263.withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY)
264.suppressing(suppressForPriorityQueue())
265.createTestSuite();
266}
267}
268