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