001/* 002 * Copyright (C) 2007 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.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 021import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 022import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 023import static com.google.common.collect.testing.features.CollectionSize.ZERO; 024 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.annotations.GwtIncompatible; 027import com.google.common.collect.testing.AbstractCollectionTester; 028import com.google.common.collect.testing.Helpers; 029import com.google.common.collect.testing.features.CollectionFeature; 030import com.google.common.collect.testing.features.CollectionSize; 031import java.lang.reflect.Method; 032import java.util.ConcurrentModificationException; 033import java.util.Iterator; 034import org.junit.Ignore; 035 036/** 037 * A generic JUnit test which tests {@code add} operations on a collection. Can't be invoked 038 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 039 * 040 * @author Chris Povirk 041 * @author Kevin Bourrillion 042 */ 043@SuppressWarnings("unchecked") // too many "unchecked generic array creations" 044@GwtCompatible(emulated = true) 045@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 046public class CollectionAddTester<E> extends AbstractCollectionTester<E> { 047 @CollectionFeature.Require(SUPPORTS_ADD) 048 public void testAdd_supportedNotPresent() { 049 assertTrue("add(notPresent) should return true", collection.add(e3())); 050 expectAdded(e3()); 051 } 052 053 @CollectionFeature.Require(absent = SUPPORTS_ADD) 054 public void testAdd_unsupportedNotPresent() { 055 try { 056 collection.add(e3()); 057 fail("add(notPresent) should throw"); 058 } catch (UnsupportedOperationException expected) { 059 } 060 expectUnchanged(); 061 expectMissing(e3()); 062 } 063 064 @CollectionFeature.Require(absent = SUPPORTS_ADD) 065 @CollectionSize.Require(absent = ZERO) 066 public void testAdd_unsupportedPresent() { 067 try { 068 assertFalse("add(present) should return false or throw", collection.add(e0())); 069 } catch (UnsupportedOperationException tolerated) { 070 } 071 expectUnchanged(); 072 } 073 074 @CollectionFeature.Require( 075 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, 076 absent = RESTRICTS_ELEMENTS 077 ) 078 public void testAdd_nullSupported() { 079 assertTrue("add(null) should return true", collection.add(null)); 080 expectAdded((E) null); 081 } 082 083 @CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES) 084 public void testAdd_nullUnsupported() { 085 try { 086 collection.add(null); 087 fail("add(null) should throw"); 088 } catch (NullPointerException expected) { 089 } 090 expectUnchanged(); 091 expectNullMissingWhenNullUnsupported("Should not contain null after unsupported add(null)"); 092 } 093 094 @CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) 095 @CollectionSize.Require(absent = ZERO) 096 public void testAddConcurrentWithIteration() { 097 try { 098 Iterator<E> iterator = collection.iterator(); 099 assertTrue(collection.add(e3())); 100 iterator.next(); 101 fail("Expected ConcurrentModificationException"); 102 } catch (ConcurrentModificationException expected) { 103 // success 104 } 105 } 106 107 /** 108 * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of 109 * {@link java.util.Collections#checkedCollection(java.util.Collection, Class)} can suppress it 110 * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a 111 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug 6409434</a> is fixed. 112 * It's unclear whether nulls were to be permitted or forbidden, but presumably the eventual fix 113 * will be to permit them, as it seems more likely that code would depend on that behavior than on 114 * the other. Thus, we say the bug is in add(), which fails to support null. 115 */ 116 @GwtIncompatible // reflection 117 public static Method getAddNullSupportedMethod() { 118 return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullSupported"); 119 } 120 121 /** 122 * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so that tests of 123 * {@link java.util.TreeSet} can suppress it with {@code 124 * FeatureSpecificTestSuiteBuilder.suppressing()} until <a 125 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed. 126 */ 127 @GwtIncompatible // reflection 128 public static Method getAddNullUnsupportedMethod() { 129 return Helpers.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported"); 130 } 131 132 /** 133 * Returns the {@link Method} instance for {@link #testAdd_unsupportedNotPresent()} so that tests 134 * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} while we figure out 135 * what to do with <a href="http://goo.gl/qJBruX">{@code ConcurrentHashMap} support for {@code 136 * entrySet().add()}</a>. 137 */ 138 @GwtIncompatible // reflection 139 public static Method getAddUnsupportedNotPresentMethod() { 140 return Helpers.getMethod(CollectionAddTester.class, "testAdd_unsupportedNotPresent"); 141 } 142}