SetAddTester.java revision dbd967a6e5c96cc1a97c5521f88dc1564ba2f81b
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
21import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22
23import com.google.common.collect.testing.features.CollectionFeature;
24import com.google.common.collect.testing.features.CollectionSize;
25
26import java.lang.reflect.Method;
27
28/**
29 * A generic JUnit test which tests add operations on a set. Can't be
30 * invoked directly; please see
31 * {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
32 *
33 * <p>This class is GWT compatible.
34 *
35 * @author Kevin Bourrillion
36 */
37public class SetAddTester<E> extends AbstractSetTester<E> {
38  @CollectionFeature.Require(SUPPORTS_ADD)
39  @CollectionSize.Require(absent = ZERO)
40  public void testAdd_supportedPresent() {
41    assertFalse("add(present) should return false", getSet().add(samples.e0));
42    expectUnchanged();
43  }
44
45  @CollectionFeature.Require(value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES})
46  @CollectionSize.Require(absent = ZERO)
47  public void testAdd_supportedNullPresent() {
48    E[] array = createArrayWithNullElement();
49    collection = getSubjectGenerator().create(array);
50    assertFalse("add(nullPresent) should return false", getSet().add(null));
51    expectContents(array);
52  }
53
54  /**
55   * Returns the {@link Method} instance for
56   * {@link #testAdd_supportedNullPresent()} so that tests can suppress it. See
57   * {@link CollectionAddTester#getAddNullSupportedMethod()} for details.
58   */
59  public static Method getAddSupportedNullPresentMethod() {
60    return Platform.getMethod(SetAddTester.class, "testAdd_supportedNullPresent");
61  }
62}
63