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 java.util.Arrays.asList; 024import static org.junit.Assert.assertThrows; 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; 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 039public class SetCreationTester<E> extends AbstractSetTester<E> { 040 @CollectionFeature.Require(value = ALLOWS_NULL_VALUES, absent = REJECTS_DUPLICATES_AT_CREATION) 041 @CollectionSize.Require(absent = {ZERO, ONE}) 042 public void testCreateWithDuplicates_nullDuplicatesNotRejected() { 043 E[] array = createArrayWithNullElement(); 044 array[0] = null; 045 collection = getSubjectGenerator().create(array); 046 047 List<E> expectedWithDuplicateRemoved = asList(array).subList(1, getNumElements()); 048 expectContents(expectedWithDuplicateRemoved); 049 } 050 051 @CollectionFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) 052 @CollectionSize.Require(absent = {ZERO, ONE}) 053 public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() { 054 E[] array = createSamplesArray(); 055 array[1] = e0(); 056 collection = getSubjectGenerator().create(array); 057 058 List<E> expectedWithDuplicateRemoved = asList(array).subList(1, getNumElements()); 059 expectContents(expectedWithDuplicateRemoved); 060 } 061 062 @CollectionFeature.Require({ALLOWS_NULL_VALUES, REJECTS_DUPLICATES_AT_CREATION}) 063 @CollectionSize.Require(absent = {ZERO, ONE}) 064 public void testCreateWithDuplicates_nullDuplicatesRejected() { 065 E[] array = createArrayWithNullElement(); 066 array[0] = null; 067 assertThrows( 068 IllegalArgumentException.class, () -> collection = getSubjectGenerator().create(array)); 069 } 070 071 @CollectionFeature.Require(REJECTS_DUPLICATES_AT_CREATION) 072 @CollectionSize.Require(absent = {ZERO, ONE}) 073 public void testCreateWithDuplicates_nonNullDuplicatesRejected() { 074 E[] array = createSamplesArray(); 075 array[1] = e0(); 076 assertThrows( 077 IllegalArgumentException.class, () -> collection = getSubjectGenerator().create(array)); 078 } 079}