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