Search in sources :

Example 1 with TestSuite

use of org.evosuite.xsd.TestSuite in project evosuite by EvoSuite.

the class StorageManager method isBetterThanPreviousGeneration.

/**
 * Before accepting the new test suite this function verifies
 * whether it is better (in terms of coverage) than a previous
 * test generation. It first checks whether it is a class that
 * has been modified. By default we consider that a class has
 * always been changed. Only HistorySchedule will change that
 * behavior. So, for all Schedules except History we accept
 * the new generated test suite. For HistorySchedule, and if a
 * class has not been changed it then checks if the new test
 * suite improves the coverage of the previous one.
 *
 * @param db
 * @param current
 * @param suite
 * @return true if the generated test suite is better (in terms of
 * coverage) than a previous generated test suite, false otherwise
 */
private boolean isBetterThanPreviousGeneration(Project db, ProjectStaticData current, TestsOnDisk suite) {
    if (suite.csvData == null) {
        // no data available
        return false;
    }
    // only HistorySchedule change this behavior
    if (current.getClassInfo(suite.cut).hasChanged()) {
        return true;
    }
    CUT cut = ProjectUtil.getCUT(db, suite.cut);
    Generation latestSuccessfulGeneration = CUTUtil.getLatestSuccessfulGeneration(cut);
    if (latestSuccessfulGeneration == null) {
        return true;
    }
    TestSuite previousTestSuite = latestSuccessfulGeneration.getSuite();
    File oldFile = getFileForTargetBestTest(cut.getFullNameOfTestSuite());
    if (!oldFile.exists()) {
        // this could happen if file was manually removed
        return true;
    }
    // is the OverallCoverage higher?
    double previousOverallCoverage = GenerationUtil.getOverallCoverage(latestSuccessfulGeneration);
    double generatedOverallCoverage = 0.0;
    // first, check if the coverage of at least one criterion is better
    for (Coverage coverage : previousTestSuite.getCoverage()) {
        if (!suite.csvData.hasCoverage(coverage.getCriterion())) {
            continue;
        }
        generatedOverallCoverage += suite.csvData.getCoverage(coverage.getCriterion());
    }
    generatedOverallCoverage /= suite.csvData.getNumberOfCoverageValues();
    double covDif = generatedOverallCoverage - previousOverallCoverage;
    if (covDif > 0.01) {
        // just has two decimal digits
        return true;
    }
    // developers could be interested on that particular goal(s)
    for (Coverage coverage : previousTestSuite.getCoverage()) {
        if (!suite.csvData.hasCoverage(coverage.getCriterion())) {
            continue;
        }
        String generatedCoverage = suite.csvData.getCoverageBitString(coverage.getCriterion());
        String previousCoverage = coverage.getCoverageBitString();
        if (generatedCoverage.length() != previousCoverage.length()) {
            // accept the new suite, as we can't compare both BitStrings
            return true;
        }
        for (int i = 0; i < generatedCoverage.length(); i++) {
            if (previousCoverage.charAt(i) == '0' && generatedCoverage.charAt(i) == '1') {
                return true;
            }
        }
    }
    if (covDif < 0.0) {
        // was higher, therefore discard the new test suite
        return false;
    }
    // if we got same coverage, look at size
    int oldSize = previousTestSuite.getTotalNumberOfStatements().intValue();
    int newSize = suite.csvData.getTotalNumberOfStatements();
    if (newSize != oldSize) {
        return newSize < oldSize;
    }
    // same number of statements, look the number of test cases
    int oldNumTests = previousTestSuite.getNumberOfTests().intValue();
    int newNumTests = suite.csvData.getNumberOfTests();
    return newNumTests < oldNumTests;
}
Also used : Generation(org.evosuite.xsd.Generation) CUT(org.evosuite.xsd.CUT) TestSuite(org.evosuite.xsd.TestSuite) Coverage(org.evosuite.xsd.Coverage) File(java.io.File)

Example 2 with TestSuite

use of org.evosuite.xsd.TestSuite in project evosuite by EvoSuite.

the class ProjectStaticDataTest method testIsToTest_SkippedClass.

@Test
public void testIsToTest_SkippedClass() {
    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());
    // and 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));
        // and no test suite
        cut.getGeneration().add(g);
    }
    Generation g4 = new Generation();
    g4.setId(XSDUtils.convert(4));
    g4.setFailed(false);
    g4.setModified(false);
    // EvoSuite skipped this one
    g4.setTimeBudgetInSeconds(XSDUtils.convert(0));
    cut.getGeneration().add(g4);
    project.getCut().add(cut);
    ProjectStaticData data = new ProjectStaticData();
    data.setProject(project);
    // coverage has not improved
    Assert.assertFalse(data.isToTest("foo.Bar", 3));
}
Also used : Project(org.evosuite.xsd.Project) Generation(org.evosuite.xsd.Generation) CUT(org.evosuite.xsd.CUT) TestSuite(org.evosuite.xsd.TestSuite) Test(org.junit.Test)

Example 3 with TestSuite

use of org.evosuite.xsd.TestSuite 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));
}
Also used : Project(org.evosuite.xsd.Project) Generation(org.evosuite.xsd.Generation) CUT(org.evosuite.xsd.CUT) TestSuite(org.evosuite.xsd.TestSuite) Test(org.junit.Test)

Example 4 with TestSuite

use of org.evosuite.xsd.TestSuite 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);
        }
    }
}
Also used : Generation(org.evosuite.xsd.Generation) CUT(org.evosuite.xsd.CUT) TestSuite(org.evosuite.xsd.TestSuite) Coverage(org.evosuite.xsd.Coverage) IOException(java.io.IOException) File(java.io.File)

Example 5 with TestSuite

use of org.evosuite.xsd.TestSuite 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();
    }
}
Also used : FilePath(hudson.FilePath) JavaSourceConversionOptions(de.java2html.options.JavaSourceConversionOptions) JavaSource2HTMLConverter(de.java2html.converter.JavaSource2HTMLConverter) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JavaSourceParser(de.java2html.javasource.JavaSourceParser) Generation(org.evosuite.xsd.Generation) TestSuite(org.evosuite.xsd.TestSuite) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) JavaSource(de.java2html.javasource.JavaSource)

Aggregations

Generation (org.evosuite.xsd.Generation)5 TestSuite (org.evosuite.xsd.TestSuite)5 CUT (org.evosuite.xsd.CUT)4 File (java.io.File)2 IOException (java.io.IOException)2 Coverage (org.evosuite.xsd.Coverage)2 Project (org.evosuite.xsd.Project)2 Test (org.junit.Test)2 JavaSource2HTMLConverter (de.java2html.converter.JavaSource2HTMLConverter)1 JavaSource (de.java2html.javasource.JavaSource)1 JavaSourceParser (de.java2html.javasource.JavaSourceParser)1 JavaSourceConversionOptions (de.java2html.options.JavaSourceConversionOptions)1 FilePath (hudson.FilePath)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1