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