Home | History | Annotate | Download | only in testers
      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.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.CollectionSize.ZERO;
     22 
     23 import com.google.common.annotations.GwtCompatible;
     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 /**
     30  * A generic JUnit test which tests {@code contains()} operations on a
     31  * collection. Can't be invoked directly; please see
     32  * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
     33  *
     34  * @author Kevin Bourrillion
     35  * @author Chris Povirk
     36  */
     37 @GwtCompatible
     38 public class CollectionContainsTester<E> extends AbstractCollectionTester<E> {
     39   @CollectionSize.Require(absent = ZERO)
     40   public void testContains_yes() {
     41     assertTrue("contains(present) should return true",
     42         collection.contains(samples.e0));
     43   }
     44 
     45   public void testContains_no() {
     46     assertFalse("contains(notPresent) should return false",
     47         collection.contains(samples.e3));
     48   }
     49 
     50   @CollectionFeature.Require(ALLOWS_NULL_QUERIES)
     51   public void testContains_nullNotContainedButQueriesSupported() {
     52     assertFalse("contains(null) should return false",
     53         collection.contains(null));
     54   }
     55 
     56   @CollectionFeature.Require(absent = ALLOWS_NULL_QUERIES)
     57   public void testContains_nullNotContainedAndUnsupported() {
     58     expectNullMissingWhenNullUnsupported(
     59         "contains(null) should return false or throw");
     60   }
     61 
     62   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
     63   @CollectionSize.Require(absent = ZERO)
     64   public void testContains_nonNullWhenNullContained() {
     65     initCollectionWithNullElement();
     66     assertFalse("contains(notPresent) should return false",
     67         collection.contains(samples.e3));
     68   }
     69 
     70   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
     71   @CollectionSize.Require(absent = ZERO)
     72   public void testContains_nullContained() {
     73     initCollectionWithNullElement();
     74     assertTrue("contains(null) should return true", collection.contains(null));
     75   }
     76 
     77   public void testContains_wrongType() {
     78     try {
     79       assertFalse("contains(wrongType) should return false or throw",
     80           collection.contains(WrongType.VALUE));
     81     } catch (ClassCastException tolerated) {
     82     }
     83   }
     84 }
     85