001/*
002 * Copyright (C) 2008 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.Helpers.getMethod;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.annotations.GwtIncompatible;
026import com.google.common.annotations.J2ktIncompatible;
027import com.google.common.collect.testing.AbstractCollectionTester;
028import com.google.common.collect.testing.features.CollectionFeature;
029import com.google.common.collect.testing.features.CollectionSize;
030import java.lang.reflect.Method;
031import org.junit.Ignore;
032
033/**
034 * A generic JUnit test which tests creation (typically through a constructor or static factory
035 * method) of a collection. Can't be invoked directly; please see {@link
036 * com.google.common.collect.testing.CollectionTestSuiteBuilder}.
037 *
038 * @author Chris Povirk
039 */
040@GwtCompatible(emulated = true)
041@Ignore("test runners must not instantiate and run this directly, only via suites we build")
042// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
043@SuppressWarnings("JUnit4ClassUsedInJUnit3")
044public class CollectionCreationTester<E> extends AbstractCollectionTester<E> {
045  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
046  @CollectionSize.Require(absent = ZERO)
047  public void testCreateWithNull_supported() {
048    E[] array = createArrayWithNullElement();
049    collection = getSubjectGenerator().create(array);
050    expectContents(array);
051  }
052
053  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
054  @CollectionSize.Require(absent = ZERO)
055  public void testCreateWithNull_unsupported() {
056    E[] array = createArrayWithNullElement();
057
058    assertThrows(
059        NullPointerException.class,
060        () -> {
061          Object unused = getSubjectGenerator().create(array);
062        });
063  }
064
065  /**
066   * Returns the {@link Method} instance for {@link #testCreateWithNull_unsupported()} so that tests
067   * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
068   * href="https://bugs.openjdk.org/browse/JDK-5045147">JDK-5045147</a> is fixed.
069   */
070  @J2ktIncompatible
071  @GwtIncompatible // reflection
072  public static Method getCreateWithNullUnsupportedMethod() {
073    return getMethod(CollectionCreationTester.class, "testCreateWithNull_unsupported");
074  }
075}