1 /* 2 * Copyright (C) 2007 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; 18 19 import static java.util.Arrays.asList; 20 21 import com.google.common.base.Function; 22 import com.google.common.collect.testing.QueueTestSuiteBuilder; 23 import com.google.common.collect.testing.TestStringQueueGenerator; 24 import com.google.common.collect.testing.features.CollectionFeature; 25 import com.google.common.collect.testing.features.CollectionSize; 26 import com.google.common.testing.ForwardingWrapperTester; 27 import java.util.Collection; 28 import java.util.Queue; 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 33 /** 34 * Tests for {@code ForwardingQueue}. 35 * 36 * @author Robert Konigsberg 37 * @author Louis Wasserman 38 */ 39 public class ForwardingQueueTest extends TestCase { 40 41 static final class StandardImplForwardingQueue<T> extends ForwardingQueue<T> { 42 private final Queue<T> backingQueue; 43 StandardImplForwardingQueue(Queue<T> backingQueue)44 StandardImplForwardingQueue(Queue<T> backingQueue) { 45 this.backingQueue = backingQueue; 46 } 47 48 @Override delegate()49 protected Queue<T> delegate() { 50 return backingQueue; 51 } 52 53 @Override addAll(Collection<? extends T> collection)54 public boolean addAll(Collection<? extends T> collection) { 55 return standardAddAll(collection); 56 } 57 58 @Override clear()59 public void clear() { 60 standardClear(); 61 } 62 63 @Override contains(Object object)64 public boolean contains(Object object) { 65 return standardContains(object); 66 } 67 68 @Override containsAll(Collection<?> collection)69 public boolean containsAll(Collection<?> collection) { 70 return standardContainsAll(collection); 71 } 72 73 @Override remove(Object object)74 public boolean remove(Object object) { 75 return standardRemove(object); 76 } 77 78 @Override removeAll(Collection<?> collection)79 public boolean removeAll(Collection<?> collection) { 80 return standardRemoveAll(collection); 81 } 82 83 @Override retainAll(Collection<?> collection)84 public boolean retainAll(Collection<?> collection) { 85 return standardRetainAll(collection); 86 } 87 88 @Override toArray()89 public Object[] toArray() { 90 return standardToArray(); 91 } 92 93 @Override toArray(T[] array)94 public <T> T[] toArray(T[] array) { 95 return standardToArray(array); 96 } 97 98 @Override toString()99 public String toString() { 100 return standardToString(); 101 } 102 103 @Override offer(T o)104 public boolean offer(T o) { 105 return standardOffer(o); 106 } 107 108 @Override peek()109 public T peek() { 110 return standardPeek(); 111 } 112 113 @Override poll()114 public T poll() { 115 return standardPoll(); 116 } 117 } 118 suite()119 public static Test suite() { 120 TestSuite suite = new TestSuite(); 121 122 suite.addTestSuite(ForwardingQueueTest.class); 123 suite.addTest( 124 QueueTestSuiteBuilder.using( 125 new TestStringQueueGenerator() { 126 127 @Override 128 protected Queue<String> create(String[] elements) { 129 return new StandardImplForwardingQueue<>(Lists.newLinkedList(asList(elements))); 130 } 131 }) 132 .named("ForwardingQueue[LinkedList] with standard implementations") 133 .withFeatures( 134 CollectionSize.ANY, 135 CollectionFeature.ALLOWS_NULL_VALUES, 136 CollectionFeature.GENERAL_PURPOSE) 137 .createTestSuite()); 138 139 return suite; 140 } 141 142 @SuppressWarnings({"rawtypes", "unchecked"}) testForwarding()143 public void testForwarding() { 144 new ForwardingWrapperTester() 145 .testForwarding( 146 Queue.class, 147 new Function<Queue, Queue>() { 148 @Override 149 public Queue apply(Queue delegate) { 150 return wrap(delegate); 151 } 152 }); 153 } 154 wrap(final Queue<T> delegate)155 private static <T> Queue<T> wrap(final Queue<T> delegate) { 156 return new ForwardingQueue<T>() { 157 @Override 158 protected Queue<T> delegate() { 159 return delegate; 160 } 161 }; 162 } 163 } 164