001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
020import static com.google.common.collect.testing.features.CollectionSize.ZERO;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.collect.testing.WrongType;
024import com.google.common.collect.testing.features.CollectionFeature;
025import com.google.common.collect.testing.features.CollectionSize;
026import org.jspecify.annotations.Nullable;
027import org.junit.Ignore;
028
029/**
030 * Common parent class for {@link ListIndexOfTester} and {@link ListLastIndexOfTester}.
031 *
032 * @author Chris Povirk
033 */
034@GwtCompatible
035@Ignore("test runners must not instantiate and run this directly, only via suites we build")
036// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
037@SuppressWarnings("JUnit4ClassUsedInJUnit3")
038public abstract class AbstractListIndexOfTester<E> extends AbstractListTester<E> {
039  /** Override to call {@code indexOf()} or {@code lastIndexOf()}. */
040  protected abstract int find(@Nullable Object o);
041
042  /** Override to return "indexOf" or "lastIndexOf()" for use in failure messages. */
043  protected abstract String getMethodName();
044
045  @CollectionSize.Require(absent = ZERO)
046  public void testFind_yes() {
047    assertEquals(
048        getMethodName() + "(firstElement) should return 0", 0, find(getOrderedElements().get(0)));
049  }
050
051  public void testFind_no() {
052    assertEquals(getMethodName() + "(notPresent) should return -1", -1, find(e3()));
053  }
054
055  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
056  public void testFind_nullNotContainedButSupported() {
057    assertEquals(getMethodName() + "(nullNotPresent) should return -1", -1, find(null));
058  }
059
060  @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
061  public void testFind_nullNotContainedAndUnsupported() {
062    try {
063      assertEquals(getMethodName() + "(nullNotPresent) should return -1 or throw", -1, find(null));
064    } catch (NullPointerException tolerated) {
065    }
066  }
067
068  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
069  @CollectionSize.Require(absent = ZERO)
070  public void testFind_nonNullWhenNullContained() {
071    initCollectionWithNullElement();
072    assertEquals(getMethodName() + "(notPresent) should return -1", -1, find(e3()));
073  }
074
075  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
076  @CollectionSize.Require(absent = ZERO)
077  public void testFind_nullContained() {
078    initCollectionWithNullElement();
079    assertEquals(
080        getMethodName() + "(null) should return " + getNullLocation(),
081        getNullLocation(),
082        find(null));
083  }
084
085  public void testFind_wrongType() {
086    try {
087      assertEquals(
088          getMethodName() + "(wrongType) should return -1 or throw", -1, find(WrongType.VALUE));
089    } catch (ClassCastException tolerated) {
090    }
091  }
092}