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