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.features.CollectionFeature.ALLOWS_NULL_VALUES;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.testing.MinimalSet;
023import com.google.common.collect.testing.features.CollectionFeature;
024import com.google.common.collect.testing.features.CollectionSize;
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028import org.junit.Ignore;
029
030/**
031 * Tests {@link List#equals}.
032 *
033 * @author George van den Driessche
034 */
035@GwtCompatible
036@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
037public class ListEqualsTester<E> extends AbstractListTester<E> {
038  public void testEquals_otherListWithSameElements() {
039    assertTrue(
040        "A List should equal any other List containing the same elements.",
041        getList().equals(new ArrayList<E>(getOrderedElements())));
042  }
043
044  @CollectionSize.Require(absent = CollectionSize.ZERO)
045  public void testEquals_otherListWithDifferentElements() {
046    ArrayList<E> other = new ArrayList<>(getSampleElements());
047    other.set(other.size() / 2, getSubjectGenerator().samples().e3());
048    assertFalse(
049        "A List should not equal another List containing different elements.",
050        getList().equals(other));
051  }
052
053  @CollectionSize.Require(absent = CollectionSize.ZERO)
054  public void testEquals_otherListContainingNull() {
055    List<E> other = new ArrayList<>(getSampleElements());
056    other.set(other.size() / 2, null);
057    assertFalse(
058        "Two Lists should not be equal if exactly one of them has null at a given index.",
059        getList().equals(other));
060  }
061
062  @CollectionSize.Require(absent = CollectionSize.ZERO)
063  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
064  public void testEquals_containingNull() {
065    ArrayList<E> elements = new ArrayList<>(getSampleElements());
066    elements.set(elements.size() / 2, null);
067    collection = getSubjectGenerator().create(elements.toArray());
068    List<E> other = new ArrayList<>(getSampleElements());
069    assertFalse(
070        "Two Lists should not be equal if exactly one of them has null at a given index.",
071        getList().equals(other));
072  }
073
074  @CollectionSize.Require(absent = CollectionSize.ZERO)
075  public void testEquals_shorterList() {
076    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
077    assertFalse(
078        "Lists of different sizes should not be equal.",
079        getList().equals(new ArrayList<E>(fewerElements)));
080  }
081
082  public void testEquals_longerList() {
083    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
084    assertFalse(
085        "Lists of different sizes should not be equal.",
086        getList().equals(new ArrayList<E>(moreElements)));
087  }
088
089  public void testEquals_set() {
090    assertFalse("A List should never equal a Set.", getList().equals(MinimalSet.from(getList())));
091  }
092}