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