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 import static org.junit.contrib.truth.Truth.ASSERT; 21 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.testing.NullPointerTester; 25 26 import junit.framework.TestCase; 27 28 import java.util.Collection; 29 import java.util.Collections; 30 31 /** 32 * Common tests for a {@code Collection}. 33 * 34 * @author Kevin Bourrillion 35 * @author Mike Bostock 36 */ 37 @GwtCompatible(emulated = true) 38 public abstract class AbstractCollectionTest extends TestCase { 39 create()40 protected abstract <E> Collection<E> create(); 41 42 protected Collection<String> c; 43 44 // public for GWT setUp()45 @Override public void setUp() throws Exception { 46 super.setUp(); 47 c = create(); 48 } 49 testIsEmptyYes()50 public void testIsEmptyYes() { 51 assertTrue(c.isEmpty()); 52 } 53 testIsEmptyNo()54 public void testIsEmptyNo() { 55 c.add("a"); 56 assertFalse(c.isEmpty()); 57 } 58 testAddOne()59 public void testAddOne() { 60 assertTrue(c.add("a")); 61 assertContents("a"); 62 } 63 testAddSeveralTimes()64 public void testAddSeveralTimes() { 65 assertTrue(c.add("a")); 66 assertTrue(c.add("b")); 67 c.add("a"); 68 c.add("b"); 69 assertTrue(c.contains("a")); 70 assertTrue(c.contains("b")); 71 } 72 testRemoveOneFromNoneStandard()73 public void testRemoveOneFromNoneStandard() { 74 assertFalse(c.remove("a")); 75 assertContents(); 76 } 77 testRemoveOneFromOneStandard()78 public void testRemoveOneFromOneStandard() { 79 c.add("a"); 80 assertTrue(c.remove("a")); 81 assertContents(); 82 } 83 testContainsNo()84 public void testContainsNo() { 85 c.add("a"); 86 assertFalse(c.contains("b")); 87 } 88 testContainsOne()89 public void testContainsOne() { 90 c.add("a"); 91 assertTrue(c.contains(new String("a"))); 92 } 93 testContainsAfterRemoval()94 public void testContainsAfterRemoval() { 95 c.add("a"); 96 c.remove("a"); 97 assertFalse(c.contains("a")); 98 } 99 testContainsAllVacuous()100 public void testContainsAllVacuous() { 101 assertTrue(c.containsAll(Collections.emptySet())); 102 } 103 testRemoveAllVacuous()104 public void testRemoveAllVacuous() { 105 assertFalse(c.removeAll(Collections.emptySet())); 106 } 107 testRetainAllVacuous()108 public void testRetainAllVacuous() { 109 assertFalse(c.retainAll(asList("a"))); 110 assertContents(); 111 } 112 testRetainAllOfNothing()113 public void testRetainAllOfNothing() { 114 c.add("a"); 115 assertTrue(c.retainAll(Collections.emptySet())); 116 assertContents(); 117 } 118 testClearNothing()119 public void testClearNothing() { 120 c.clear(); 121 assertContents(); 122 } 123 testClear()124 public void testClear() { 125 c = createSample(); 126 c.clear(); 127 assertContents(); 128 } 129 testEqualsNo()130 public void testEqualsNo() { 131 c.add("a"); 132 133 Collection<String> c2 = create(); 134 c2.add("b"); 135 136 assertFalse(c.equals(c2)); 137 } 138 testEqualsYes()139 public void testEqualsYes() { 140 c.add("a"); 141 c.add("b"); 142 c.add("b"); 143 144 Collection<String> c2 = create(); 145 c2.add("a"); 146 c2.add("b"); 147 c2.add("b"); 148 149 assertEquals(c, c2); 150 } 151 testEqualsSelf()152 public void testEqualsSelf() { 153 c.add("a"); 154 c.add("b"); 155 c.add("b"); 156 157 assertEquals(c, c); 158 } 159 testEqualsTricky()160 public void testEqualsTricky() { 161 c.add("a"); 162 c.add("a"); 163 164 Collection<String> c2 = create(); 165 c2.add("a"); 166 c2.add("a"); 167 c2.add("b"); 168 c2.add("b"); 169 c2.remove("b"); 170 c2.remove("b"); 171 172 assertEquals(c, c2); 173 } 174 testEqualsPartial()175 public void testEqualsPartial() { 176 c.add("a"); 177 c.add("b"); 178 179 Collection<String> c2 = create(); 180 c2.add("a"); 181 c2.add("c"); 182 183 assertFalse(c.equals(c2)); 184 185 Collection<String> c3 = create(); 186 c2.add("b"); 187 c2.add("c"); 188 189 assertFalse(c2.equals(c3)); 190 } 191 testEqualsDifferentTypes()192 public void testEqualsDifferentTypes() { 193 c.add("a"); 194 assertFalse(c.equals("a")); 195 } 196 testToArrayOne()197 public void testToArrayOne() { 198 c.add("a"); 199 String[] array = new String[3]; 200 assertSame(array, c.toArray(array)); 201 assertEquals("a", array[0]); 202 assertNull(array[1]); 203 } 204 205 @GwtIncompatible("NullPointerTester") testNullPointerExceptions()206 public void testNullPointerExceptions() throws Exception { 207 NullPointerTester tester = new NullPointerTester(); 208 tester.testAllPublicInstanceMethods(c); 209 } 210 createSample()211 protected Collection<String> createSample() { 212 Collection<String> c = create(); 213 c.add("a"); 214 c.add("b"); 215 c.add("b"); 216 c.add("c"); 217 c.add("d"); 218 c.add("d"); 219 c.add("d"); 220 return c; 221 } 222 assertContents(String... expected)223 protected void assertContents(String... expected) { 224 ASSERT.that(c).hasContentsAnyOrder(expected); 225 } 226 } 227