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.SUPPORTS_ADD; 021import static com.google.common.collect.testing.features.CollectionSize.ZERO; 022 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.annotations.GwtIncompatible; 025import com.google.common.collect.testing.Helpers; 026import com.google.common.collect.testing.features.CollectionFeature; 027import com.google.common.collect.testing.features.CollectionSize; 028import java.lang.reflect.Method; 029 030/** 031 * A generic JUnit test which tests add operations on a set. Can't be invoked directly; please see 032 * {@link com.google.common.collect.testing.SetTestSuiteBuilder}. 033 * 034 * @author Kevin Bourrillion 035 */ 036@GwtCompatible(emulated = true) 037public class SetAddTester<E> extends AbstractSetTester<E> { 038 @CollectionFeature.Require(SUPPORTS_ADD) 039 @CollectionSize.Require(absent = ZERO) 040 public void testAdd_supportedPresent() { 041 assertFalse("add(present) should return false", getSet().add(e0())); 042 expectUnchanged(); 043 } 044 045 @CollectionFeature.Require(value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}) 046 @CollectionSize.Require(absent = ZERO) 047 public void testAdd_supportedNullPresent() { 048 E[] array = createArrayWithNullElement(); 049 collection = getSubjectGenerator().create(array); 050 assertFalse("add(nullPresent) should return false", getSet().add(null)); 051 expectContents(array); 052 } 053 054 /** 055 * Returns the {@link Method} instance for {@link #testAdd_supportedNullPresent()} so that tests 056 * can suppress it. See {@link CollectionAddTester#getAddNullSupportedMethod()} for details. 057 */ 058 @GwtIncompatible // reflection 059 public static Method getAddSupportedNullPresentMethod() { 060 return Helpers.getMethod(SetAddTester.class, "testAdd_supportedNullPresent"); 061 } 062}