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