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