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