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