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

patterntesting.runtime.junit.ComparableTester 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.4.0
Show newest version
/*
 * $Id: ComparableTester.java,v 1.2 2011/09/25 19:41:52 oboehm Exp $
 *
 * Copyright (c) 2011 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 21.09.2011 by oliver ([email protected])
 */

package patterntesting.runtime.junit;

import java.util.*;

import org.junit.Assert;
import org.slf4j.*;

import patterntesting.runtime.monitor.ClasspathMonitor;

/**
 * This utility class checks classes which implements the {@link Comparable}
 * interface. E.g. for two objects which are equals it is expected that the
 * {@link Comparable#compareTo(Object)} method returns 0.
 * 
 * @author oliver ([email protected])
 * @since 1.2 (21.09.2011)
 */
public final class ComparableTester {
    
    private static final Logger log = LoggerFactory.getLogger(ComparableTester.class);
    private static final ClasspathMonitor classpathMonitor = ClasspathMonitor.getInstance();

    /** Utility class - no need to instantiate it. */
    private ComparableTester() {}
    
    /**
     * The {@link Comparable#compareTo(Object)} method should return 0 if the
     * given objects are eqals. If they are not equals the shouldn't return 0.
     * This is checked here.
     * 
     * @param c1 the first Comparable
     * @param c2 the second Comparable
     * @throws AssertionError if the check fails
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void assertCompareTo(final Comparable c1, final Comparable c2) {
        int ret1 = c1.compareTo(c2);
        int ret2 = c2.compareTo(c1);
        if (c1.equals(c2)) {
            String msg = c1.getClass() + ": compareTo(..) should return 0 for equals objects";
            Assert.assertEquals(msg, 0, ret1);
            Assert.assertEquals(msg, 0, ret2);
        } else {
            String msg = c1.getClass()
                    + ": compareTo(..) should return not 0 for not equals objects " + c1 + " and "
                    + c2;
            Assert.assertTrue(msg, ret1 != 0);
            Assert.assertTrue(msg, ret2 != 0);
            msg = c1.getClass() + ": <" + c2 + ">.compareTo(<" + c1 + ">) should return "
                    + (-ret2) + " (not " + ret2 + ")";
            if (ret1 < 0) {
                Assert.assertTrue(msg, ret2 > 0);
            } else {
                Assert.assertTrue(msg, ret2 < 0);
            }
        }
        log.info("compareTo implementation of " + c1.getClass() + " seems to be ok");
    }
    
    /**
     * This method will create two objects of the given class using the
     * default constructor and compares them. So two preconditions must be
     * true:
     * 
    *
  1. the class must not be abstract
  2. *
  3. there must be a (public) default constructor
  4. * *
* * @param clazz the clazz * @throws AssertionError if the check fails */ @SuppressWarnings("rawtypes") public static void assertCompareTo(final Class clazz) throws AssertionError { log.trace("checking {}.compareTo(..)...", clazz); Comparable comp = (Comparable) ObjectTester.newInstanceOf(clazz); Comparable clone = (Comparable) ObjectTester.clone(comp); assertCompareTo(comp, clone); } /** * Check for each class in the given collection if the compareTo method * works as expected * * @param classes a collection of classes to be checked */ @SuppressWarnings("rawtypes") public static void assertCompareTo(final Collection> classes) { for (Class clazz : classes) { assertCompareTo(clazz); } } /** * Check for each {@link Comparable} class in the given package if the * compareTo(..) method works as expected. *
* To get a name of a package call {@link Package#getPackage(String)}. * But be sure that you can't get null as result. In this case * use {@link #assertCompareToOfPackage(String)}. * * @param pkg the package e.g. "patterntesting.runtime" * @see #assertCompareToOfPackage(String) */ public static void assertCompareTo(final Package pkg) { assert pkg!= null; assertCompareToOfPackage(pkg.getName()); } /** * Check for each {@link Comparable} class in the given package if the * compareTo(..) method works as expected. * * @param packageName the package name e.g. "patterntesting.runtime" */ @SuppressWarnings("rawtypes") public static void assertCompareToOfPackage(final String packageName) { assert packageName != null; Collection> comparables = getComparableClasses(packageName); assertCompareTo(comparables); } /** * Check for each {@link Comparable} class in the given package if the * compareTo(..) method works as expected. * * @param packageName the package name e.g. "patterntesting.runtime" * @param excluded classes which should be excluded from the check * @see #assertCompareToOfPackage(String) */ public static void assertCompareToOfPackage(final String packageName, final Class>... excluded) { List>> excludedList = Arrays.asList(excluded); assertCompareToOfPackage(packageName, excludedList); } /** * Check for each {@link Comparable} class in the given package if the * compareTo(..) method works as expected. * * @param packageName the package name e.g. "patterntesting.runtime" * @param excluded classes which should be excluded from the check * @see #assertCompareToOfPackage(String) */ @SuppressWarnings("rawtypes") public static void assertCompareToOfPackage(final String packageName, final List>> excluded) { Collection> classes = getComparableClasses(packageName); log.debug("{} will be excluded from check", excluded); ObjectTester.removeClasses(classes, excluded); assertCompareTo(classes); } @SuppressWarnings( "rawtypes" ) private static Collection> getComparableClasses(final String packageName) { Collection> comparables = classpathMonitor.getClassList(packageName, Comparable.class); return comparables; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy