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