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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET;
22
23import com.google.common.collect.testing.features.CollectionFeature;
24import com.google.common.collect.testing.features.CollectionSize;
25import com.google.common.collect.testing.features.ListFeature;
26
27import java.lang.reflect.Method;
28
29/**
30 * A generic JUnit test which tests {@code set()} operations on a list. Can't be
31 * invoked directly; please see
32 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
33 *
34 * <p>This class is GWT compatible.
35 *
36 * @author George van den Driessche
37 */
38public class ListSetTester<E> extends AbstractListTester<E> {
39  @ListFeature.Require(SUPPORTS_SET)
40  @CollectionSize.Require(absent = ZERO)
41  public void testSet() {
42    doTestSet(samples.e3);
43  }
44
45  @CollectionSize.Require(absent = ZERO)
46  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
47  @ListFeature.Require(SUPPORTS_SET)
48  public void testSet_null() {
49    doTestSet(null);
50  }
51
52  @CollectionSize.Require(absent = ZERO)
53  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
54  @ListFeature.Require(SUPPORTS_SET)
55  public void testSet_replacingNull() {
56    E[] elements = createSamplesArray();
57    int i = aValidIndex();
58    elements[i] = null;
59    collection = getSubjectGenerator().create(elements);
60
61    doTestSet(samples.e3);
62  }
63
64  private void doTestSet(E newValue) {
65    int index = aValidIndex();
66    E initialValue = getList().get(index);
67    assertEquals("set(i, x) should return the old element at position i.",
68        initialValue, getList().set(index, newValue));
69    assertEquals("After set(i, x), get(i) should return x",
70        newValue, getList().get(index));
71    assertEquals("set() should not change the size of a list.",
72        getNumElements(), getList().size());
73  }
74
75  @ListFeature.Require(SUPPORTS_SET)
76  public void testSet_indexTooLow() {
77    try {
78      getList().set(-1, samples.e3);
79      fail("set(-1) should throw IndexOutOfBoundsException");
80    } catch (IndexOutOfBoundsException expected) {
81    }
82    expectUnchanged();
83  }
84
85  @ListFeature.Require(SUPPORTS_SET)
86  public void testSet_indexTooHigh() {
87    int index = getNumElements();
88    try {
89      getList().set(index, samples.e3);
90      fail("set(size) should throw IndexOutOfBoundsException");
91    } catch (IndexOutOfBoundsException expected) {
92    }
93    expectUnchanged();
94  }
95
96  @CollectionSize.Require(absent = ZERO)
97  @ListFeature.Require(absent = SUPPORTS_SET)
98  public void testSet_unsupported() {
99    try {
100      getList().set(aValidIndex(), samples.e3);
101      fail("set() should throw UnsupportedOperationException");
102    } catch (UnsupportedOperationException expected) {
103    }
104    expectUnchanged();
105  }
106
107  @CollectionSize.Require(ZERO)
108  @ListFeature.Require(absent = SUPPORTS_SET)
109  public void testSet_unsupportedByEmptyList() {
110    try {
111      getList().set(0, samples.e3);
112      fail("set() should throw UnsupportedOperationException "
113          + "or IndexOutOfBoundsException");
114    } catch (UnsupportedOperationException tolerated) {
115    } catch (IndexOutOfBoundsException tolerated) {
116    }
117    expectUnchanged();
118  }
119
120  @CollectionSize.Require(absent = ZERO)
121  @ListFeature.Require(SUPPORTS_SET)
122  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
123  public void testSet_nullUnsupported() {
124    try {
125      getList().set(aValidIndex(), null);
126      fail("set(null) should throw NullPointerException");
127    } catch (NullPointerException expected) {
128    }
129    expectUnchanged();
130  }
131
132  private int aValidIndex() {
133    return getList().size() / 2;
134  }
135
136  /**
137   * Returns the {@link java.lang.reflect.Method} instance for
138   * {@link #testSet_null()} so that tests of {@link
139   * java.util.Collections#checkedCollection(java.util.Collection, Class)} can
140   * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}
141   * until <a
142   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug
143   * 6409434</a> is fixed. It's unclear whether nulls were to be permitted or
144   * forbidden, but presumably the eventual fix will be to permit them, as it
145   * seems more likely that code would depend on that behavior than on the
146   * other. Thus, we say the bug is in set(), which fails to support null.
147   */
148  public static Method getSetNullSupportedMethod() {
149    return Platform.getMethod(ListSetTester.class, "testSet_null");
150  }
151}
152