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.CollectionFeature.REJECTS_DUPLICATES_AT_CREATION;
021import static com.google.common.collect.testing.features.CollectionSize.ONE;
022import static com.google.common.collect.testing.features.CollectionSize.ZERO;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.features.CollectionFeature;
026import com.google.common.collect.testing.features.CollectionSize;
027import java.util.Arrays;
028import java.util.List;
029import org.junit.Ignore;
030
031/**
032 * A generic JUnit test which tests creation (typically through a constructor or static factory
033 * method) of a set. Can't be invoked directly; please see {@link
034 * com.google.common.collect.testing.SetTestSuiteBuilder}.
035 *
036 * @author Chris Povirk
037 */
038@GwtCompatible
039@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
040@SuppressWarnings("JUnit4ClassUsedInJUnit3")
041public class SetCreationTester<E> extends AbstractSetTester<E> {
042  @CollectionFeature.Require(value = ALLOWS_NULL_VALUES, absent = REJECTS_DUPLICATES_AT_CREATION)
043  @CollectionSize.Require(absent = {ZERO, ONE})
044  public void testCreateWithDuplicates_nullDuplicatesNotRejected() {
045    E[] array = createArrayWithNullElement();
046    array[0] = null;
047    collection = getSubjectGenerator().create(array);
048
049    List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements());
050    expectContents(expectedWithDuplicateRemoved);
051  }
052
053  @CollectionFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION)
054  @CollectionSize.Require(absent = {ZERO, ONE})
055  public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() {
056    E[] array = createSamplesArray();
057    array[1] = e0();
058    collection = getSubjectGenerator().create(array);
059
060    List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements());
061    expectContents(expectedWithDuplicateRemoved);
062  }
063
064  @CollectionFeature.Require({ALLOWS_NULL_VALUES, REJECTS_DUPLICATES_AT_CREATION})
065  @CollectionSize.Require(absent = {ZERO, ONE})
066  public void testCreateWithDuplicates_nullDuplicatesRejected() {
067    E[] array = createArrayWithNullElement();
068    array[0] = null;
069    try {
070      collection = getSubjectGenerator().create(array);
071      fail("Should reject duplicate null elements at creation");
072    } catch (IllegalArgumentException expected) {
073    }
074  }
075
076  @CollectionFeature.Require(REJECTS_DUPLICATES_AT_CREATION)
077  @CollectionSize.Require(absent = {ZERO, ONE})
078  public void testCreateWithDuplicates_nonNullDuplicatesRejected() {
079    E[] array = createSamplesArray();
080    array[1] = e0();
081    try {
082      collection = getSubjectGenerator().create(array);
083      fail("Should reject duplicate non-null elements at creation");
084    } catch (IllegalArgumentException expected) {
085    }
086  }
087}