patterntesting.runtime.junit.ComparableTester Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
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.
/*
* $Id: ComparableTester.java,v 1.7 2016/03/14 22:01:56 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 java.util.regex.Pattern;
import org.junit.Assert;
import org.slf4j.*;
import patterntesting.runtime.monitor.ClasspathMonitor;
import patterntesting.runtime.util.Converter;
/**
* 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 extends AbstractTester {
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
*/
@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:
*
* - the class must not be abstract
* - there must be a (public) default constructor
*
*
* @param clazz the clazz
* @throws AssertionError if the check fails
*/
@SuppressWarnings("rawtypes")
public static void assertCompareTo(final Class extends Comparable> 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 extends Comparable> 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.
*
* 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"
* @param excluded class pattern which should be excluded from the check
* @see #assertCompareToOfPackage(String, Pattern...)
* @since 1.6
*/
public static void assertCompareTo(final Package pkg, final Pattern... excluded) {
assert pkg!= null;
assertCompareToOfPackage(pkg.getName(), excluded);
}
/**
* 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 extends Comparable>>... 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.
*
* - Note:
* - Since v1.5.1 this method is deprecated to avoid confusion with method
* {@link #assertCompareToOfPackage(String, Pattern...)}. This method will be
* removed in v1.8.
*
*
* @param packageName the package name e.g. "patterntesting.runtime"
* @param excluded classes which should be excluded from the check
* @see #assertCompareToOfPackage(String)
* @deprecated use {@link #assertCompareToOfPackage(String, Class...)} instead
*/
// TODO: remove me in 1.8
@Deprecated
@SuppressWarnings("rawtypes")
public static void assertCompareToOfPackage(final String packageName,
final List>> excluded) {
Collection> classes = getComparableClasses(packageName);
LOG.debug("{} will be excluded from check.", excluded);
removeClasses(classes, excluded);
assertCompareTo(classes);
}
/**
* 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)
* @since 1.6
*/
@SuppressWarnings("rawtypes")
public static void assertCompareToOfPackage(final String packageName, final Pattern... excluded) {
Collection> classes = getComparableClasses(packageName);
if (LOG.isDebugEnabled()) {
LOG.debug("Pattern {} will be excluded from check.", Converter.toShortString(excluded));
}
removeClasses(classes, excluded);
assertCompareTo(classes);
}
@SuppressWarnings("rawtypes")
private static Collection> getComparableClasses(final String packageName) {
Collection> comparables = classpathMonitor.getClassList(packageName,
Comparable.class);
return comparables;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy