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