use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ProjectStaticDataTest method testIsToTest_ActuallyImproved.
@Test
public void testIsToTest_ActuallyImproved() {
Project project = new Project();
project.setTotalNumberOfTestableClasses(XSDUtils.convert(1));
CUT cut = new CUT();
cut.setFullNameOfTargetClass("foo.Bar");
cut.setFullNameOfTestSuite("foo.BarTest");
Generation g0 = new Generation();
g0.setId(XSDUtils.convert(0));
g0.setFailed(false);
g0.setModified(true);
g0.setTimeBudgetInSeconds(XSDUtils.convert(60));
// empty dummy test suite
g0.setSuite(new TestSuite());
// add first generation
cut.getGeneration().add(g0);
for (int i = 1; i <= 3; i++) {
Generation g = new Generation();
g.setId(XSDUtils.convert(i));
g.setFailed(false);
g.setModified(false);
g.setTimeBudgetInSeconds(XSDUtils.convert(60));
g.setSuite(new TestSuite());
cut.getGeneration().add(g);
}
project.getCut().add(cut);
ProjectStaticData data = new ProjectStaticData();
data.setProject(project);
// coverage has improved
Assert.assertTrue(data.isToTest("foo.Bar", 3));
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ProjectStaticDataTest method testIsToTest_OneLastNGenerationsFailed.
@Test
public void testIsToTest_OneLastNGenerationsFailed() {
Project project = new Project();
project.setTotalNumberOfTestableClasses(XSDUtils.convert(1));
CUT cut = new CUT();
cut.setFullNameOfTargetClass("foo.Bar");
cut.setFullNameOfTestSuite("foo.BarTest");
for (int i = 0; i < 5; i++) {
Generation generation = new Generation();
generation.setId(XSDUtils.convert(i));
generation.setFailed(i % 2 != 0);
generation.setModified(false);
generation.setTimeBudgetInSeconds(XSDUtils.convert(60));
cut.getGeneration().add(generation);
}
project.getCut().add(cut);
ProjectStaticData data = new ProjectStaticData();
data.setProject(project);
// to be considered has improved, all N generations
// have to end successfully
Assert.assertTrue(data.isToTest("foo.Bar", 3));
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class StorageManager method updateDatabase.
/**
* Not only modify the state of <code>db</code>, but
* also copy/replace new test cases on file disk
*
* @param ondisk
* @param db
*/
private void updateDatabase(String targetClass, TestsOnDisk ondisk, Project db, ProjectStaticData current) {
// extractClassName(tmpTests, ondisk.testSuite);
String testName = targetClass + Properties.JUNIT_SUFFIX;
// CUT data
CUT cut = ProjectUtil.getCUT(db, targetClass);
if (cut == null) {
// first generation
cut = new CUT();
cut.setFullNameOfTargetClass(targetClass);
cut.setFullNameOfTestSuite(testName);
db.getCut().add(cut);
}
// Generation data
Generation generation = new Generation();
generation.setId(BigInteger.valueOf(cut.getGeneration().size()));
// by default
generation.setFailed(false);
generation.setModified(current.getClassInfo(targetClass).hasChanged());
generation.setTimeBudgetInSeconds(BigInteger.valueOf(current.getClassInfo(targetClass).getTimeBudgetInSeconds()));
generation.setMemoryInMB(BigInteger.valueOf(current.getClassInfo(targetClass).getMemoryInMB()));
if (!current.getClassInfo(targetClass).isToTest()) {
// if a class was not considered for testing purpose,
// we still want to keep some information about it.
// that information will be crucial to, for example,
// determine how much time EvoSuite spent over all classes
cut.getGeneration().add(generation);
// we do not have more information, so return
return;
}
File std_err_CLIENT = new File(this.tmpLogs + File.separator + targetClass + File.separator + "std_err_CLIENT.log");
assert std_err_CLIENT.exists();
File std_out_CLIENT = new File(this.tmpLogs + File.separator + targetClass + File.separator + "std_out_CLIENT.log");
assert std_out_CLIENT.exists();
File std_err_MASTER = new File(this.tmpLogs + File.separator + targetClass + File.separator + "std_err_MASTER.log");
assert std_err_MASTER.exists();
File std_out_MASTER = new File(this.tmpLogs + File.separator + targetClass + File.separator + "std_out_MASTER.log");
assert std_out_MASTER.exists();
generation.setStdErrCLIENT(std_err_CLIENT.getAbsolutePath());
generation.setStdOutCLIENT(std_out_CLIENT.getAbsolutePath());
generation.setStdErrMASTER(std_err_MASTER.getAbsolutePath());
generation.setStdOutMASTER(std_out_MASTER.getAbsolutePath());
cut.getGeneration().add(generation);
if (ondisk == null) {
// was it supposed to happen?
if (current.getClassInfo(targetClass).isToTest()) {
// it should have generated test cases
generation.setFailed(true);
/*
* TODO to properly update failure data, we will first need
* to change how we output such info in EvoSuite (likely
* we will need something more than statistics.csv)
*/
}
return;
}
assert ondisk.isValid();
CsvJUnitData csv = ondisk.csvData;
if (!isBetterThanAnyExistingTestSuite(db, current, ondisk)) {
// about EvoSuite execution.
return;
}
// Test Suite data
TestSuite suite = new TestSuite();
suite.setFullPathOfTestSuite(ondisk.testSuite.getAbsolutePath());
suite.setNumberOfTests(BigInteger.valueOf(csv.getNumberOfTests()));
suite.setTotalNumberOfStatements(BigInteger.valueOf(csv.getTotalNumberOfStatements()));
suite.setTotalEffortInSeconds(BigInteger.valueOf(csv.getDurationInSeconds()));
List<Coverage> coverageValues = new ArrayList<Coverage>();
for (String criterion : csv.getCoverageVariables()) {
Coverage coverage = new Coverage();
coverage.setCriterion(criterion);
coverage.setCoverageValue(Double.parseDouble(this.df.format(csv.getCoverage(criterion))));
coverage.setCoverageBitString(csv.getCoverageBitString(criterion));
coverageValues.add(coverage);
}
suite.getCoverage().addAll(coverageValues);
generation.setSuite(suite);
/*
* So far we have modified only the content of db.
* Need also to update the actual test cases
*/
removeBestTestSuite(testName);
addBestTestSuite(ondisk.testSuite);
File scaffolding = getScaffoldingIfExists(ondisk.testSuite);
if (scaffolding != null) {
addBestTestSuite(scaffolding);
}
if (ondisk.serializedSuite != null) {
File target = new File(getSeedInFolder(), ondisk.serializedSuite.getName());
target.delete();
try {
FileUtils.copyFile(ondisk.serializedSuite, target);
} catch (IOException e) {
logger.error("Failed to copy over a new generated serialized test suite: " + e.getMessage(), e);
}
}
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ClassAction method highlightSource.
public void highlightSource(VirtualChannel channel, BuildListener listener) throws InterruptedException {
Generation latestGeneration = CUTUtil.getLatestGeneration(this.cut);
if (latestGeneration.isFailed()) {
StringBuilder str = new StringBuilder();
str.append("<h3>std_err_CLIENT</h3>");
str.append("<p>" + this.getLog(channel, latestGeneration.getStdErrCLIENT()) + "</p>");
str.append("<h3>std_out_CLIENT</h3>");
str.append("<p>" + this.getLog(channel, latestGeneration.getStdOutCLIENT()) + "</p>");
str.append("<h3>std_err_MASTER</h3>");
str.append("<p>" + this.getLog(channel, latestGeneration.getStdErrMASTER()) + "</p>");
str.append("<h3>std_out_MASTER</h3>");
str.append("<p>" + this.getLog(channel, latestGeneration.getStdOutMASTER()) + "</p>");
this.testSourceCode = str.toString();
return;
}
Generation latestSuccessfulGeneration = CUTUtil.getLatestSuccessfulGeneration(this.cut);
if (latestSuccessfulGeneration == null) {
this.testSourceCode = "<p>There was not a single successful generation " + "for this class. Likely this is an EvoSuite bug.</p>";
return;
}
TestSuite suite = latestSuccessfulGeneration.getSuite();
if (suite == null) {
this.testSourceCode = "<p>Test suite of the latest successful generation " + "is null. Likely this is an EvoSuite bug.</p>";
return;
}
try {
String javaFile = suite.getFullPathOfTestSuite();
listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "JavaFile: " + javaFile);
ByteArrayOutputStream out = new ByteArrayOutputStream();
new FilePath(channel, javaFile).copyTo(out);
InputStream file = new ByteArrayInputStream(out.toByteArray());
JavaSource source = new JavaSourceParser().parse(new InputStreamReader(file, Charset.forName("UTF-8")));
JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
options.setShowLineNumbers(true);
options.setAddLineAnchors(true);
JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter();
StringWriter writer = new StringWriter();
converter.convert(source, options, writer);
this.testSourceCode = writer.toString();
} catch (IOException e) {
listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + e.getMessage());
listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Returning a empty source-code");
this.testSourceCode = e.getMessage();
}
}
Aggregations