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.ConcurrentLinkedQueue; 31 import java.util.concurrent.LinkedBlockingDeque; 32 import java.util.concurrent.LinkedBlockingQueue; 33 import java.util.concurrent.PriorityBlockingQueue; 34 import junit.framework.Test; 35 import junit.framework.TestSuite; 36 37 /** 38 * Generates a test suite covering the {@link Queue} implementations in the {@link java.util} 39 * package. Can be subclassed to specify tests that should be suppressed. 40 * 41 * @author Jared Levy 42 */ 43 @GwtIncompatible 44 public class TestsForQueuesInJavaUtil { suite()45 public static Test suite() { 46 return new TestsForQueuesInJavaUtil().allTests(); 47 } 48 allTests()49 public Test allTests() { 50 TestSuite suite = new TestSuite(); 51 suite.addTest(testsForArrayDeque()); 52 suite.addTest(testsForLinkedList()); 53 suite.addTest(testsForArrayBlockingQueue()); 54 suite.addTest(testsForConcurrentLinkedQueue()); 55 suite.addTest(testsForLinkedBlockingDeque()); 56 suite.addTest(testsForLinkedBlockingQueue()); 57 suite.addTest(testsForPriorityBlockingQueue()); 58 suite.addTest(testsForPriorityQueue()); 59 return suite; 60 } 61 suppressForArrayDeque()62 protected Collection<Method> suppressForArrayDeque() { 63 return Collections.emptySet(); 64 } 65 suppressForLinkedList()66 protected Collection<Method> suppressForLinkedList() { 67 return Collections.emptySet(); 68 } 69 suppressForArrayBlockingQueue()70 protected Collection<Method> suppressForArrayBlockingQueue() { 71 return Collections.emptySet(); 72 } 73 suppressForConcurrentLinkedQueue()74 protected Collection<Method> suppressForConcurrentLinkedQueue() { 75 return Collections.emptySet(); 76 } 77 suppressForLinkedBlockingDeque()78 protected Collection<Method> suppressForLinkedBlockingDeque() { 79 return Collections.emptySet(); 80 } 81 suppressForLinkedBlockingQueue()82 protected Collection<Method> suppressForLinkedBlockingQueue() { 83 return Collections.emptySet(); 84 } 85 suppressForPriorityBlockingQueue()86 protected Collection<Method> suppressForPriorityBlockingQueue() { 87 return Collections.emptySet(); 88 } 89 suppressForPriorityQueue()90 protected Collection<Method> suppressForPriorityQueue() { 91 return Collections.emptySet(); 92 } 93 testsForArrayDeque()94 public Test testsForArrayDeque() { 95 return QueueTestSuiteBuilder.using( 96 new TestStringQueueGenerator() { 97 @Override 98 public Queue<String> create(String[] elements) { 99 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 } 221