Home | History | Annotate | Download | only in collect
      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.collect.testing.QueueTestSuiteBuilder;
     22 import com.google.common.collect.testing.TestStringQueueGenerator;
     23 import com.google.common.collect.testing.features.CollectionFeature;
     24 import com.google.common.collect.testing.features.CollectionSize;
     25 
     26 import junit.framework.Test;
     27 import junit.framework.TestSuite;
     28 
     29 import java.util.Collection;
     30 import java.util.Collections;
     31 import java.util.Queue;
     32 
     33 /**
     34  * Tests for {@code ForwardingQueue}.
     35  *
     36  * @author Robert Konigsberg
     37  * @author Louis Wasserman
     38  */
     39 public class ForwardingQueueTest extends ForwardingTestCase {
     40 
     41   static final class StandardImplForwardingQueue<T>
     42       extends ForwardingQueue<T> {
     43     private final Queue<T> backingQueue;
     44 
     45     StandardImplForwardingQueue(Queue<T> backingQueue) {
     46       this.backingQueue = backingQueue;
     47     }
     48 
     49     @Override protected Queue<T> delegate() {
     50       return backingQueue;
     51     }
     52 
     53     @Override public boolean addAll(Collection<? extends T> collection) {
     54       return standardAddAll(collection);
     55     }
     56 
     57     @Override public void clear() {
     58       standardClear();
     59     }
     60 
     61     @Override public boolean contains(Object object) {
     62       return standardContains(object);
     63     }
     64 
     65     @Override public boolean containsAll(Collection<?> collection) {
     66       return standardContainsAll(collection);
     67     }
     68 
     69     @Override public boolean remove(Object object) {
     70       return standardRemove(object);
     71     }
     72 
     73     @Override public boolean removeAll(Collection<?> collection) {
     74       return standardRemoveAll(collection);
     75     }
     76 
     77     @Override public boolean retainAll(Collection<?> collection) {
     78       return standardRetainAll(collection);
     79     }
     80 
     81     @Override public Object[] toArray() {
     82       return standardToArray();
     83     }
     84 
     85     @Override public <T> T[] toArray(T[] array) {
     86       return standardToArray(array);
     87     }
     88 
     89     @Override public String toString() {
     90       return standardToString();
     91     }
     92 
     93     @Override public boolean offer(T o) {
     94       return standardOffer(o);
     95     }
     96 
     97     @Override public T peek() {
     98       return standardPeek();
     99     }
    100 
    101     @Override public T poll() {
    102       return standardPoll();
    103     }
    104   }
    105 
    106   private Queue<String> forward;
    107   private Queue<String> queue;
    108 
    109   public static Test suite() {
    110     TestSuite suite = new TestSuite();
    111 
    112     suite.addTestSuite(ForwardingQueueTest.class);
    113     suite.addTest(
    114         QueueTestSuiteBuilder.using(new TestStringQueueGenerator() {
    115 
    116           @Override protected Queue<String> create(String[] elements) {
    117             return new StandardImplForwardingQueue<String>(
    118                 Lists.newLinkedList(asList(elements)));
    119           }
    120         }).named(
    121             "ForwardingQueue[LinkedList] with standard implementations")
    122             .withFeatures(CollectionSize.ANY,
    123                 CollectionFeature.ALLOWS_NULL_VALUES,
    124                 CollectionFeature.GENERAL_PURPOSE).createTestSuite());
    125 
    126     return suite;
    127   }
    128 
    129   /*
    130    * Class parameters must be raw, so we can't create a proxy with generic
    131    * type arguments. The created proxy only records calls and returns null, so
    132    * the type is irrelevant at runtime.
    133    */
    134   @SuppressWarnings("unchecked")
    135   @Override protected void setUp() throws Exception {
    136     super.setUp();
    137     queue = createProxyInstance(Queue.class);
    138     forward = new ForwardingQueue<String>() {
    139       @Override protected Queue<String> delegate() {
    140         return queue;
    141       }
    142     };
    143   }
    144 
    145   public void testAdd_T() {
    146     forward.add("asdf");
    147     assertEquals("[add(Object)]", getCalls());
    148   }
    149 
    150   public void testAddAll_Collection() {
    151     forward.addAll(Collections.singleton("asdf"));
    152     assertEquals("[addAll(Collection)]", getCalls());
    153   }
    154 
    155   public void testClear() {
    156     forward.clear();
    157     assertEquals("[clear]", getCalls());
    158   }
    159 
    160   public void testContains_T() {
    161     forward.contains("asdf");
    162     assertEquals("[contains(Object)]", getCalls());
    163   }
    164 
    165   public void testContainsAll_Collection() {
    166     forward.containsAll(Collections.singleton("asdf"));
    167     assertEquals("[containsAll(Collection)]", getCalls());
    168   }
    169 
    170   public void testElement() {
    171     forward.element();
    172     assertEquals("[element]", getCalls());
    173   }
    174 
    175   public void testIterator() {
    176     forward.iterator();
    177     assertEquals("[iterator]", getCalls());
    178   }
    179 
    180   public void testIsEmpty() {
    181     forward.isEmpty();
    182     assertEquals("[isEmpty]", getCalls());
    183   }
    184 
    185   public void testOffer_T() {
    186     forward.offer("asdf");
    187     assertEquals("[offer(Object)]", getCalls());
    188   }
    189 
    190   public void testPeek() {
    191     forward.peek();
    192     assertEquals("[peek]", getCalls());
    193   }
    194 
    195   public void testPoll() {
    196     forward.poll();
    197     assertEquals("[poll]", getCalls());
    198   }
    199 
    200   public void testRemove() {
    201     forward.remove();
    202     assertEquals("[remove]", getCalls());
    203   }
    204 
    205   public void testRemove_Object() {
    206     forward.remove(Object.class);
    207     assertEquals("[remove(Object)]", getCalls());
    208   }
    209 
    210   public void testRemoveAll_Collection() {
    211     forward.removeAll(Collections.singleton("asdf"));
    212     assertEquals("[removeAll(Collection)]", getCalls());
    213   }
    214 
    215   public void testRetainAll_Collection() {
    216     forward.retainAll(Collections.singleton("asdf"));
    217     assertEquals("[retainAll(Collection)]", getCalls());
    218   }
    219 
    220   public void testSize() {
    221     forward.size();
    222     assertEquals("[size]", getCalls());
    223   }
    224 
    225   public void testToArray() {
    226     forward.toArray();
    227     assertEquals("[toArray]", getCalls());
    228   }
    229 
    230   public void testToArray_TArray() {
    231     forward.toArray(new String[0]);
    232     assertEquals("[toArray(Object[])]", getCalls());
    233   }
    234 
    235   public void testToString() {
    236     forward.toString();
    237     assertEquals("[toString]", getCalls());
    238   }
    239 }
    240