View Javadoc
1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  import static com.google.common.collect.testing.features.CollectionFeature.REJECTS_DUPLICATES_AT_CREATION;
21  import static com.google.common.collect.testing.features.CollectionSize.ONE;
22  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23  
24  import com.google.common.annotations.GwtCompatible;
25  import com.google.common.collect.testing.features.CollectionFeature;
26  import com.google.common.collect.testing.features.CollectionSize;
27  import java.util.Arrays;
28  import java.util.List;
29  
30  /**
31   * A generic JUnit test which tests creation (typically through a constructor or
32   * static factory method) of a set. Can't be invoked directly; please see
33   * {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
34   *
35   * @author Chris Povirk
36   */
37  @GwtCompatible
38  public class SetCreationTester<E> extends AbstractSetTester<E> {
39    @CollectionFeature.Require(value = ALLOWS_NULL_VALUES, absent = REJECTS_DUPLICATES_AT_CREATION)
40    @CollectionSize.Require(absent = {ZERO, ONE})
41    public void testCreateWithDuplicates_nullDuplicatesNotRejected() {
42      E[] array = createArrayWithNullElement();
43      array[0] = null;
44      collection = getSubjectGenerator().create(array);
45  
46      List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements());
47      expectContents(expectedWithDuplicateRemoved);
48    }
49  
50    @CollectionFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION)
51    @CollectionSize.Require(absent = {ZERO, ONE})
52    public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() {
53      E[] array = createSamplesArray();
54      array[1] = e0();
55      collection = getSubjectGenerator().create(array);
56  
57      List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements());
58      expectContents(expectedWithDuplicateRemoved);
59    }
60  
61    @CollectionFeature.Require({ALLOWS_NULL_VALUES, REJECTS_DUPLICATES_AT_CREATION})
62    @CollectionSize.Require(absent = {ZERO, ONE})
63    public void testCreateWithDuplicates_nullDuplicatesRejected() {
64      E[] array = createArrayWithNullElement();
65      array[0] = null;
66      try {
67        collection = getSubjectGenerator().create(array);
68        fail("Should reject duplicate null elements at creation");
69      } catch (IllegalArgumentException expected) {
70      }
71    }
72  
73    @CollectionFeature.Require(REJECTS_DUPLICATES_AT_CREATION)
74    @CollectionSize.Require(absent = {ZERO, ONE})
75    public void testCreateWithDuplicates_nonNullDuplicatesRejected() {
76      E[] array = createSamplesArray();
77      array[1] = e0();
78      try {
79        collection = getSubjectGenerator().create(array);
80        fail("Should reject duplicate non-null elements at creation");
81      } catch (IllegalArgumentException expected) {
82      }
83    }
84  }