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