aboutsummaryrefslogtreecommitdiffstats
path: root/guava-testlib/src/com/google/common/collect/testing/testers/ListSetTester.java
blob: 2b3b7b07ab339c507b71f22b1904c5c29803dfc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * 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;

import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.ListFeature;

import java.lang.reflect.Method;

/**
 * A generic JUnit test which tests {@code set()} operations on a list. Can't be
 * invoked directly; please see
 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
 *
 * <p>This class is GWT compatible.
 *
 * @author George van den Driessche
 */
public class ListSetTester<E> extends AbstractListTester<E> {
  @ListFeature.Require(SUPPORTS_SET)
  @CollectionSize.Require(absent = ZERO)
  public void testSet() {
    doTestSet(samples.e3);
  }

  @CollectionSize.Require(absent = ZERO)
  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
  @ListFeature.Require(SUPPORTS_SET)
  public void testSet_null() {
    doTestSet(null);
  }

  @CollectionSize.Require(absent = ZERO)
  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
  @ListFeature.Require(SUPPORTS_SET)
  public void testSet_replacingNull() {
    E[] elements = createSamplesArray();
    int i = aValidIndex();
    elements[i] = null;
    collection = getSubjectGenerator().create(elements);

    doTestSet(samples.e3);
  }

  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.",
        initialValue, getList().set(index, newValue));
    assertEquals("After set(i, x), get(i) should return x",
        newValue, getList().get(index));
    assertEquals("set() should not change the size of a list.",
        getNumElements(), getList().size());
  }

  @ListFeature.Require(SUPPORTS_SET)
  public void testSet_indexTooLow() {
    try {
      getList().set(-1, samples.e3);
      fail("set(-1) should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException expected) {
    }
    expectUnchanged();
  }

  @ListFeature.Require(SUPPORTS_SET)
  public void testSet_indexTooHigh() {
    int index = getNumElements();
    try {
      getList().set(index, samples.e3);
      fail("set(size) should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException expected) {
    }
    expectUnchanged();
  }

  @CollectionSize.Require(absent = ZERO)
  @ListFeature.Require(absent = SUPPORTS_SET)
  public void testSet_unsupported() {
    try {
      getList().set(aValidIndex(), samples.e3);
      fail("set() should throw UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }
    expectUnchanged();
  }

  @CollectionSize.Require(ZERO)
  @ListFeature.Require(absent = SUPPORTS_SET)
  public void testSet_unsupportedByEmptyList() {
    try {
      getList().set(0, samples.e3);
      fail("set() should throw UnsupportedOperationException "
          + "or IndexOutOfBoundsException");
    } catch (UnsupportedOperationException tolerated) {
    } catch (IndexOutOfBoundsException tolerated) {
    }
    expectUnchanged();
  }

  @CollectionSize.Require(absent = ZERO)
  @ListFeature.Require(SUPPORTS_SET)
  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
  public void testSet_nullUnsupported() {
    try {
      getList().set(aValidIndex(), null);
      fail("set(null) should throw NullPointerException");
    } catch (NullPointerException expected) {
    }
    expectUnchanged();
  }

  private int aValidIndex() {
    return getList().size() / 2;
  }

  /**
   * Returns the {@link java.lang.reflect.Method} instance for
   * {@link #testSet_null()} so that tests of {@link
   * java.util.Collections#checkedCollection(java.util.Collection, Class)} can
   * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()}
   * until <a
   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug
   * 6409434</a> 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.
   */
  public static Method getSetNullSupportedMethod() {
    return Platform.getMethod(ListSetTester.class, "testSet_null");
  }
}