• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.MinimalSet;
23 import com.google.common.collect.testing.SetTestSuiteBuilder;
24 import com.google.common.collect.testing.TestStringSetGenerator;
25 import com.google.common.collect.testing.features.CollectionFeature;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.testing.EqualsTester;
28 import com.google.common.testing.ForwardingWrapperTester;
29 import java.util.Collection;
30 import java.util.Set;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 
35 /**
36  * Tests for {@code ForwardingSet}.
37  *
38  * @author Robert Konigsberg
39  * @author Louis Wasserman
40  */
41 public class ForwardingSetTest extends TestCase {
42   static class StandardImplForwardingSet<T> extends ForwardingSet<T> {
43     private final Set<T> backingSet;
44 
StandardImplForwardingSet(Set<T> backingSet)45     StandardImplForwardingSet(Set<T> backingSet) {
46       this.backingSet = backingSet;
47     }
48 
49     @Override
delegate()50     protected Set<T> delegate() {
51       return backingSet;
52     }
53 
54     @Override
equals(Object object)55     public boolean equals(Object object) {
56       return standardEquals(object);
57     }
58 
59     @Override
hashCode()60     public int hashCode() {
61       return standardHashCode();
62     }
63 
64     @Override
addAll(Collection<? extends T> collection)65     public boolean addAll(Collection<? extends T> collection) {
66       return standardAddAll(collection);
67     }
68 
69     @Override
clear()70     public void clear() {
71       standardClear();
72     }
73 
74     @Override
contains(Object object)75     public boolean contains(Object object) {
76       return standardContains(object);
77     }
78 
79     @Override
containsAll(Collection<?> collection)80     public boolean containsAll(Collection<?> collection) {
81       return standardContainsAll(collection);
82     }
83 
84     @Override
remove(Object object)85     public boolean remove(Object object) {
86       return standardRemove(object);
87     }
88 
89     @Override
removeAll(Collection<?> collection)90     public boolean removeAll(Collection<?> collection) {
91       return standardRemoveAll(collection);
92     }
93 
94     @Override
retainAll(Collection<?> collection)95     public boolean retainAll(Collection<?> collection) {
96       return standardRetainAll(collection);
97     }
98 
99     @Override
toArray()100     public Object[] toArray() {
101       return standardToArray();
102     }
103 
104     @Override
toArray(T[] array)105     public <T> T[] toArray(T[] array) {
106       return standardToArray(array);
107     }
108 
109     @Override
toString()110     public String toString() {
111       return standardToString();
112     }
113   }
114 
suite()115   public static Test suite() {
116     TestSuite suite = new TestSuite();
117 
118     suite.addTestSuite(ForwardingSetTest.class);
119     suite.addTest(
120         SetTestSuiteBuilder.using(
121                 new TestStringSetGenerator() {
122                   @Override
123                   protected Set<String> create(String[] elements) {
124                     return new StandardImplForwardingSet<>(Sets.newLinkedHashSet(asList(elements)));
125                   }
126                 })
127             .named("ForwardingSet[LinkedHashSet] with standard implementations")
128             .withFeatures(
129                 CollectionSize.ANY,
130                 CollectionFeature.ALLOWS_NULL_VALUES,
131                 CollectionFeature.GENERAL_PURPOSE)
132             .createTestSuite());
133     suite.addTest(
134         SetTestSuiteBuilder.using(
135                 new TestStringSetGenerator() {
136                   @Override
137                   protected Set<String> create(String[] elements) {
138                     return new StandardImplForwardingSet<>(MinimalSet.of(elements));
139                   }
140                 })
141             .named("ForwardingSet[MinimalSet] with standard implementations")
142             .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES)
143             .createTestSuite());
144 
145     return suite;
146   }
147 
148   @SuppressWarnings({"rawtypes", "unchecked"})
testForwarding()149   public void testForwarding() {
150     new ForwardingWrapperTester()
151         .testForwarding(
152             Set.class,
153             new Function<Set, Set>() {
154               @Override
155               public Set apply(Set delegate) {
156                 return wrap(delegate);
157               }
158             });
159   }
160 
testEquals()161   public void testEquals() {
162     Set<String> set1 = ImmutableSet.of("one");
163     Set<String> set2 = ImmutableSet.of("two");
164     new EqualsTester()
165         .addEqualityGroup(set1, wrap(set1), wrap(set1))
166         .addEqualityGroup(set2, wrap(set2))
167         .testEquals();
168   }
169 
wrap(final Set<T> delegate)170   private static <T> Set<T> wrap(final Set<T> delegate) {
171     return new ForwardingSet<T>() {
172       @Override
173       protected Set<T> delegate() {
174         return delegate;
175       }
176     };
177   }
178 }
179