1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; 20 import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS; 21 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD; 22 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 23 24 import com.google.common.collect.testing.AbstractCollectionTester; 25 import com.google.common.collect.testing.features.CollectionFeature; 26 import com.google.common.collect.testing.features.CollectionSize; 27 28 import java.lang.reflect.Method; 29 30 /** 31 * A generic JUnit test which tests {@code add} operations on a collection. 32 * Can't be invoked directly; please see 33 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}. 34 * 35 * <p>This class is GWT compatible. 36 * 37 * @author Chris Povirk 38 * @author Kevin Bourrillion 39 */ 40 @SuppressWarnings("unchecked") // too many "unchecked generic array creations" 41 public class CollectionAddTester<E> extends AbstractCollectionTester<E> { 42 @CollectionFeature.Require(SUPPORTS_ADD) 43 public void testAdd_supportedNotPresent() { 44 assertTrue("add(notPresent) should return true", 45 collection.add(samples.e3)); 46 expectAdded(samples.e3); 47 } 48 49 @CollectionFeature.Require(absent = SUPPORTS_ADD) 50 public void testAdd_unsupportedNotPresent() { 51 try { 52 collection.add(samples.e3); 53 fail("add(notPresent) should throw"); 54 } catch (UnsupportedOperationException expected) { 55 } 56 expectUnchanged(); 57 expectMissing(samples.e3); 58 } 59 60 @CollectionFeature.Require(absent = SUPPORTS_ADD) 61 @CollectionSize.Require(absent = ZERO) 62 public void testAdd_unsupportedPresent() { 63 try { 64 assertFalse("add(present) should return false or throw", 65 collection.add(samples.e0)); 66 } catch (UnsupportedOperationException tolerated) { 67 } 68 expectUnchanged(); 69 } 70 71 @CollectionFeature.Require( 72 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}, 73 absent = RESTRICTS_ELEMENTS) 74 public void testAdd_nullSupported() { 75 assertTrue("add(null) should return true", collection.add(null)); 76 expectAdded((E) null); 77 } 78 79 @CollectionFeature.Require(value = SUPPORTS_ADD, 80 absent = ALLOWS_NULL_VALUES) 81 public void testAdd_nullUnsupported() { 82 try { 83 collection.add(null); 84 fail("add(null) should throw"); 85 } catch (NullPointerException expected) { 86 } 87 expectUnchanged(); 88 expectNullMissingWhenNullUnsupported( 89 "Should not contain null after unsupported add(null)"); 90 } 91 92 /** 93 * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so 94 * that tests of {@link 95 * java.util.Collections#checkedCollection(java.util.Collection, Class)} can 96 * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} 97 * until <a 98 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6409434">Sun bug 99 * 6409434</a> is fixed. It's unclear whether nulls were to be permitted or 100 * forbidden, but presumably the eventual fix will be to permit them, as it 101 * seems more likely that code would depend on that behavior than on the 102 * other. Thus, we say the bug is in add(), which fails to support null. 103 */ 104 public static Method getAddNullSupportedMethod() { 105 return Platform.getMethod(CollectionAddTester.class, "testAdd_nullSupported"); 106 } 107 108 /** 109 * Returns the {@link Method} instance for {@link #testAdd_nullSupported()} so 110 * that tests of {@link 111 * java.util.Collections#checkedCollection(java.util.Collection, Class)} can 112 * suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} 113 * until <a 114 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun 115 * bug 5045147</a> is fixed. 116 */ 117 public static Method getAddNullUnsupportedMethod() { 118 return Platform.getMethod(CollectionAddTester.class, "testAdd_nullUnsupported"); 119 } 120 } 121