001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.Helpers.getMethod;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
023import static org.junit.Assert.assertThrows;
024
025import com.google.common.annotations.GwtCompatible;
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.annotations.J2ktIncompatible;
028import com.google.common.collect.testing.features.CollectionFeature;
029import com.google.common.collect.testing.features.CollectionSize;
030import com.google.common.collect.testing.features.ListFeature;
031import java.lang.reflect.Method;
032
033/**
034 * A generic JUnit test which tests {@code set()} operations on a list. Can't be invoked directly;
035 * please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
036 *
037 * @author George van den Driessche
038 */
039@GwtCompatible
040public class ListSetTester<E> extends AbstractListTester<E> {
041  @ListFeature.Require(SUPPORTS_SET)
042  @CollectionSize.Require(absent = ZERO)
043  public void testSet() {
044    doTestSet(e3());
045  }
046
047  @CollectionSize.Require(absent = ZERO)
048  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
049  @ListFeature.Require(SUPPORTS_SET)
050  public void testSet_null() {
051    doTestSet(null);
052  }
053
054  @CollectionSize.Require(absent = ZERO)
055  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
056  @ListFeature.Require(SUPPORTS_SET)
057  public void testSet_replacingNull() {
058    E[] elements = createSamplesArray();
059    int i = aValidIndex();
060    elements[i] = null;
061    collection = getSubjectGenerator().create(elements);
062
063    doTestSet(e3());
064  }
065
066  private void doTestSet(E newValue) {
067    int index = aValidIndex();
068    E initialValue = getList().get(index);
069    assertEquals(
070        "set(i, x) should return the old element at position i.",
071        initialValue,
072        getList().set(index, newValue));
073    assertEquals("After set(i, x), get(i) should return x", newValue, getList().get(index));
074    assertEquals("set() should not change the size of a list.", getNumElements(), getList().size());
075  }
076
077  @ListFeature.Require(SUPPORTS_SET)
078  public void testSet_indexTooLow() {
079    assertThrows(IndexOutOfBoundsException.class, () -> getList().set(-1, e3()));
080    expectUnchanged();
081  }
082
083  @ListFeature.Require(SUPPORTS_SET)
084  public void testSet_indexTooHigh() {
085    int index = getNumElements();
086    assertThrows(IndexOutOfBoundsException.class, () -> getList().set(index, e3()));
087    expectUnchanged();
088  }
089
090  @CollectionSize.Require(absent = ZERO)
091  @ListFeature.Require(absent = SUPPORTS_SET)
092  public void testSet_unsupported() {
093    assertThrows(UnsupportedOperationException.class, () -> getList().set(aValidIndex(), e3()));
094    expectUnchanged();
095  }
096
097  @CollectionSize.Require(ZERO)
098  @ListFeature.Require(absent = SUPPORTS_SET)
099  public void testSet_unsupportedByEmptyList() {
100    RuntimeException expected = assertThrows(RuntimeException.class, () -> getList().set(0, e3()));
101    if (!(expected instanceof UnsupportedOperationException
102        || expected instanceof IndexOutOfBoundsException)) {
103      throw expected;
104    }
105    expectUnchanged();
106  }
107
108  @CollectionSize.Require(absent = ZERO)
109  @ListFeature.Require(SUPPORTS_SET)
110  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
111  public void testSet_nullUnsupported() {
112    assertThrows(NullPointerException.class, () -> getList().set(aValidIndex(), null));
113    expectUnchanged();
114  }
115
116  private int aValidIndex() {
117    return getList().size() / 2;
118  }
119
120  /**
121   * Returns the {@link java.lang.reflect.Method} instance for {@link #testSet_null()} so that tests
122   * of {@link java.util.Collections#checkedCollection(java.util.Collection, Class)} can suppress it
123   * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
124   * href="https://bugs.openjdk.org/browse/JDK-6409434">JDK-6409434</a> is fixed. It's unclear
125   * whether nulls were to be permitted or forbidden, but presumably the eventual fix will be to
126   * permit them, as it seems more likely that code would depend on that behavior than on the other.
127   * Thus, we say the bug is in set(), which fails to support null.
128   */
129  @J2ktIncompatible
130  @GwtIncompatible // reflection
131  public static Method getSetNullSupportedMethod() {
132    return getMethod(ListSetTester.class, "testSet_null");
133  }
134}