Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2008 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect.testing.testers;
 
 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
 import static com.google.common.collect.testing.features.CollectionSize.ONE;
 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
 
 
 import java.util.List;

A generic JUnit test which tests retainAll operations on a collection. Can't be invoked directly; please see com.google.common.collect.testing.CollectionTestSuiteBuilder.

This class is GWT compatible.

Author(s):
Chris Povirk
 
 @SuppressWarnings("unchecked"// too many "unchecked generic array creations"
 public class CollectionRetainAllTester<E> extends AbstractCollectionTester<E> {

  
A collection of elements to retain, along with a description for use in failure messages.
 
   private class Target {
     private final Collection<E> toRetain;
     private final String description;
 
     private Target(Collection<E> toRetainString description) {
       this. = toRetain;
       this. = description;
     }
 
     @Override public String toString() {
       return ;
     }
   }
 
   private Target empty;
   private Target disjoint;
   private Target superset;
   private Target sameElements;
   private Target partialOverlap;
   private Target containsDuplicates;
   private Target nullSingleton;
 
   @Override public void setUp() throws Exception {
     super.setUp();
 
      = new Target(emptyCollection(), "empty");
     /*
      * We test that nullSingleton.retainAll(disjointList) does NOT throw a
      * NullPointerException when disjointList does not, so we can't use
      * MinimalCollection, which throws NullPointerException on calls to
      * contains(null).
      */
     List<E> disjointList = Arrays.asList(..);
     
         = new Target(disjointList"disjoint");
     
         = new Target(MinimalCollection.of(
             .....),
             "superset");
         = new Target(MinimalCollection.of(.), "subset");
     
         = new Target(Arrays.asList(createSamplesArray()), "sameElements");
      = new Target(
         MinimalCollection.of(....),
         "containsDuplicates");
        = new Target(MinimalCollection.of(..),
            "partialOverlap");
        = new Target(Collections.<E>singleton(null), "nullSingleton");
  }
  // retainAll(empty)
  }
  }
  @CollectionSize.Require(absent = )
  }
  @CollectionSize.Require(absent = )
  }
  // retainAll(disjoint)
  }
  }
  @CollectionSize.Require(absent = )
  }
  @CollectionSize.Require(absent = )
  }
  // retainAll(superset)
  public void testRetainAll_superset() {
  }
  }
  // retainAll(subset)
  @CollectionSize.Require(absent = {})
  public void testRetainAll_subset() {
  }
  @CollectionSize.Require(absent = {})
  public void testRetainAll_subsetUnsupported() {
  }
  // retainAll(sameElements)
  public void testRetainAll_sameElements() {
  }
  }
  // retainAll(partialOverlap)
  @CollectionSize.Require(absent = {})
  public void testRetainAll_partialOverlap() {
  }
  @CollectionSize.Require(absent = {})
  }
  // retainAll(containsDuplicates)
  }
  @CollectionSize.Require(absent = {})
  }
  // retainAll(nullSingleton)
  }
  @CollectionSize.Require(absent = )
  }
  }
  @CollectionSize.Require(absent = {})
  }
  // nullSingleton.retainAll()
  @CollectionSize.Require(absent = )
  }
  // retainAll(null)
  /*
   * AbstractCollection fails the retainAll(null) test when the subject
   * collection is empty, but we'd still like to test retainAll(null) when we
   * can. We split the test into empty and non-empty cases. This allows us to
   * suppress only the former.
   */
    try {
      .retainAll(null);
      // Returning successfully is not ideal, but tolerated.
    } catch (NullPointerException expected) {
    }
  }
  @CollectionSize.Require(absent = )
    try {
      .retainAll(null);
      fail("retainAll(null) should throw NullPointerException");
    } catch (NullPointerException expected) {
    }
  }
  private void expectReturnsTrue(Target target) {
    String message
        = Platform.format("retainAll(%s) should return true"target);
    assertTrue(message.retainAll(target.toRetain));
  }
  private void expectReturnsFalse(Target target) {
    String message
        = Platform.format("retainAll(%s) should return false"target);
    assertFalse(message.retainAll(target.toRetain));
  }
  private void expectThrows(Target target) {
    try {
      .retainAll(target.toRetain);
      String message = Platform.format("retainAll(%s) should throw"target);
      fail(message);
    } catch (UnsupportedOperationException expected) {
    }
  }
  private void expectReturnsFalseOrThrows(Target target) {
    String message
        = Platform.format("retainAll(%s) should return false or throw"target);
    try {
      assertFalse(message.retainAll(target.toRetain));
    } catch (UnsupportedOperationException tolerated) {
    }
  }
New to GrepCode? Check out our FAQ X