patterntesting.runtime.junit.SerializableTester 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.
/*
* (c)reated 21.07.2010 by oliver
*/
package patterntesting.runtime.junit;
import java.io.*;
import java.util.*;
import org.slf4j.*;
import patterntesting.runtime.monitor.ClasspathMonitor;
import patterntesting.runtime.util.Converter;
/**
* This is a utility class to check the serializable nature of classes.
*
* NOTE: In the future this class will be perhaps part of the ObjectTester
* class.
*
* Before v1.1 the methods are named "checkSerialization". Since 1.1 these
* methods will have now an "assert" prefix ("assertSerialization").
*
* @author oliver ([email protected])
* @since 1.0.3 (21.07.2010)
*/
public final class SerializableTester {
private static final Logger log = LoggerFactory.getLogger(SerializableTester.class);
private static final ClasspathMonitor classpathMonitor = ClasspathMonitor.getInstance();
/** Utitlity class - no need to instantiate it. */
private SerializableTester() {}
/**
* Check serialization and deserialization of an object.
* Because not all classes has a (correct) overwritten equals method
* we do not compare the deserialized object with the equals() method.
* But if it implements Comparable we verify the equality of the two
* objects with the compareTo() method.
*
* @param object the object
* @throws NotSerializableException if object can't serialized
* @since 1.1
*/
public static void assertSerialization(final Serializable object)
throws NotSerializableException {
byte[] bytes = Converter.serialize(object);
log.debug(object + " serialized in " + bytes.length + " bytes");
try {
Object deserialized = Converter.deserialize(bytes);
if (object instanceof Comparable>) {
ObjectTester.assertCompareTo(object, deserialized);
}
} catch (ClassNotFoundException canthappen) {
throw new RuntimeException(canthappen);
}
}
/**
* This method will create an object of the given class using the
* default constructor. So three preconditions must be true:
*
* - the class must not be abstract
* - there must be a (public) default constructor
* - it must be Serializable
*
*
* @param clazz the clazz
* @throws NotSerializableException if the check fails
* @since 1.1
*/
public static void assertSerialization(final Class extends Serializable> clazz) throws NotSerializableException {
if (log.isTraceEnabled()) {
log.trace("checking " + clazz.getName() + " if it can be serialized...");
}
try {
Serializable obj = clazz.newInstance();
assertSerialization(obj);
} catch (InstantiationException e) {
throw new IllegalArgumentException("can't instantiate " + clazz, e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("can't access ctor of " + clazz, e);
}
}
/**
* Check for each class in the given collection if it can be serialized and
* deserialized.
*
* @param classes a collection of classes to be checked
* @throws NotSerializableException if one of the classes can't be serialized
* @since 1.1
*/
public static void assertSerialization(final Collection> classes) throws NotSerializableException {
for (Class> clazz : classes) {
assertSerialization(clazz);
}
}
/**
* Check for each class in the given package if it can be serialized and
* deserialized.
*
* 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 #assertSerializationOfPackage(String)}.
*
* @param pkg the package e.g. "patterntesting.runtime"
* @throws NotSerializableException if one of the class can't be serialized
* @see #assertSerializationOfPackage(String)
* @since 1.1
*/
public static void assertSerialization(final Package pkg) throws NotSerializableException {
assert pkg!= null;
assertSerializationOfPackage(pkg.getName());
}
/**
* Check for each class in the given package if it can be serialized and
* deserialized.
*
* @param packageName the package name e.g. "patterntesting.runtime"
* @throws NotSerializableException if one of the class can't be serialized
* @since 1.1
*/
public static void assertSerializationOfPackage(final String packageName) throws NotSerializableException {
assert packageName != null;
Collection> classes = getSerializableClasses(packageName);
assertSerialization(classes);
}
@SuppressWarnings("unchecked")
private static Collection> getSerializableClasses(final String packageName) {
String[] classnames = classpathMonitor.getClasspathClasses();
Collection> classes = new ArrayList>();
for (int i = 0; i < classnames.length; i++) {
if (classnames[i].startsWith(packageName)) {
try {
Class> clazz = Class.forName(classnames[i]);
if (Serializable.class.isAssignableFrom(clazz)) {
classes.add((Class) clazz);
}
} catch (ClassNotFoundException ignored) {
log.info(classnames[i] + " will be ignored", ignored);
}
}
}
return classes;
}
}