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