com.atlassian.clover.api.optimization.TestOptimizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clover Show documentation
Show all versions of clover Show documentation
Clover is an award winning code coverage and testing tool for Java and Groovy.
It integrates easily with Maven (1, 2 and 3), Ant, Grails, Eclipse and IntelliJ IDEA
as well as with continuous integration servers such as Bamboo, Jenkins or Hudson.
Note: before Clover 4.0 this artifact was named com.cenqua.clover:clover.
package com.atlassian.clover.api.optimization;
import com.atlassian.clover.optimization.LocalSnapshotOptimizer;
import com.atlassian.clover.optimization.ClassOptimizable;
import com.atlassian.clover.api.CloverException;
import com.atlassian.clover.Logger;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
/**
* A TestOptimizer allows you to optimize a Collection of classes (that are Tests) programatically.
*
* Typically, this is to be done if your tests in are defined in a junit.framework.TestSuite.
*
*
* - The {@link #optimize} method will take a list of Optimizables and return an optimized list of Optimizables.
* - The {@link #optimizeTestClasses} method is a convenience method for optimizing a list of Test Classes.
*
*/
public class TestOptimizer {
private final OptimizationOptions options;
public TestOptimizer(final OptimizationOptions options) {
this.options = options;
}
public TestOptimizer(final String initString, final File snapshotFile) {
this.options = new OptimizationOptions.Builder().debug(true).initString(initString).snapshot(snapshotFile).build();
}
/**
* Optimize a collection of Test classes.
*
* @param testClasses classes to be optimized
* @return an optimized list of Classes
*/
public Collection optimizeTestClasses(final Collection testClasses) {
final List optimizableClasses= convertClassesToOptimizables(testClasses);
final List extends Optimizable> optimizedClasses = optimize(optimizableClasses);
return convertClassList(optimizedClasses);
}
/**
* Optimize a collection of any Optimizable.
*
* @param optimizables a list of objects, denoting tests, to optimize
* @param Optimizable
* @return an optimized list of optimizable objects
*/
public List optimize(final List optimizables) {
try {
// actually optimize
return LocalSnapshotOptimizer.optimize(optimizables, options);
} catch (CloverException e) {
Logger.getInstance().error("Optimization failed. Running all specified tests.");
Logger.getInstance().debug(e.getMessage(), e);
}
return optimizables;
}
/**
* Optimizes a collection of unknown objects. It tries to call 'getName()' method on each element from
* objectList
using reflections. It returns a List where each element is an Optimizable
* which getName() method returns a value of corresponding objectList.get(x).getName()
.
*
* This is a workaround for Clover-for-Grails plugin, where FileOptimizable class cannot
* implement Optimizable interface due to problems with dependency resolution in Grails
* via 'install-plugin' command. We're using Object and reflections to pass the necessary information.
*
* @param objectList list of objects having the 'String getName()' method returning name of optimizable resource
* @return List<Optimizable> optimized resource set
* @throws java.lang.NoSuchMethodException if getName() method does not exist
* @throws java.lang.reflect.InvocationTargetException if getName() call fails
* @throws java.lang.IllegalAccessException if getName() call fails
*/
public List optimizeObjects(final List objectList) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
final List list = new ArrayList(objectList.size());
for (final Object obj : objectList) {
// name = obj.getName();
final String name = (String)obj.getClass().getMethod("getName").invoke(obj);
list.add(new StringOptimizable(name));
}
// perform optimization
return optimize(list);
}
private Collection convertClassList(final List extends Optimizable> optimizables) {
// create the list of culled and re-ordered classes.
final Collection optimizedClasses = new ArrayList(optimizables.size());
for (final Optimizable optimizable : optimizables) {
// lookup the class for each optimizable
final ClassOptimizable optClass = (ClassOptimizable) optimizable;
optimizedClasses.add(optClass.getMyClass());
}
return optimizedClasses;
}
/**
* Converts a collection of classes to a Map of Optimizables, Class.
*
* @param testClasses the list of classes to convert
* @return a map of classes, keyed on Optimizable.
*/
private List convertClassesToOptimizables(final Collection testClasses) {
final List optimizables = new ArrayList(testClasses.size());
for (final Class aClass : testClasses) {
optimizables.add(new ClassOptimizable(aClass));
}
return optimizables;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy