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