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