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