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 static java.util.Collections.emptySet;
020
021import com.google.common.annotations.GwtIncompatible;
022import com.google.common.collect.testing.features.CollectionFeature;
023import com.google.common.collect.testing.features.CollectionSize;
024import java.lang.reflect.Method;
025import java.util.ArrayDeque;
026import java.util.Collection;
027import java.util.LinkedList;
028import java.util.PriorityQueue;
029import java.util.Queue;
030import java.util.concurrent.ArrayBlockingQueue;
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(testsForConcurrentLinkedQueue());
056    suite.addTest(testsForLinkedBlockingDeque());
057    suite.addTest(testsForLinkedBlockingQueue());
058    suite.addTest(testsForPriorityBlockingQueue());
059    suite.addTest(testsForPriorityQueue());
060    return suite;
061  }
062
063  protected Collection<Method> suppressForArrayDeque() {
064    return emptySet();
065  }
066
067  protected Collection<Method> suppressForLinkedList() {
068    return emptySet();
069  }
070
071  protected Collection<Method> suppressForArrayBlockingQueue() {
072    return emptySet();
073  }
074
075  protected Collection<Method> suppressForConcurrentLinkedQueue() {
076    return emptySet();
077  }
078
079  protected Collection<Method> suppressForLinkedBlockingDeque() {
080    return emptySet();
081  }
082
083  protected Collection<Method> suppressForLinkedBlockingQueue() {
084    return emptySet();
085  }
086
087  protected Collection<Method> suppressForPriorityBlockingQueue() {
088    return emptySet();
089  }
090
091  protected Collection<Method> suppressForPriorityQueue() {
092    return emptySet();
093  }
094
095  public Test testsForArrayDeque() {
096    return QueueTestSuiteBuilder.using(
097            new TestStringQueueGenerator() {
098              @Override
099              public Queue<String> create(String[] elements) {
100                return new ArrayDeque<>(MinimalCollection.of(elements));
101              }
102            })
103        .named("ArrayDeque")
104        .withFeatures(
105            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
106        .suppressing(suppressForArrayDeque())
107        .createTestSuite();
108  }
109
110  public Test testsForLinkedList() {
111    return QueueTestSuiteBuilder.using(
112            new TestStringQueueGenerator() {
113              @Override
114              public Queue<String> create(String[] elements) {
115                return new LinkedList<>(MinimalCollection.of(elements));
116              }
117            })
118        .named("LinkedList")
119        .withFeatures(
120            CollectionFeature.GENERAL_PURPOSE,
121            CollectionFeature.ALLOWS_NULL_VALUES,
122            CollectionFeature.KNOWN_ORDER,
123            CollectionSize.ANY)
124        .skipCollectionTests() // already covered in TestsForListsInJavaUtil
125        .suppressing(suppressForLinkedList())
126        .createTestSuite();
127  }
128
129  public Test testsForArrayBlockingQueue() {
130    return QueueTestSuiteBuilder.using(
131            new TestStringQueueGenerator() {
132              @Override
133              public Queue<String> create(String[] elements) {
134                return new ArrayBlockingQueue<>(100, false, MinimalCollection.of(elements));
135              }
136            })
137        .named("ArrayBlockingQueue")
138        .withFeatures(
139            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
140        .suppressing(suppressForArrayBlockingQueue())
141        .createTestSuite();
142  }
143
144  public Test testsForConcurrentLinkedQueue() {
145    return QueueTestSuiteBuilder.using(
146            new TestStringQueueGenerator() {
147              @Override
148              public Queue<String> create(String[] elements) {
149                return new ConcurrentLinkedQueue<>(MinimalCollection.of(elements));
150              }
151            })
152        .named("ConcurrentLinkedQueue")
153        .withFeatures(
154            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
155        .suppressing(suppressForConcurrentLinkedQueue())
156        .createTestSuite();
157  }
158
159  public Test testsForLinkedBlockingDeque() {
160    return QueueTestSuiteBuilder.using(
161            new TestStringQueueGenerator() {
162              @Override
163              public Queue<String> create(String[] elements) {
164                return new LinkedBlockingDeque<>(MinimalCollection.of(elements));
165              }
166            })
167        .named("LinkedBlockingDeque")
168        .withFeatures(
169            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
170        .suppressing(suppressForLinkedBlockingDeque())
171        .createTestSuite();
172  }
173
174  public Test testsForLinkedBlockingQueue() {
175    return QueueTestSuiteBuilder.using(
176            new TestStringQueueGenerator() {
177              @Override
178              public Queue<String> create(String[] elements) {
179                return new LinkedBlockingQueue<>(MinimalCollection.of(elements));
180              }
181            })
182        .named("LinkedBlockingQueue")
183        .withFeatures(
184            CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY)
185        .suppressing(suppressForLinkedBlockingQueue())
186        .createTestSuite();
187  }
188
189  // Not specifying KNOWN_ORDER for PriorityQueue and PriorityBlockingQueue
190  // even though they do have it, because our tests interpret KNOWN_ORDER to
191  // also mean that the iterator returns the head element first, which those
192  // don't.
193
194  public Test testsForPriorityBlockingQueue() {
195    return QueueTestSuiteBuilder.using(
196            new TestStringQueueGenerator() {
197              @Override
198              public Queue<String> create(String[] elements) {
199                return new PriorityBlockingQueue<>(MinimalCollection.of(elements));
200              }
201            })
202        .named("PriorityBlockingQueue")
203        .withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY)
204        .suppressing(suppressForPriorityBlockingQueue())
205        .createTestSuite();
206  }
207
208  public Test testsForPriorityQueue() {
209    return QueueTestSuiteBuilder.using(
210            new TestStringQueueGenerator() {
211              @Override
212              public Queue<String> create(String[] elements) {
213                return new PriorityQueue<>(MinimalCollection.of(elements));
214              }
215            })
216        .named("PriorityQueue")
217        .withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY)
218        .suppressing(suppressForPriorityQueue())
219        .createTestSuite();
220  }
221}