1/*
2 * Copyright (C) 2007 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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24
25import com.google.common.annotations.GwtCompatible;
26import com.google.common.collect.testing.AbstractCollectionTester;
27import com.google.common.collect.testing.features.CollectionFeature;
28import com.google.common.collect.testing.features.CollectionSize;
29
30import java.util.ConcurrentModificationException;
31import java.util.Iterator;
32
33/**
34 * A generic JUnit test which tests {@code add} operations on a collection.
35 * Can't be invoked directly; please see
36 * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
37 *
38 * @author Chris Povirk
39 * @author Kevin Bourrillion
40 */
41@SuppressWarnings("unchecked") // too many "unchecked generic array creations"
42@GwtCompatible(emulated = true)
43public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
44  @CollectionFeature.Require(SUPPORTS_ADD)
45  public void testAdd_supportedNotPresent() {
46    assertTrue("add(notPresent) should return true",
47        collection.add(samples.e3));
48    expectAdded(samples.e3);
49  }
50
51  @CollectionFeature.Require(absent = SUPPORTS_ADD)
52  public void testAdd_unsupportedNotPresent() {
53    try {
54      collection.add(samples.e3);
55      fail("add(notPresent) should throw");
56    } catch (UnsupportedOperationException expected) {
57    }
58    expectUnchanged();
59    expectMissing(samples.e3);
60  }
61
62  @CollectionFeature.Require(absent = SUPPORTS_ADD)
63  @CollectionSize.Require(absent = ZERO)
64  public void testAdd_unsupportedPresent() {
65    try {
66      assertFalse("add(present) should return false or throw",
67          collection.add(samples.e0));
68    } catch (UnsupportedOperationException tolerated) {
69    }
70    expectUnchanged();
71  }
72
73  @CollectionFeature.Require(
74      value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
75      absent = RESTRICTS_ELEMENTS)
76  public void testAdd_nullSupported() {
77    assertTrue("add(null) should return true", collection.add(null));
78    expectAdded((E) null);
79  }
80
81  @CollectionFeature.Require(value = SUPPORTS_ADD,
82      absent = ALLOWS_NULL_VALUES)
83  public void testAdd_nullUnsupported() {
84    try {
85      collection.add(null);
86      fail("add(null) should throw");
87    } catch (NullPointerException expected) {
88    }
89    expectUnchanged();
90    expectNullMissingWhenNullUnsupported(
91        "Should not contain null after unsupported add(null)");
92  }
93
94  @CollectionFeature.Require({SUPPORTS_ADD,
95      FAILS_FAST_ON_CONCURRENT_MODIFICATION})
96  @CollectionSize.Require(absent = ZERO)
97  public void testAddConcurrentWithIteration() {
98    try {
99      Iterator<E> iterator = collection.iterator();
100      assertTrue(collection.add(samples.e3));
101      iterator.next();
102      fail("Expected ConcurrentModificationException");
103    } catch (ConcurrentModificationException expected) {
104      // success
105    }
106  }
107}
108
109