Class<?> clazz = obj.getClass();
logger.fine(String.format("Testing: %s", clazz.getName()));
/* Use every test provided to see if the class is thread safe. */
for (Object t : tests) {
ThreadTest test = (ThreadTest)t;
ThreadSafety result = test.test(clazz);
if (result != ThreadSafety.THREAD) {
/* Any test that is not thread safe is failed. */
failedTests.add(test.getClass());
}
/* If a result was returned, and it's worse than the stored one. */
if (result != null
&& (result.val() < safety.val())) {
safety = result; // Set the new safety result.
}
}
/* Check the task can be serialised if it's VM safe. */
if (safety == ThreadSafety.VM
&& !isSerialisabe(obj)) {
/* If a task cannot be serialised, it's unsafe. */
safety = ThreadSafety.SEQUENTIAL;
}
/* Print out the thread safeness of the class. */
switch (safety) {
case SEQUENTIAL:
logger.info(String.format("Class %s - Sequential safe",
clazz.getName()));
break;
case THREAD:
logger.info(String.format("Class %s - Thread safe",
clazz.getName()));
break;
case VM:
logger.info(String.format("Class %s - Virtual Machine safe",
clazz.getName()));
break;
default:
logger.info(String.format("Unknown test result for class %s",
clazz.getName()));
}
/* Print any failed tests to the loggers fine output. */
if (failedTests.size() > 0) {
StringBuilder errors = new StringBuilder("Failed tests:\n");
for (Class<?> test : failedTests) {
errors.append('\t').append(test.getName()).append('\n');
}
logger.fine(errors.toString());
}