use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.
the class CoverageAnalysis method analyzeCoverage.
/**
* Identify all JUnit tests starting with the given name prefix, instrument
* and run tests
*/
public static void analyzeCoverage() {
Sandbox.goingToExecuteSUTCode();
TestGenerationContext.getInstance().goingToExecuteSUTCode();
Sandbox.goingToExecuteUnsafeCodeOnSameThread();
ExecutionTracer.setCheckCallerThread(false);
try {
String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
if (Properties.TARGET_CLASS.endsWith(".jar") || Properties.TARGET_CLASS.contains(File.separator)) {
targetClasses = DependencyAnalysis.analyzeTarget(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
} else {
targetClasses.add(Properties.TARGET_CLASS);
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
}
LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
} catch (Throwable e) {
LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
return;
} finally {
Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
Sandbox.doneWithExecutingSUTCode();
TestGenerationContext.getInstance().doneWithExecutingSUTCode();
}
// TestCluster.getInstance();
List<Class<?>> testClasses = getTestClasses();
LoggingUtils.getEvoLogger().info("* Found " + testClasses.size() + " test class(es)");
if (testClasses.isEmpty())
return;
/*
* sort them in a deterministic way, in case there are
* static state dependencies
*/
sortTestClasses(testClasses);
Class<?>[] tests = testClasses.toArray(new Class<?>[testClasses.size()]);
LoggingUtils.getEvoLogger().info("* Executing test(s)");
if (Properties.SELECTED_JUNIT == null) {
boolean origUseAgent = EvoRunner.useAgent;
boolean origUseClassLoader = EvoRunner.useClassLoader;
try {
// avoid double instrumentation
EvoRunner.useAgent = false;
// avoid double instrumentation
EvoRunner.useClassLoader = false;
List<JUnitResult> results = executeTests(tests);
printReport(results);
} finally {
EvoRunner.useAgent = origUseAgent;
EvoRunner.useClassLoader = origUseClassLoader;
}
} else {
// instead of just running junit tests, carve them
JUnitTestCarvedChromosomeFactory carvedFactory = new JUnitTestCarvedChromosomeFactory(null);
TestSuiteChromosome testSuite = carvedFactory.getCarvedTestSuite();
int goals = 0;
for (Properties.Criterion pc : Properties.CRITERION) {
LoggingUtils.getEvoLogger().info("* Coverage analysis for criterion " + pc);
TestFitnessFactory ffactory = FitnessFunctions.getFitnessFactory(pc);
goals += ffactory.getCoverageGoals().size();
FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(pc);
ffunction.getFitness(testSuite);
CoverageCriteriaAnalyzer.analyzeCoverage(testSuite, pc);
}
// Generate test suite
TestSuiteGenerator.writeJUnitTestsAndCreateResult(testSuite);
StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals);
if (Properties.COVERAGE_MATRIX)
throw new IllegalArgumentException("Coverage matrix not yet available when measuring coverage of a carved test suite");
}
}
use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testBeanDateConverterUtils6.
@Test
public void testBeanDateConverterUtils6() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.DateConverterTest6.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.DateConverter.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertEquals(1, factory.getNumCarvedTestCases());
String code = factory.getChromosome().getTestCase().toCode();
System.out.println(code);
}
use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testBeanDateConverterUtils9.
@Test
public void testBeanDateConverterUtils9() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.DateConverterTest9.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.DateConverter.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertEquals(1, factory.getNumCarvedTestCases());
String code = factory.getChromosome().getTestCase().toCode();
System.out.println(code);
}
use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testBeanDateConverterUtils1.
@Test
public void testBeanDateConverterUtils1() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.DateConverterTest1.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.DateConverter.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertEquals(1, factory.getNumCarvedTestCases());
String code = factory.getChromosome().getTestCase().toCode();
System.out.println(code);
}
use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassList.
@Test
public void testGenericClassList() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithListTest.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithList.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertTrue(factory.hasCarvedTestCases());
TestChromosome carved = factory.getChromosome();
Assert.assertNotNull(carved);
Assert.assertEquals("", 10, carved.test.size());
for (int i = 0; i < carved.test.size(); i++) {
Statement stmt = carved.test.getStatement(i);
boolean valid = stmt.isValid();
Assert.assertTrue("Invalid stmt at position " + i, valid);
}
String code = carved.toString();
String setLong = "GenericObjectWrapperWithList<GenericObjectWrapperWithListTest.Foo>";
Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
}
Aggregations