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.collect.testing.features.CollectionFeature; 20 import com.google.common.collect.testing.features.CollectionSize; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import java.lang.reflect.Method; 26 import java.util.Collection; 27 import java.util.Collections; 28 import java.util.LinkedList; 29 import java.util.PriorityQueue; 30 import java.util.Queue; 31 import java.util.concurrent.ArrayBlockingQueue; 32 import java.util.concurrent.ConcurrentLinkedQueue; 33 import java.util.concurrent.LinkedBlockingQueue; 34 import java.util.concurrent.PriorityBlockingQueue; 35 36 /** 37 * Generates a test suite covering the {@link Queue} implementations in the 38 * {@link java.util} package. Can be subclassed to specify tests that should 39 * be suppressed. 40 * 41 * @author Jared Levy 42 */ 43 public class TestsForQueuesInJavaUtil { 44 public static Test suite() { 45 return new TestsForQueuesInJavaUtil().allTests(); 46 } 47 48 public Test allTests() { 49 TestSuite suite = new TestSuite(); 50 suite.addTest(testsForLinkedList()); 51 suite.addTest(testsForArrayBlockingQueue()); 52 suite.addTest(testsForConcurrentLinkedQueue()); 53 suite.addTest(testsForLinkedBlockingQueue()); 54 suite.addTest(testsForPriorityBlockingQueue()); 55 suite.addTest(testsForPriorityQueue()); 56 return suite; 57 } 58 59 protected Collection<Method> suppressForLinkedList() { 60 return Collections.emptySet(); 61 } 62 protected Collection<Method> suppressForArrayBlockingQueue() { 63 return Collections.emptySet(); 64 } 65 protected Collection<Method> suppressForConcurrentLinkedQueue() { 66 return Collections.emptySet(); 67 } 68 protected Collection<Method> suppressForLinkedBlockingQueue() { 69 return Collections.emptySet(); 70 } 71 protected Collection<Method> suppressForPriorityBlockingQueue() { 72 return Collections.emptySet(); 73 } 74 protected Collection<Method> suppressForPriorityQueue() { 75 return Collections.emptySet(); 76 } 77 78 public Test testsForLinkedList() { 79 return QueueTestSuiteBuilder 80 .using(new TestStringQueueGenerator() { 81 @Override public Queue<String> create(String[] elements) { 82 return new LinkedList<String>(MinimalCollection.of(elements)); 83 } 84 }) 85 .named("LinkedList") 86 .withFeatures( 87 CollectionFeature.GENERAL_PURPOSE, 88 CollectionFeature.ALLOWS_NULL_VALUES, 89 CollectionFeature.KNOWN_ORDER, 90 CollectionSize.ANY) 91 .skipCollectionTests() // already covered in TestsForListsInJavaUtil 92 .suppressing(suppressForLinkedList()) 93 .createTestSuite(); 94 } 95 96 public Test testsForArrayBlockingQueue() { 97 return QueueTestSuiteBuilder 98 .using(new TestStringQueueGenerator() { 99 @Override public Queue<String> create(String[] elements) { 100 return new ArrayBlockingQueue<String>( 101 100, false, MinimalCollection.of(elements)); 102 } 103 }) 104 .named("ArrayBlockingQueue") 105 .withFeatures( 106 CollectionFeature.GENERAL_PURPOSE, 107 CollectionFeature.KNOWN_ORDER, 108 CollectionSize.ANY) 109 .suppressing(suppressForArrayBlockingQueue()) 110 .createTestSuite(); 111 } 112 113 public Test testsForConcurrentLinkedQueue() { 114 return QueueTestSuiteBuilder 115 .using(new TestStringQueueGenerator() { 116 @Override public Queue<String> create(String[] elements) { 117 return new ConcurrentLinkedQueue<String>( 118 MinimalCollection.of(elements)); 119 } 120 }) 121 .named("ConcurrentLinkedQueue") 122 .withFeatures( 123 CollectionFeature.GENERAL_PURPOSE, 124 CollectionFeature.KNOWN_ORDER, 125 CollectionSize.ANY) 126 .suppressing(suppressForConcurrentLinkedQueue()) 127 .createTestSuite(); 128 } 129 130 public Test testsForLinkedBlockingQueue() { 131 return QueueTestSuiteBuilder 132 .using(new TestStringQueueGenerator() { 133 @Override public Queue<String> create(String[] elements) { 134 return new LinkedBlockingQueue<String>( 135 MinimalCollection.of(elements)); 136 } 137 }) 138 .named("LinkedBlockingQueue") 139 .withFeatures( 140 CollectionFeature.GENERAL_PURPOSE, 141 CollectionFeature.KNOWN_ORDER, 142 CollectionSize.ANY) 143 .suppressing(suppressForLinkedBlockingQueue()) 144 .createTestSuite(); 145 } 146 147 // Not specifying KNOWN_ORDER for PriorityQueue and PriorityBlockingQueue 148 // even though they do have it, because our tests interpret KNOWN_ORDER to 149 // also mean that the iterator returns the head element first, which those 150 // don't. 151 152 public Test testsForPriorityBlockingQueue() { 153 return QueueTestSuiteBuilder 154 .using(new TestStringQueueGenerator() { 155 @Override public Queue<String> create(String[] elements) { 156 return new PriorityBlockingQueue<String>( 157 MinimalCollection.of(elements)); 158 } 159 }) 160 .named("PriorityBlockingQueue") 161 .withFeatures( 162 CollectionFeature.GENERAL_PURPOSE, 163 CollectionSize.ANY) 164 .suppressing(suppressForPriorityBlockingQueue()) 165 .createTestSuite(); 166 } 167 168 public Test testsForPriorityQueue() { 169 return QueueTestSuiteBuilder 170 .using(new TestStringQueueGenerator() { 171 @Override public Queue<String> create(String[] elements) { 172 return new PriorityQueue<String>(MinimalCollection.of(elements)); 173 } 174 }) 175 .named("PriorityQueue") 176 .withFeatures( 177 CollectionFeature.GENERAL_PURPOSE, 178 CollectionSize.ANY) 179 .suppressing(suppressForPriorityQueue()) 180 .createTestSuite(); 181 } 182 } 183