001/* 002 * Copyright (C) 2008 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.Helpers.copyToList; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.collect.testing.AbstractCollectionTester; 023import java.util.Collection; 024import java.util.List; 025import org.checkerframework.checker.nullness.qual.Nullable; 026import org.junit.Ignore; 027 028/** 029 * Base class for list testers. 030 * 031 * @author George van den Driessche 032 */ 033@GwtCompatible 034@ElementTypesAreNonnullByDefault 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 class AbstractListTester<E extends @Nullable Object> extends AbstractCollectionTester<E> { 039 /* 040 * Previously we had a field named list that was initialized to the value of 041 * collection in setUp(), but that caused problems when a tester changed the 042 * value of list or collection but not both. 043 */ 044 protected final List<E> getList() { 045 return (List<E>) collection; 046 } 047 048 /** 049 * {@inheritDoc} 050 * 051 * <p>The {@code AbstractListTester} implementation overrides {@link 052 * AbstractCollectionTester#expectContents(Collection)} to verify that the order of the elements 053 * in the list under test matches what is expected. 054 */ 055 @Override 056 protected void expectContents(Collection<E> expectedCollection) { 057 List<E> expectedList = copyToList(expectedCollection); 058 // Avoid expectEquals() here to delay reason manufacture until necessary. 059 if (getList().size() != expectedList.size()) { 060 fail("size mismatch: " + reportContext(expectedList)); 061 } 062 for (int i = 0; i < expectedList.size(); i++) { 063 E expected = expectedList.get(i); 064 E actual = getList().get(i); 065 if (expected != actual && (expected == null || !expected.equals(actual))) { 066 fail("mismatch at index " + i + ": " + reportContext(expectedList)); 067 } 068 } 069 } 070 071 /** 072 * Used to delay string formatting until actually required, as it otherwise shows up in the test 073 * execution profile when running an extremely large numbers of tests. 074 */ 075 private String reportContext(List<E> expected) { 076 return Platform.format( 077 "expected collection %s; actual collection %s", expected, this.collection); 078 } 079}