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. 040public class SetCreationTester<E> extends AbstractSetTester<E> { 041 @CollectionFeature.Require(value = ALLOWS_NULL_VALUES, absent = REJECTS_DUPLICATES_AT_CREATION) 042 @CollectionSize.Require(absent = {ZERO, ONE}) 043 public void testCreateWithDuplicates_nullDuplicatesNotRejected() { 044 E[] array = createArrayWithNullElement(); 045 array[0] = null; 046 collection = getSubjectGenerator().create(array); 047 048 List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements()); 049 expectContents(expectedWithDuplicateRemoved); 050 } 051 052 @CollectionFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) 053 @CollectionSize.Require(absent = {ZERO, ONE}) 054 public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() { 055 E[] array = createSamplesArray(); 056 array[1] = e0(); 057 collection = getSubjectGenerator().create(array); 058 059 List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements()); 060 expectContents(expectedWithDuplicateRemoved); 061 } 062 063 @CollectionFeature.Require({ALLOWS_NULL_VALUES, REJECTS_DUPLICATES_AT_CREATION}) 064 @CollectionSize.Require(absent = {ZERO, ONE}) 065 public void testCreateWithDuplicates_nullDuplicatesRejected() { 066 E[] array = createArrayWithNullElement(); 067 array[0] = null; 068 try { 069 collection = getSubjectGenerator().create(array); 070 fail("Should reject duplicate null elements at creation"); 071 } catch (IllegalArgumentException expected) { 072 } 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 try { 081 collection = getSubjectGenerator().create(array); 082 fail("Should reject duplicate non-null elements at creation"); 083 } catch (IllegalArgumentException expected) { 084 } 085 } 086}