All Downloads are FREE. Search and download functionalities are using the official Maven repository.

patterntesting.runtime.junit.CollectionTester Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.5.0
Show newest version
/*
 * $Id: CollectionTester.java,v 1.9 2016/03/16 22:09:46 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 org.slf4j.*;

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 {

    private static final Logger LOG = LoggerFactory.getLogger(CollectionTester.class);

    /** 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 if 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)) { if (LOG.isTraceEnabled()) { LOG.trace("{}: [{}] is not in {}.", msg, Converter.toLongString(elem1), Converter.toLongString(c2)); } else if (LOG.isDebugEnabled()) { LOG.debug("{}: [{}] is not in {}.", msg, Converter.toString(elem1), Converter.toString(c2)); } 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); for (int i = 0; i < l1.size(); i++) { Object elem1 = l1.get(i); Object elem2 = l2.get(i); if (!elem1.equals(elem2)) { if (LOG.isTraceEnabled()) { LOG.trace("{}. element differs ({} != {}).", i + 1, Converter.toLongString(elem1), Converter.toLongString(elem2)); } else if (LOG.isDebugEnabled()) { LOG.debug("{}. element differs ({} != {}).", i + 1, Converter.toString(elem1), Converter.toString(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)) { if (LOG.isTraceEnabled()) { LOG.trace("Element \"{}\" is missing in {}.", Converter.toLongString(element), Converter.toLongString(c2)); } else if (LOG.isDebugEnabled()) { LOG.debug("Element \"{}\" is missing in {}.", Converter.toString(element), Converter.toString(c2)); } throw new AssertionError("\"" + Converter.toShortString(element) + "\" is missing in one collection (size: " + c1.size() + " / " + c2.size() + " elements)"); } } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy