Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/testers/AbstractListTester.java

raw
1 /*
2 * Copyright (C) 2008 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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.collect.testing.AbstractCollectionTester;
21 import com.google.common.collect.testing.Helpers;
22 import java.util.Collection;
23 import java.util.List;
24 import org.junit.Ignore;
25
26 /**
27 * Base class for list testers.
28 *
29 * @author George van den Driessche
30 */
31 @GwtCompatible
32 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
33 public class AbstractListTester<E> extends AbstractCollectionTester<E> {
34 /*
35 * Previously we had a field named list that was initialized to the value of
36 * collection in setUp(), but that caused problems when a tester changed the
37 * value of list or collection but not both.
38 */
39 protected final List<E> getList() {
40 return (List<E>) collection;
41 }
42
43 /**
44 * {@inheritDoc}
45 *
46 * <p>The {@code AbstractListTester} implementation overrides {@link
47 * AbstractCollectionTester#expectContents(Collection)} to verify that the order of the elements
48 * in the list under test matches what is expected.
49 */
50 @Override
51 protected void expectContents(Collection<E> expectedCollection) {
52 List<E> expectedList = Helpers.copyToList(expectedCollection);
53 // Avoid expectEquals() here to delay reason manufacture until necessary.
54 if (getList().size() != expectedList.size()) {
55 fail("size mismatch: " + reportContext(expectedList));
56 }
57 for (int i = 0; i < expectedList.size(); i++) {
58 E expected = expectedList.get(i);
59 E actual = getList().get(i);
60 if (expected != actual && (expected == null || !expected.equals(actual))) {
61 fail("mismatch at index " + i + ": " + reportContext(expectedList));
62 }
63 }
64 }
65
66 /**
67 * Used to delay string formatting until actually required, as it otherwise shows up in the test
68 * execution profile when running an extremely large numbers of tests.
69 */
70 private String reportContext(List<E> expected) {
71 return Platform.format(
72 "expected collection %s; actual collection %s", expected, this.collection);
73 }
74 }