View Javadoc
1   /*
2    * Copyright (C) 2008 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.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
20  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21  import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
23  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
24  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.collect.testing.AbstractCollectionTester;
28  import com.google.common.collect.testing.WrongType;
29  import com.google.common.collect.testing.features.CollectionFeature;
30  import com.google.common.collect.testing.features.CollectionSize;
31  import java.util.ConcurrentModificationException;
32  import java.util.Iterator;
33  
34  /**
35   * A generic JUnit test which tests {@code remove} operations on a collection.
36   * Can't be invoked directly; please see
37   * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
38   *
39   * @author George van den Driessche
40   */
41  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
42  @GwtCompatible
43  public class CollectionRemoveTester<E> extends AbstractCollectionTester<E> {
44    @CollectionFeature.Require(SUPPORTS_REMOVE)
45    @CollectionSize.Require(absent = ZERO)
46    public void testRemove_present() {
47      int initialSize = collection.size();
48      assertTrue("remove(present) should return true", collection.remove(e0()));
49      assertEquals(
50          "remove(present) should decrease a collection's size by one.",
51          initialSize - 1,
52          collection.size());
53      expectMissing(e0());
54    }
55  
56    @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
57    @CollectionSize.Require(SEVERAL)
58    public void testRemovePresentConcurrentWithIteration() {
59      try {
60        Iterator<E> iterator = collection.iterator();
61        assertTrue(collection.remove(e0()));
62        iterator.next();
63        fail("Expected ConcurrentModificationException");
64      } catch (ConcurrentModificationException expected) {
65        // success
66      }
67    }
68  
69    @CollectionFeature.Require(SUPPORTS_REMOVE)
70    public void testRemove_notPresent() {
71      assertFalse("remove(notPresent) should return false", collection.remove(e3()));
72      expectUnchanged();
73    }
74  
75    @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
76    @CollectionSize.Require(absent = ZERO)
77    public void testRemove_nullPresent() {
78      collection = getSubjectGenerator().create(createArrayWithNullElement());
79  
80      int initialSize = collection.size();
81      assertTrue("remove(null) should return true", collection.remove(null));
82      assertEquals(
83          "remove(present) should decrease a collection's size by one.",
84          initialSize - 1,
85          collection.size());
86      expectMissing((E) null);
87    }
88  
89    @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
90    @CollectionSize.Require(absent = ZERO)
91    public void testRemove_unsupported() {
92      try {
93        collection.remove(e0());
94        fail("remove(present) should throw UnsupportedOperationException");
95      } catch (UnsupportedOperationException expected) {
96      }
97      expectUnchanged();
98      assertTrue("remove(present) should not remove the element", collection.contains(e0()));
99    }
100 
101   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
102   public void testRemove_unsupportedNotPresent() {
103     try {
104       assertFalse(
105           "remove(notPresent) should return false or throw UnsupportedOperationException",
106           collection.remove(e3()));
107     } catch (UnsupportedOperationException tolerated) {
108     }
109     expectUnchanged();
110     expectMissing(e3());
111   }
112 
113   @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
114   public void testRemove_nullNotSupported() {
115     try {
116       assertFalse(
117           "remove(null) should return false or throw NullPointerException",
118           collection.remove(null));
119     } catch (NullPointerException tolerated) {
120     }
121     expectUnchanged();
122   }
123 
124   @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
125   public void testRemove_nullAllowed() {
126     assertFalse("remove(null) should return false", collection.remove(null));
127     expectUnchanged();
128   }
129 
130   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
131   @CollectionSize.Require(absent = ZERO)
132   public void testIteratorRemove_unsupported() {
133     Iterator<E> iterator = collection.iterator();
134     iterator.next();
135     try {
136       iterator.remove();
137       fail("iterator.remove() should throw UnsupportedOperationException");
138     } catch (UnsupportedOperationException expected) {
139     }
140     expectUnchanged();
141     assertTrue(collection.contains(e0()));
142   }
143 
144   @CollectionFeature.Require(SUPPORTS_REMOVE)
145   public void testRemove_wrongType() {
146     try {
147       assertFalse(collection.remove(WrongType.VALUE));
148     } catch (ClassCastException tolerated) {
149     }
150     expectUnchanged();
151   }
152 }