patterntesting.runtime.junit.CollectionTester Maven / Gradle / Ivy
/*
* $Id: CollectionTester.java,v 1.4 2013/12/19 20:45:39 oboehm Exp $
*
* Copyright (c) 2012 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 13.01.2012 by oliver ([email protected])
*/
package patterntesting.runtime.junit;
import java.util.*;
import patterntesting.runtime.util.Converter;
/**
* If you want to assert that the content of two collections are equals use
* this tester here.
*
* @author oliver ([email protected])
* @since 1.2 (13.01.2012)
*/
public final class CollectionTester {
/** Utility class - no need to instantiate it. */
private CollectionTester() {}
/**
* Checks if each object in collection c1 is equals to that of Collection c2.
* If not this method will tell you which element is different.
* If the collection have different size this method will tell you which
* element is missing.
*
* The order of the elements is not relevant. I.e. two collections with different
* ordering of its elements are equals if they have both the same elements
* (independent from the internal ordering).
*
*
* Note: Use Lists ordering is important.
*
*
* @param c1 the c1
* @param c2 the c2
*/
public static void assertEquals(final Collection> c1, final Collection> c2) {
assertEquals(c1, c2, "collections differs");
}
/**
* Assert equals. This method is not for public use but is used by
* MapTester. This is the reason why this method is 'protected'.
*
* @param c1 the c1
* @param c2 the c2
* @param msg the msg
*/
protected static void assertEquals(final Collection> c1, final Collection> c2, final String msg) {
if (c1.size() < c2.size()) {
assertEquals(c2, c1);
} else {
assertEqualsSize(c1, c2);
for (Object elem1 : c1) {
if (!c2.contains(elem1)) {
throw new AssertionError(msg + ": ["
+ Converter.toShortString(elem1) + "] is not in "
+ Converter.toShortString(c2));
}
}
}
}
/**
* Checks if each object in list l1 is equals to that of list l2.
* If not this method will tell you which element is different.
* If the collection have different size this method will tell you which
* element is missing.
*
* Because two lists are compared the order of the element is important.
* I.e. two lists with the same elements but different order are not
* equals.
*
*
* @param l1 the list 1
* @param l2 the list 2
*/
public static void assertEquals(final List> l1, final List> l2) {
if (l1.size() < l2.size()) {
assertEquals(l2, l1);
} else {
assertEqualsSize(l1, l2);
Iterator> iter1 = l1.iterator();
Iterator> iter2 = l2.iterator();
for (int i = 0; iter1.hasNext(); i++) {
Object elem1 = iter1.next();
Object elem2 = iter2.next();
if (!elem1.equals(elem2)) {
throw new AssertionError((i+1) + ". element differs ("
+ Converter.toShortString(elem1) + " != "
+ Converter.toShortString(elem2) + ")");
}
}
}
}
private static void assertEqualsSize(final Collection> c1, final Collection> c2)
throws AssertionError {
if (c1.size() > c2.size()) {
for (Object element : c1) {
if (!c2.contains(element)) {
throw new AssertionError("\"" + Converter.toShortString(element)
+ "\" is missing in " + Converter.toShortString(c2));
}
}
}
}
}