CollectionCreationTester.java revision 3c77433663281544363151bf284b0240dfd22a42
1/*
2 * Copyright (C) 2008 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.CollectionSize.ZERO;
21
22import com.google.common.annotations.GwtCompatible;
23import com.google.common.annotations.GwtIncompatible;
24import com.google.common.collect.testing.AbstractCollectionTester;
25import com.google.common.collect.testing.Helpers;
26import com.google.common.collect.testing.features.CollectionFeature;
27import com.google.common.collect.testing.features.CollectionSize;
28
29import java.lang.reflect.Method;
30
31/**
32 * A generic JUnit test which tests creation (typically through a constructor or
33 * static factory method) of a collection. Can't be invoked directly; please see
34 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
35 *
36 * <p>This class is GWT compatible.
37 *
38 * @author Chris Povirk
39 */
40@GwtCompatible(emulated = true)
41public class CollectionCreationTester<E> extends AbstractCollectionTester<E> {
42  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
43  @CollectionSize.Require(absent = ZERO)
44  public void testCreateWithNull_supported() {
45    E[] array = createArrayWithNullElement();
46    collection = getSubjectGenerator().create(array);
47    expectContents(array);
48  }
49
50  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
51  @CollectionSize.Require(absent = ZERO)
52  public void testCreateWithNull_unsupported() {
53    E[] array = createArrayWithNullElement();
54
55    try {
56      getSubjectGenerator().create(array);
57      fail("Creating a collection containing null should fail");
58    } catch (NullPointerException expected) {
59    }
60  }
61
62  /**
63   * Returns the {@link Method} instance for {@link
64   * #testCreateWithNull_unsupported()} so that tests can suppress it
65   * with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
66   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun
67   * bug 5045147</a> is fixed.
68   */
69  @GwtIncompatible("reflection")
70  public static Method getCreateWithNullUnsupportedMethod() {
71    return Helpers.getMethod(CollectionCreationTester.class, "testCreateWithNull_unsupported");
72  }
73}
74