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.features.CollectionFeature.ALLOWS_NULL_VALUES;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.collect.testing.AbstractCollectionTester;
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 creation (typically through a constructor or static factory
032 * method) of a collection. Can't be invoked directly; please see {@link
033 * com.google.common.collect.testing.CollectionTestSuiteBuilder}.
034 *
035 * @author Chris Povirk
036 */
037@GwtCompatible(emulated = true)
038public class CollectionCreationTester<E> extends AbstractCollectionTester<E> {
039  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
040  @CollectionSize.Require(absent = ZERO)
041  public void testCreateWithNull_supported() {
042    E[] array = createArrayWithNullElement();
043    collection = getSubjectGenerator().create(array);
044    expectContents(array);
045  }
046
047  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
048  @CollectionSize.Require(absent = ZERO)
049  public void testCreateWithNull_unsupported() {
050    E[] array = createArrayWithNullElement();
051
052    try {
053      getSubjectGenerator().create(array);
054      fail("Creating a collection containing null should fail");
055    } catch (NullPointerException expected) {
056    }
057  }
058
059  /**
060   * Returns the {@link Method} instance for {@link #testCreateWithNull_unsupported()} so that tests
061   * can suppress it with {@code FeatureSpecificTestSuiteBuilder.suppressing()} until <a
062   * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147">Sun bug 5045147</a> is fixed.
063   */
064  @GwtIncompatible // reflection
065  public static Method getCreateWithNullUnsupportedMethod() {
066    return Helpers.getMethod(CollectionCreationTester.class, "testCreateWithNull_unsupported");
067  }
068}