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;
18
19import java.util.Collection;
20
21/**
22 * Base class for collection testers.
23 *
24 * <p>This class is GWT compatible.
25 *
26 * @param <E> the element type of the collection to be tested.
27 *
28 * @author Kevin Bourrillion
29 */
30public abstract class AbstractCollectionTester<E>
31    extends AbstractContainerTester<Collection<E>, E> {
32
33  // TODO: replace this with an accessor.
34  protected Collection<E> collection;
35
36  @Override protected Collection<E> actualContents() {
37    return collection;
38  }
39
40  // TODO: dispose of this once collection is encapsulated.
41  @Override protected Collection<E> resetContainer(Collection<E> newContents) {
42    collection = super.resetContainer(newContents);
43    return collection;
44  }
45
46  /** @see AbstractContainerTester#resetContainer() */
47  protected void resetCollection() {
48    resetContainer();
49  }
50
51  /**
52   * @return an array of the proper size with {@code null} inserted into the
53   * middle element.
54   */
55  protected E[] createArrayWithNullElement() {
56    E[] array = createSamplesArray();
57    array[getNullLocation()] = null;
58    return array;
59  }
60
61  protected void initCollectionWithNullElement() {
62    E[] array = createArrayWithNullElement();
63    resetContainer(getSubjectGenerator().create(array));
64  }
65
66  /**
67   * Equivalent to {@link #expectMissing(Object[]) expectMissing}{@code (null)}
68   * except that the call to {@code contains(null)} is permitted to throw a
69   * {@code NullPointerException}.
70   *
71   * @param message message to use upon assertion failure
72   */
73  protected void expectNullMissingWhenNullUnsupported(String message) {
74    try {
75      assertFalse(message, actualContents().contains(null));
76    } catch (NullPointerException tolerated) {
77      // Tolerated
78    }
79  }
80}
81