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