11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect.testing.testers; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.collect.testing.features.CollectionSize.ZERO; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.testing.AbstractCollectionTester; 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.testing.features.CollectionFeature; 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.testing.features.CollectionSize; 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.lang.reflect.Method; 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * A generic JUnit test which tests {@code add} operations on a collection. 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Can't be invoked directly; please see 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>This class is GWT compatible. 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Chris Povirk 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Kevin Bourrillion 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@SuppressWarnings("unchecked") // too many "unchecked generic array creations" 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class CollectionAddTester<E> extends AbstractCollectionTester<E> { 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @CollectionFeature.Require(SUPPORTS_ADD) 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAdd_supportedNotPresent() { 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue("add(notPresent) should return true", 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert collection.add(samples.e3)); 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectAdded(samples.e3); 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @CollectionFeature.Require(absent = SUPPORTS_ADD) 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAdd_unsupportedNotPresent() { 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert collection.add(samples.e3); 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail("add(notPresent) should throw"); 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (UnsupportedOperationException expected) { 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectUnchanged(); 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectMissing(samples.e3); 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @CollectionFeature.Require(absent = SUPPORTS_ADD) 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @CollectionSize.Require(absent = ZERO) 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAdd_unsupportedPresent() { 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertFalse("add(present) should return false or throw", 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert collection.add(samples.e0)); 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (UnsupportedOperationException tolerated) { 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectUnchanged(); 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @CollectionFeature.Require( 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert absent = RESTRICTS_ELEMENTS) 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAdd_nullSupported() { 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue("add(null) should return true", collection.add(null)); 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectAdded((E) null); 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @CollectionFeature.Require(value = SUPPORTS_ADD, 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert absent = ALLOWS_NULL_VALUES) 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAdd_nullUnsupported() { 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert collection.add(null); 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail("add(null) should throw"); 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (NullPointerException expected) { 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectUnchanged(); 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert expectNullMissingWhenNullUnsupported( 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Should not contain null after unsupported add(null)"); 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * that tests of {@link 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * java.util.Collections#checkedCollection(java.util.Collection, Class)} can 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * until <a 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 6409434</a> is fixed. It's unclear whether nulls were to be permitted or 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * forbidden, but presumably the eventual fix will be to permit them, as it 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * seems more likely that code would depend on that behavior than on the 1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * other. Thus, we say the bug is in add(), which fails to support null. 1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static Method getAddNullSupportedMethod() { 1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Platform.getMethod(CollectionAddTester.class, "testAdd_nullSupported"); 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so 1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * that tests of {@link 1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * java.util.Collections#checkedCollection(java.util.Collection, Class)} can 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} 1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * until <a 1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun 1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * bug 5045147</a> is fixed. 1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static Method getAddNullUnsupportedMethod() { 1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Platform.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported"); 1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 121