Search in sources :

Example 1 with Coverage

use of org.evosuite.xsd.Coverage 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 Coverage

use of org.evosuite.xsd.Coverage 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)

Aggregations

File (java.io.File)2 CUT (org.evosuite.xsd.CUT)2 Coverage (org.evosuite.xsd.Coverage)2 Generation (org.evosuite.xsd.Generation)2 TestSuite (org.evosuite.xsd.TestSuite)2 IOException (java.io.IOException)1