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.CollectionSize.ZERO;
 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
 
 
A generic JUnit test which tests set() operations on a list. Can't be invoked directly; please see com.google.common.collect.testing.ListTestSuiteBuilder.

Author(s):
George van den Driessche
 
 @GwtCompatible(emulated = true)
 public class ListSetTester<E> extends AbstractListTester<E> {
   @CollectionSize.Require(absent = )
   public void testSet() {
     doTestSet(.);
   }
 
   @CollectionSize.Require(absent = )
   public void testSet_null() {
     doTestSet(null);
   }
 
   @CollectionSize.Require(absent = )
   public void testSet_replacingNull() {
     E[] elements = createSamplesArray();
     int i = aValidIndex();
     elements[i] = null;
      = getSubjectGenerator().create(elements);
 
     doTestSet(.);
   }
 
   private void doTestSet(E newValue) {
     int index = aValidIndex();
     E initialValue = getList().get(index);
     assertEquals("set(i, x) should return the old element at position i.",
         initialValuegetList().set(indexnewValue));
     assertEquals("After set(i, x), get(i) should return x",
         newValuegetList().get(index));
     assertEquals("set() should not change the size of a list.",
         getNumElements(), getList().size());
   }
 
   public void testSet_indexTooLow() {
     try {
       getList().set(-1, .);
       fail("set(-1) should throw IndexOutOfBoundsException");
     } catch (IndexOutOfBoundsException expected) {
     }
     expectUnchanged();
   }
 
   public void testSet_indexTooHigh() {
     int index = getNumElements();
     try {
       getList().set(index.);
       fail("set(size) should throw IndexOutOfBoundsException");
     } catch (IndexOutOfBoundsException expected) {
     }
     expectUnchanged();
   }
 
   @CollectionSize.Require(absent = )
  public void testSet_unsupported() {
    try {
      getList().set(aValidIndex(), .);
      fail("set() should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }
  }
  public void testSet_unsupportedByEmptyList() {
    try {
      getList().set(0, .);
      fail("set() should throw UnsupportedOperationException "
          + "or IndexOutOfBoundsException");
    } catch (UnsupportedOperationException tolerated) {
    } catch (IndexOutOfBoundsException tolerated) {
    }
  }
  @CollectionSize.Require(absent = )
  public void testSet_nullUnsupported() {
    try {
      getList().set(aValidIndex(), null);
      fail("set(null) should throw NullPointerException");
    } catch (NullPointerException expected) {
    }
  }
  private int aValidIndex() {
    return getList().size() / 2;
  }

  
Returns the java.lang.reflect.Method instance for testSet_null() so that tests of java.util.Collections.checkedCollection(java.util.Collection,java.lang.Class) can suppress it with FeatureSpecificTestSuiteBuilder.suppressing() until Sun bug 6409434 is fixed. It's unclear whether nulls were to be permitted or forbidden, but presumably the eventual fix will be to permit them, as it seems more likely that code would depend on that behavior than on the other. Thus, we say the bug is in set(), which fails to support null.
  @GwtIncompatible("reflection")
  public static Method getSetNullSupportedMethod() {
    return Helpers.getMethod(ListSetTester.class"testSet_null");
  }
New to GrepCode? Check out our FAQ X