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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20
21import com.google.common.collect.testing.MinimalSet;
22import com.google.common.collect.testing.features.CollectionFeature;
23import com.google.common.collect.testing.features.CollectionSize;
24
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.List;
28
29/**
30 * Tests {@link List#equals}.
31 *
32 * <p>This class is GWT compatible.
33 *
34 * @author George van den Driessche
35 */
36public class ListEqualsTester<E> extends AbstractListTester<E> {
37  public void testEquals_otherListWithSameElements() {
38    assertTrue(
39        "A List should equal any other List containing the same elements.",
40        getList().equals(new ArrayList<E>(getSampleElements())));
41  }
42
43  @CollectionSize.Require(absent = CollectionSize.ZERO)
44  public void testEquals_otherListWithDifferentElements() {
45    ArrayList<E> other = new ArrayList<E>(getSampleElements());
46    other.set(other.size() / 2, getSubjectGenerator().samples().e3);
47    assertFalse(
48        "A List should not equal another List containing different elements.",
49        getList().equals(other));
50  }
51
52  @CollectionSize.Require(absent = CollectionSize.ZERO)
53  public void testEquals_otherListContainingNull() {
54    List<E> other = new ArrayList<E>(getSampleElements());
55    other.set(other.size() / 2, null);
56    assertFalse("Two Lists should not be equal if exactly one of them has "
57        + "null at a given index.",
58        getList().equals(other));
59  }
60
61  @CollectionSize.Require(absent = CollectionSize.ZERO)
62  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
63  public void testEquals_containingNull() {
64    ArrayList<E> elements = new ArrayList<E>(getSampleElements());
65    elements.set(elements.size() / 2, null);
66    collection = getSubjectGenerator().create(elements.toArray());
67    List<E> other = new ArrayList<E>(getSampleElements());
68    assertFalse("Two Lists should not be equal if exactly one of them has "
69        + "null at a given index.",
70        getList().equals(other));
71  }
72
73  @CollectionSize.Require(absent = CollectionSize.ZERO)
74  public void testEquals_shorterList() {
75    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
76    assertFalse("Lists of different sizes should not be equal.",
77        getList().equals(new ArrayList<E>(fewerElements)));
78  }
79
80  public void testEquals_longerList() {
81    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
82    assertFalse("Lists of different sizes should not be equal.",
83        getList().equals(new ArrayList<E>(moreElements)));
84  }
85
86  public void testEquals_set() {
87    assertFalse("A List should never equal a Set.",
88        getList().equals(MinimalSet.from(getList())));
89  }
90}
91