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.base.Function; 22 import com.google.common.collect.testing.CollectionTestSuiteBuilder; 23 import com.google.common.collect.testing.MinimalCollection; 24 import com.google.common.collect.testing.TestStringCollectionGenerator; 25 import com.google.common.collect.testing.features.CollectionFeature; 26 import com.google.common.collect.testing.features.CollectionSize; 27 import com.google.common.testing.ForwardingWrapperTester; 28 import java.util.Collection; 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 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 TestCase { 41 static final class StandardImplForwardingCollection<T> extends ForwardingCollection<T> { 42 private final Collection<T> backingCollection; 43 StandardImplForwardingCollection(Collection<T> backingCollection)44 StandardImplForwardingCollection(Collection<T> backingCollection) { 45 this.backingCollection = backingCollection; 46 } 47 48 @Override delegate()49 protected Collection<T> delegate() { 50 return backingCollection; 51 } 52 53 @Override addAll(Collection<? extends T> collection)54 public boolean addAll(Collection<? extends T> collection) { 55 return standardAddAll(collection); 56 } 57 58 @Override clear()59 public void clear() { 60 standardClear(); 61 } 62 63 @Override contains(Object object)64 public boolean contains(Object object) { 65 return standardContains(object); 66 } 67 68 @Override containsAll(Collection<?> collection)69 public boolean containsAll(Collection<?> collection) { 70 return standardContainsAll(collection); 71 } 72 73 @Override remove(Object object)74 public boolean remove(Object object) { 75 return standardRemove(object); 76 } 77 78 @Override removeAll(Collection<?> collection)79 public boolean removeAll(Collection<?> collection) { 80 return standardRemoveAll(collection); 81 } 82 83 @Override retainAll(Collection<?> collection)84 public boolean retainAll(Collection<?> collection) { 85 return standardRetainAll(collection); 86 } 87 88 @Override toArray()89 public Object[] toArray() { 90 return standardToArray(); 91 } 92 93 @Override toArray(T[] array)94 public <T> T[] toArray(T[] array) { 95 return standardToArray(array); 96 } 97 98 @Override toString()99 public String toString() { 100 return standardToString(); 101 } 102 } 103 suite()104 public static Test suite() { 105 TestSuite suite = new TestSuite(); 106 107 suite.addTestSuite(ForwardingCollectionTest.class); 108 suite.addTest( 109 CollectionTestSuiteBuilder.using( 110 new TestStringCollectionGenerator() { 111 @Override 112 protected Collection<String> create(String[] elements) { 113 return new StandardImplForwardingCollection<>( 114 Lists.newLinkedList(asList(elements))); 115 } 116 }) 117 .named("ForwardingCollection[LinkedList] with standard implementations") 118 .withFeatures( 119 CollectionSize.ANY, 120 CollectionFeature.ALLOWS_NULL_VALUES, 121 CollectionFeature.GENERAL_PURPOSE) 122 .createTestSuite()); 123 suite.addTest( 124 CollectionTestSuiteBuilder.using( 125 new TestStringCollectionGenerator() { 126 @Override 127 protected Collection<String> create(String[] elements) { 128 return new StandardImplForwardingCollection<>(MinimalCollection.of(elements)); 129 } 130 }) 131 .named("ForwardingCollection[MinimalCollection] with standard" + " implementations") 132 .withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES) 133 .createTestSuite()); 134 135 return suite; 136 } 137 138 @SuppressWarnings({"rawtypes", "unchecked"}) testForwarding()139 public void testForwarding() { 140 new ForwardingWrapperTester() 141 .testForwarding( 142 Collection.class, 143 new Function<Collection, Collection>() { 144 @Override 145 public Collection apply(Collection delegate) { 146 return wrap(delegate); 147 } 148 }); 149 } 150 wrap(final Collection<T> delegate)151 private static <T> Collection<T> wrap(final Collection<T> delegate) { 152 return new ForwardingCollection<T>() { 153 @Override 154 protected Collection<T> delegate() { 155 return delegate; 156 } 157 }; 158 } 159 } 160