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