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 // We are testing LinkedList / testing our tests on LinkedList. 115 @SuppressWarnings("JdkObsolete") 116 public Queue<String> create(String[] elements) { 117 return new LinkedList<>(MinimalCollection.of(elements)); 118 } 119 }) 120 .named("LinkedList") 121 .withFeatures( 122 CollectionFeature.GENERAL_PURPOSE, 123 CollectionFeature.ALLOWS_NULL_VALUES, 124 CollectionFeature.KNOWN_ORDER, 125 CollectionSize.ANY) 126 .skipCollectionTests() // already covered in TestsForListsInJavaUtil 127 .suppressing(suppressForLinkedList()) 128 .createTestSuite(); 129 } 130 131 public Test testsForArrayBlockingQueue() { 132 return QueueTestSuiteBuilder.using( 133 new TestStringQueueGenerator() { 134 @Override 135 public Queue<String> create(String[] elements) { 136 return new ArrayBlockingQueue<>(100, false, MinimalCollection.of(elements)); 137 } 138 }) 139 .named("ArrayBlockingQueue") 140 .withFeatures( 141 CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) 142 .suppressing(suppressForArrayBlockingQueue()) 143 .createTestSuite(); 144 } 145 146 public Test testsForConcurrentLinkedQueue() { 147 return QueueTestSuiteBuilder.using( 148 new TestStringQueueGenerator() { 149 @Override 150 public Queue<String> create(String[] elements) { 151 return new ConcurrentLinkedQueue<>(MinimalCollection.of(elements)); 152 } 153 }) 154 .named("ConcurrentLinkedQueue") 155 .withFeatures( 156 CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) 157 .suppressing(suppressForConcurrentLinkedQueue()) 158 .createTestSuite(); 159 } 160 161 public Test testsForLinkedBlockingDeque() { 162 return QueueTestSuiteBuilder.using( 163 new TestStringQueueGenerator() { 164 @Override 165 public Queue<String> create(String[] elements) { 166 return new LinkedBlockingDeque<>(MinimalCollection.of(elements)); 167 } 168 }) 169 .named("LinkedBlockingDeque") 170 .withFeatures( 171 CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) 172 .suppressing(suppressForLinkedBlockingDeque()) 173 .createTestSuite(); 174 } 175 176 public Test testsForLinkedBlockingQueue() { 177 return QueueTestSuiteBuilder.using( 178 new TestStringQueueGenerator() { 179 @Override 180 public Queue<String> create(String[] elements) { 181 return new LinkedBlockingQueue<>(MinimalCollection.of(elements)); 182 } 183 }) 184 .named("LinkedBlockingQueue") 185 .withFeatures( 186 CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) 187 .suppressing(suppressForLinkedBlockingQueue()) 188 .createTestSuite(); 189 } 190 191 // Not specifying KNOWN_ORDER for PriorityQueue and PriorityBlockingQueue 192 // even though they do have it, because our tests interpret KNOWN_ORDER to 193 // also mean that the iterator returns the head element first, which those 194 // don't. 195 196 public Test testsForPriorityBlockingQueue() { 197 return QueueTestSuiteBuilder.using( 198 new TestStringQueueGenerator() { 199 @Override 200 public Queue<String> create(String[] elements) { 201 return new PriorityBlockingQueue<>(MinimalCollection.of(elements)); 202 } 203 }) 204 .named("PriorityBlockingQueue") 205 .withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY) 206 .suppressing(suppressForPriorityBlockingQueue()) 207 .createTestSuite(); 208 } 209 210 public Test testsForPriorityQueue() { 211 return QueueTestSuiteBuilder.using( 212 new TestStringQueueGenerator() { 213 @Override 214 public Queue<String> create(String[] elements) { 215 return new PriorityQueue<>(MinimalCollection.of(elements)); 216 } 217 }) 218 .named("PriorityQueue") 219 .withFeatures(CollectionFeature.GENERAL_PURPOSE, CollectionSize.ANY) 220 .suppressing(suppressForPriorityQueue()) 221 .createTestSuite(); 222 } 223}