ForwardingCollectionTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
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
17package com.google.common.collect;
18
19import static java.util.Arrays.asList;
20
21import com.google.common.collect.testing.CollectionTestSuiteBuilder;
22import com.google.common.collect.testing.MinimalCollection;
23import com.google.common.collect.testing.TestStringCollectionGenerator;
24import com.google.common.collect.testing.features.CollectionFeature;
25import com.google.common.collect.testing.features.CollectionSize;
26
27import junit.framework.Test;
28import junit.framework.TestSuite;
29
30import java.util.Collection;
31import java.util.Collections;
32
33/**
34 * Tests for {@link ForwardingCollection}.
35 *
36 * @author Robert Konigsberg
37 * @author Hayward Chan
38 * @author Louis Wasserman
39 */
40public class ForwardingCollectionTest extends ForwardingTestCase {
41  static final class StandardImplForwardingCollection<T>
42      extends ForwardingCollection<T> {
43    private final Collection<T> backingCollection;
44
45    StandardImplForwardingCollection(Collection<T> backingCollection) {
46      this.backingCollection = backingCollection;
47    }
48
49    @Override protected Collection<T> delegate() {
50      return backingCollection;
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
94  private static final Collection<String> EMPTY_COLLECTION =
95      Collections.emptyList();
96
97  private Collection<String> forward;
98
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
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
145  public void testAdd_T() {
146    forward.add("asdf");
147    assertEquals("[add(Object)]", getCalls());
148  }
149
150  public void testAddAll_Collection() {
151    forward.addAll(EMPTY_COLLECTION);
152    assertEquals("[addAll(Collection)]", getCalls());
153  }
154
155  public void testClear() {
156    forward.clear();
157    assertEquals("[clear]", getCalls());
158  }
159
160  public void testContains_Object() {
161    forward.contains(null);
162    assertEquals("[contains(Object)]", getCalls());
163  }
164
165  public void testContainsAll_Collection() {
166    forward.containsAll(EMPTY_COLLECTION);
167    assertEquals("[containsAll(Collection)]", getCalls());
168  }
169
170  public void testIsEmpty() {
171    forward.isEmpty();
172    assertEquals("[isEmpty]", getCalls());
173  }
174
175  public void testIterator() {
176    forward.iterator();
177    assertEquals("[iterator]", getCalls());
178  }
179
180  public void testRemove_Object() {
181    forward.remove(null);
182    assertEquals("[remove(Object)]", getCalls());
183  }
184
185  public void testRemoveAll_Collection() {
186    forward.removeAll(EMPTY_COLLECTION);
187    assertEquals("[removeAll(Collection)]", getCalls());
188  }
189
190  public void testRetainAll_Collection() {
191    forward.retainAll(EMPTY_COLLECTION);
192    assertEquals("[retainAll(Collection)]", getCalls());
193  }
194
195  public void testSize() {
196    forward.size();
197    assertEquals("[size]", getCalls());
198  }
199
200  public void testToArray() {
201    forward.toArray();
202    assertEquals("[toArray]", getCalls());
203  }
204
205  public void testToArray_TArray() {
206    forward.toArray(new String[0]);
207    assertEquals("[toArray(Object[])]", getCalls());
208  }
209
210  public void testToString() {
211    forward.toString();
212    assertEquals("[toString]", getCalls());
213  }
214
215  public void testEquals_Object() {
216    forward.equals("asdf");
217    assertFalse("equals() should not be forwarded.", isCalled());
218  }
219
220  public void testHashCode() {
221    forward.hashCode();
222    assertFalse("hashCode() should not be forwarded.", isCalled());
223  }
224}
225