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