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.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.WrongType;
24  import com.google.common.collect.testing.features.CollectionFeature;
25  import com.google.common.collect.testing.features.CollectionSize;
26  
27  /**
28   * Common parent class for {@link ListIndexOfTester} and
29   * {@link ListLastIndexOfTester}.
30   *
31   * @author Chris Povirk
32   */
33  @GwtCompatible
34  public abstract class AbstractListIndexOfTester<E> extends AbstractListTester<E> {
35    /** Override to call {@code indexOf()} or {@code lastIndexOf()}. */
36    protected abstract int find(Object o);
37  
38    /**
39     * Override to return "indexOf" or "lastIndexOf()" for use in failure
40     * messages.
41     */
42    protected abstract String getMethodName();
43  
44    @CollectionSize.Require(absent = ZERO)
45    public void testFind_yes() {
46      assertEquals(
47          getMethodName() + "(firstElement) should return 0", 0, find(getOrderedElements().get(0)));
48    }
49  
50    public void testFind_no() {
51      assertEquals(getMethodName() + "(notPresent) should return -1", -1, find(e3()));
52    }
53  
54    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
55    public void testFind_nullNotContainedButSupported() {
56      assertEquals(getMethodName() + "(nullNotPresent) should return -1", -1, find(null));
57    }
58  
59    @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
60    public void testFind_nullNotContainedAndUnsupported() {
61      try {
62        assertEquals(getMethodName() + "(nullNotPresent) should return -1 or throw", -1, find(null));
63      } catch (NullPointerException tolerated) {
64      }
65    }
66  
67    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
68    @CollectionSize.Require(absent = ZERO)
69    public void testFind_nonNullWhenNullContained() {
70      initCollectionWithNullElement();
71      assertEquals(getMethodName() + "(notPresent) should return -1", -1, find(e3()));
72    }
73  
74    @CollectionFeature.Require(ALLOWS_NULL_VALUES)
75    @CollectionSize.Require(absent = ZERO)
76    public void testFind_nullContained() {
77      initCollectionWithNullElement();
78      assertEquals(
79          getMethodName() + "(null) should return " + getNullLocation(),
80          getNullLocation(),
81          find(null));
82    }
83  
84    public void testFind_wrongType() {
85      try {
86        assertEquals(
87            getMethodName() + "(wrongType) should return -1 or throw", -1, find(WrongType.VALUE));
88      } catch (ClassCastException tolerated) {
89      }
90    }
91  }