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("test runners must not instantiate and run this directly, only via suites we build")
037// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
038@SuppressWarnings("JUnit4ClassUsedInJUnit3")
039public class ListEqualsTester<E> extends AbstractListTester<E> {
040  public void testEquals_otherListWithSameElements() {
041    assertTrue(
042        "A List should equal any other List containing the same elements.",
043        getList().equals(new ArrayList<E>(getOrderedElements())));
044  }
045
046  @CollectionSize.Require(absent = CollectionSize.ZERO)
047  public void testEquals_otherListWithDifferentElements() {
048    ArrayList<E> other = new ArrayList<>(getSampleElements());
049    other.set(other.size() / 2, getSubjectGenerator().samples().e3());
050    assertFalse(
051        "A List should not equal another List containing different elements.",
052        getList().equals(other));
053  }
054
055  @CollectionSize.Require(absent = CollectionSize.ZERO)
056  public void testEquals_otherListContainingNull() {
057    List<E> other = new ArrayList<>(getSampleElements());
058    other.set(other.size() / 2, null);
059    assertFalse(
060        "Two Lists should not be equal if exactly one of them has null at a given index.",
061        getList().equals(other));
062  }
063
064  @CollectionSize.Require(absent = CollectionSize.ZERO)
065  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
066  public void testEquals_containingNull() {
067    ArrayList<E> elements = new ArrayList<>(getSampleElements());
068    elements.set(elements.size() / 2, null);
069    collection = getSubjectGenerator().create(elements.toArray());
070    List<E> other = new ArrayList<>(getSampleElements());
071    assertFalse(
072        "Two Lists should not be equal if exactly one of them has null at a given index.",
073        getList().equals(other));
074  }
075
076  @CollectionSize.Require(absent = CollectionSize.ZERO)
077  public void testEquals_shorterList() {
078    Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
079    assertFalse(
080        "Lists of different sizes should not be equal.",
081        getList().equals(new ArrayList<E>(fewerElements)));
082  }
083
084  public void testEquals_longerList() {
085    Collection<E> moreElements = getSampleElements(getNumElements() + 1);
086    assertFalse(
087        "Lists of different sizes should not be equal.",
088        getList().equals(new ArrayList<E>(moreElements)));
089  }
090
091  public void testEquals_set() {
092    assertFalse("A List should never equal a Set.", getList().equals(MinimalSet.from(getList())));
093  }
094}