001/*
002 * Copyright (C) 2009 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;
018
019import com.google.common.annotations.GwtIncompatible;
020import com.google.common.collect.testing.features.CollectionFeature;
021import com.google.common.collect.testing.features.CollectionSize;
022import java.lang.reflect.Method;
023import java.util.ArrayDeque;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.LinkedList;
027import java.util.PriorityQueue;
028import java.util.Queue;
029import java.util.concurrent.ArrayBlockingQueue;
030import java.util.concurrent.ConcurrentLinkedDeque;
031import java.util.concurrent.ConcurrentLinkedQueue;
032import java.util.concurrent.LinkedBlockingDeque;
033import java.util.concurrent.LinkedBlockingQueue;
034import java.util.concurrent.PriorityBlockingQueue;
035import junit.framework.Test;
036import junit.framework.TestSuite;
037
038/**
039 * Generates a test suite covering the {@link Queue} implementations in the {@link java.util}
040 * package. Can be subclassed to specify tests that should be suppressed.
041 *
042 * @author Jared Levy
043 */
044@GwtIncompatible
045public class TestsForQueuesInJavaUtil {
046  public static Test suite() {
047    return new TestsForQueuesInJavaUtil().allTests();
048  }
049
050  public Test allTests() {
051    TestSuite suite = new TestSuite();
052    suite.addTest(testsForArrayDeque());
053    suite.addTest(testsForLinkedList());
054    suite.addTest(testsForArrayBlockingQueue());
055    suite.addTest(testsForCheckedQueue());
056    suite.addTest(testsForConcurrentLinkedDeque());
057    suite.addTest(testsForConcurrentLinkedQueue());
058    suite.addTest(testsForLinkedBlockingDeque());
059    suite.addTest(testsForLinkedBlockingQueue());
060    suite.addTest(testsForPriorityBlockingQueue());
061    suite.addTest(testsForPriorityQueue());
062    return suite;
063  }
064
065  protected Collection<Method> suppressForCheckedQueue() {
066    return Collections.emptySet();
067  }
068
069  protected Collection<Method> suppressForArrayDeque() {
070    return Collections.emptySet();
071  }
072
073  protected Collection<Method> suppressForLinkedList() {
074    return Collections.emptySet();
075  }
076
077  protected Collection<Method> suppressForArrayBlockingQueue() {
078    return Collections.emptySet();
079  }
080
081  protected Collection<Method> suppressForConcurrentLinkedDeque() {
082    return Collections.emptySet();
083  }
084
085  protected Collection<Method> suppressForConcurrentLinkedQueue() {
086    return Collections.emptySet();
087  }
088
089  protected Collection<Method> suppressForLinkedBlockingDeque() {
090    return Collections.emptySet();
091  }
092
093  protected Collection<Method> suppressForLinkedBlockingQueue() {
094    return Collections.emptySet();
095  }
096
097  protected Collection<Method> suppressForPriorityBlockingQueue() {
098    return Collections.emptySet();
099  }
100
101  protected Collection<Method> suppressForPriorityQueue() {
102    return Collections.emptySet();
103  }
104
105  public Test testsForCheckedQueue() {
106    return QueueTestSuiteBuilder.using(
107            new TestStringQueueGenerator() {
108              @Override
109              public Queue<String> create(String[] elements) {
110                Queue<String> queue = new LinkedList<>(MinimalCollection.of(elements));
111                return Collections.checkedQueue(queue, String.class);
112              }
113            })
114        .named("checkedQueue/LinkedList")
115        .withFeatures(
116            CollectionFeature.GENERAL_PURPOSE,
117            CollectionFeature.ALLOWS_NULL_VALUES,
118            CollectionFeature.KNOWN_ORDER,
119            CollectionFeature.RESTRICTS_ELEMENTS,
120            CollectionSize.ANY)
121        // don't skip collection tests since checkedQueue() is not tested by TestsForListsInJavaUtil
122        .suppressing(suppressForCheckedQueue())
123        .createTestSuite();
124  }
125
126  public Test testsForArrayDeque() {
127    return QueueTestSuiteBuilder.using(
128            new TestStringQueueGenerator() {
129              @Override
130              public Queue<String> create(String[] elements) {
131                return new ArrayDeque<>(MinimalCollection.of(elements));
132              }
133            })
134        .named("ArrayDeque")
135        .withFeatures(
136            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
137        .suppressing(suppressForArrayDeque())
138        .createTestSuite();
139  }
140
141  public Test testsForLinkedList() {
142    return QueueTestSuiteBuilder.using(
143            new TestStringQueueGenerator() {
144              @Override
145              public Queue<String> create(String[] elements) {
146                return new LinkedList<>(MinimalCollection.of(elements));
147              }
148            })
149        .named("LinkedList")
150        .withFeatures(
151            CollectionFeature.GENERAL_PURPOSE,
152            CollectionFeature.ALLOWS_NULL_VALUES,
153            CollectionFeature.KNOWN_ORDER,
154            CollectionSize.ANY)
155        .skipCollectionTests() // already covered in TestsForListsInJavaUtil
156        .suppressing(suppressForLinkedList())
157        .createTestSuite();
158  }
159
160  public Test testsForArrayBlockingQueue() {
161    return QueueTestSuiteBuilder.using(
162            new TestStringQueueGenerator() {
163              @Override
164              public Queue<String> create(String[] elements) {
165                return new ArrayBlockingQueue<>(100, false, MinimalCollection.of(elements));
166              }
167            })
168        .named("ArrayBlockingQueue")
169        .withFeatures(
170            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
171        .suppressing(suppressForArrayBlockingQueue())
172        .createTestSuite();
173  }
174
175  public Test testsForConcurrentLinkedDeque() {
176    return QueueTestSuiteBuilder.using(
177            new TestStringQueueGenerator() {
178              @Override
179              public Queue<String> create(String[] elements) {
180                return new ConcurrentLinkedDeque<>(MinimalCollection.of(elements));
181              }
182            })
183        .named("ConcurrentLinkedDeque")
184        .withFeatures(
185            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
186        .suppressing(suppressForConcurrentLinkedDeque())
187        .createTestSuite();
188  }
189
190  public Test testsForConcurrentLinkedQueue() {
191    return QueueTestSuiteBuilder.using(
192            new TestStringQueueGenerator() {
193              @Override
194              public Queue<String> create(String[] elements) {
195                return new ConcurrentLinkedQueue<>(MinimalCollection.of(elements));
196              }
197            })
198        .named("ConcurrentLinkedQueue")
199        .withFeatures(
200            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
201        .suppressing(suppressForConcurrentLinkedQueue())
202        .createTestSuite();
203  }
204
205  public Test testsForLinkedBlockingDeque() {
206    return QueueTestSuiteBuilder.using(
207            new TestStringQueueGenerator() {
208              @Override
209              public Queue<String> create(String[] elements) {
210                return new LinkedBlockingDeque<>(MinimalCollection.of(elements));
211              }
212            })
213        .named("LinkedBlockingDeque")
214        .withFeatures(
215            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
216        .suppressing(suppressForLinkedBlockingDeque())
217        .createTestSuite();
218  }
219
220  public Test testsForLinkedBlockingQueue() {
221    return QueueTestSuiteBuilder.using(
222            new TestStringQueueGenerator() {
223              @Override
224              public Queue<String> create(String[] elements) {
225                return new LinkedBlockingQueue<>(MinimalCollection.of(elements));
226              }
227            })
228        .named("LinkedBlockingQueue")
229        .withFeatures(
230            CollectionFeature.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
240  public Test testsForPriorityBlockingQueue() {
241    return QueueTestSuiteBuilder.using(
242            new TestStringQueueGenerator() {
243              @Override
244              public Queue<String> create(String[] elements) {
245                return new PriorityBlockingQueue<>(MinimalCollection.of(elements));
246              }
247            })
248        .named("PriorityBlockingQueue")
249        .withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY)
250        .suppressing(suppressForPriorityBlockingQueue())
251        .createTestSuite();
252  }
253
254  public Test testsForPriorityQueue() {
255    return QueueTestSuiteBuilder.using(
256            new TestStringQueueGenerator() {
257              @Override
258              public Queue<String> create(String[] elements) {
259                return new PriorityQueue<>(MinimalCollection.of(elements));
260              }
261            })
262        .named("PriorityQueue")
263        .withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY)
264        .suppressing(suppressForPriorityQueue())
265        .createTestSuite();
266  }
267}