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.features.CollectionFeature.ALLOWS_NULL_VALUES; 020import static com.google.common.collect.testing.features.CollectionSize.ZERO; 021import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_SET; 022 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.annotations.GwtIncompatible; 025import com.google.common.annotations.J2ktIncompatible; 026import com.google.common.collect.testing.Helpers; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import com.google.common.collect.testing.features.ListFeature; 030import java.lang.reflect.Method; 031import org.junit.Ignore; 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(emulated = true) 040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 041@SuppressWarnings("JUnit4ClassUsedInJUnit3") 042public class ListSetTester<E> extends AbstractListTester<E> { 043 @ListFeature.Require(SUPPORTS_SET) 044 @CollectionSize.Require(absent = ZERO) 045 public void testSet() { 046 doTestSet(e3()); 047 } 048 049 @CollectionSize.Require(absent = ZERO) 050 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 051 @ListFeature.Require(SUPPORTS_SET) 052 public void testSet_null() { 053 doTestSet(null); 054 } 055 056 @CollectionSize.Require(absent = ZERO) 057 @CollectionFeature.Require(ALLOWS_NULL_VALUES) 058 @ListFeature.Require(SUPPORTS_SET) 059 public void testSet_replacingNull() { 060 E[] elements = createSamplesArray(); 061 int i = aValidIndex(); 062 elements[i] = null; 063 collection = getSubjectGenerator().create(elements); 064 065 doTestSet(e3()); 066 } 067 068 private void doTestSet(E newValue) { 069 int index = aValidIndex(); 070 E initialValue = getList().get(index); 071 assertEquals( 072 "set(i, x) should return the old element at position i.", 073 initialValue, 074 getList().set(index, newValue)); 075 assertEquals("After set(i, x), get(i) should return x", newValue, getList().get(index)); 076 assertEquals("set() should not change the size of a list.", getNumElements(), getList().size()); 077 } 078 079 @ListFeature.Require(SUPPORTS_SET) 080 public void testSet_indexTooLow() { 081 try { 082 getList().set(-1, e3()); 083 fail("set(-1) should throw IndexOutOfBoundsException"); 084 } catch (IndexOutOfBoundsException expected) { 085 } 086 expectUnchanged(); 087 } 088 089 @ListFeature.Require(SUPPORTS_SET) 090 public void testSet_indexTooHigh() { 091 int index = getNumElements(); 092 try { 093 getList().set(index, e3()); 094 fail("set(size) should throw IndexOutOfBoundsException"); 095 } catch (IndexOutOfBoundsException expected) { 096 } 097 expectUnchanged(); 098 } 099 100 @CollectionSize.Require(absent = ZERO) 101 @ListFeature.Require(absent = SUPPORTS_SET) 102 public void testSet_unsupported() { 103 try { 104 getList().set(aValidIndex(), 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, e3()); 116 fail("set() should throw UnsupportedOperationException or IndexOutOfBoundsException"); 117 } catch (UnsupportedOperationException | IndexOutOfBoundsException expected) { 118 } 119 expectUnchanged(); 120 } 121 122 @CollectionSize.Require(absent = ZERO) 123 @ListFeature.Require(SUPPORTS_SET) 124 @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES) 125 public void testSet_nullUnsupported() { 126 try { 127 getList().set(aValidIndex(), null); 128 fail("set(null) should throw NullPointerException"); 129 } catch (NullPointerException expected) { 130 } 131 expectUnchanged(); 132 } 133 134 private int aValidIndex() { 135 return getList().size() / 2; 136 } 137 138 /** 139 * Returns the {@link java.lang.reflect.Method} instance for {@link #testSet_null()} so that tests 140 * of {@link java.util.Collections#checkedCollection(java.util.Collection, Class)} can suppress it 141 * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 142 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug 6409434</a> is fixed. 143 * It's unclear whether nulls were to be permitted or forbidden, but presumably the eventual fix 144 * will be to permit them, as it seems more likely that code would depend on that behavior than on 145 * the other. Thus, we say the bug is in set(), which fails to support null. 146 */ 147 @J2ktIncompatible 148 @GwtIncompatible // reflection 149 public static Method getSetNullSupportedMethod() { 150 return Helpers.getMethod(ListSetTester.class, "testSet_null"); 151 } 152}