Search in sources :

Example 1 with TestSuiteWriter

use of org.evosuite.junit.writer.TestSuiteWriter in project evosuite by EvoSuite.

the class TestSuiteGenerator method writeJUnitTestsAndCreateResult.

/**
 * <p>
 * If Properties.JUNIT_TESTS is set, this method writes the given test cases
 * to the default directory Properties.TEST_DIR.
 *
 * <p>
 * The name of the test will be equal to the SUT followed by the given
 * suffix
 *
 * @param testSuite
 *            a test suite.
 */
public static TestGenerationResult writeJUnitTestsAndCreateResult(TestSuiteChromosome testSuite, String suffix) {
    List<TestCase> tests = testSuite.getTests();
    if (Properties.JUNIT_TESTS) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.WRITING_TESTS);
        TestSuiteWriter suiteWriter = new TestSuiteWriter();
        suiteWriter.insertTests(tests);
        String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
        String testDir = Properties.TEST_DIR;
        LoggingUtils.getEvoLogger().info("* Writing JUnit test case '" + (name + suffix) + "' to " + testDir);
        suiteWriter.writeTestSuite(name + suffix, testDir, testSuite.getLastExecutionResults());
    }
    return TestGenerationResultBuilder.buildSuccessResult();
}
Also used : TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 2 with TestSuiteWriter

use of org.evosuite.junit.writer.TestSuiteWriter in project evosuite by EvoSuite.

the class TestSuiteGenerator method writeJUnitFailingTests.

public void writeJUnitFailingTests() {
    if (!Properties.CHECK_CONTRACTS)
        return;
    FailingTestSet.sendStatistics();
    if (Properties.JUNIT_TESTS) {
        TestSuiteWriter suiteWriter = new TestSuiteWriter();
        // suiteWriter.insertTests(FailingTestSet.getFailingTests());
        TestSuiteChromosome suite = new TestSuiteChromosome();
        for (TestCase test : FailingTestSet.getFailingTests()) {
            test.setFailing();
            suite.addTest(test);
        }
        String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
        String testDir = Properties.TEST_DIR;
        LoggingUtils.getEvoLogger().info("* Writing failing test cases '" + (name + Properties.JUNIT_SUFFIX) + "' to " + testDir);
        suiteWriter.insertAllTests(suite.getTests());
        FailingTestSet.writeJUnitTestSuite(suiteWriter);
        suiteWriter.writeTestSuite(name + Properties.JUNIT_FAILED_SUFFIX, testDir, suite.getLastExecutionResults());
    }
}
Also used : TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 3 with TestSuiteWriter

use of org.evosuite.junit.writer.TestSuiteWriter in project evosuite by EvoSuite.

the class FailingTestSet method writeJUnitTestSuite.

/**
 * Output the failing tests in a JUnit test suite
 */
public static void writeJUnitTestSuite() {
    logger.info("Writing {} failing tests", violations.size());
    TestSuiteWriter writer = new TestSuiteWriter();
    writeJUnitTestSuite(writer);
    String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
    String testDir = Properties.TEST_DIR;
    writer.writeTestSuite("Failures" + name, testDir, Collections.emptyList());
}
Also used : TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter)

Example 4 with TestSuiteWriter

use of org.evosuite.junit.writer.TestSuiteWriter in project evosuite by EvoSuite.

the class JUnitAnalyzer method compileTests.

private static List<File> compileTests(List<TestCase> tests, File dir) {
    TestSuiteWriter suite = new TestSuiteWriter();
    suite.insertAllTests(tests);
    // to get name, remove all package before last '.'
    int beginIndex = Properties.TARGET_CLASS.lastIndexOf(".") + 1;
    String name = Properties.TARGET_CLASS.substring(beginIndex);
    // postfix
    name += "_" + (NUM++) + "_tmp_" + Properties.JUNIT_SUFFIX;
    try {
        // now generate the JUnit test case
        List<File> generated = suite.writeTestSuite(name, dir.getAbsolutePath(), Collections.EMPTY_LIST);
        for (File file : generated) {
            if (!file.exists()) {
                logger.error("Supposed to generate " + file + " but it does not exist");
                return null;
            }
        }
        // try to compile the test cases
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (compiler == null) {
            logger.error("No Java compiler is available");
            return null;
        }
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        Locale locale = Locale.getDefault();
        Charset charset = Charset.forName("UTF-8");
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, locale, charset);
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(generated);
        List<String> optionList = new ArrayList<>();
        String evosuiteCP = ClassPathHandler.getInstance().getEvoSuiteClassPath();
        if (JarPathing.containsAPathingJar(evosuiteCP)) {
            evosuiteCP = JarPathing.expandPathingJars(evosuiteCP);
        }
        String targetProjectCP = ClassPathHandler.getInstance().getTargetProjectClasspath();
        if (JarPathing.containsAPathingJar(targetProjectCP)) {
            targetProjectCP = JarPathing.expandPathingJars(targetProjectCP);
        }
        String classpath = targetProjectCP + File.pathSeparator + evosuiteCP;
        optionList.addAll(Arrays.asList("-classpath", classpath));
        CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnits);
        boolean compiled = task.call();
        fileManager.close();
        if (!compiled) {
            logger.error("Compilation failed on compilation units: " + compilationUnits);
            logger.error("Classpath: " + classpath);
            // TODO remove
            logger.error("evosuiteCP: " + evosuiteCP);
            for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
                if (diagnostic.getMessage(null).startsWith("error while writing")) {
                    logger.error("Error is due to file permissions, ignoring...");
                    return generated;
                }
                logger.error("Diagnostic: " + diagnostic.getMessage(null) + ": " + diagnostic.getLineNumber());
            }
            StringBuffer buffer = new StringBuffer();
            for (JavaFileObject sourceFile : compilationUnits) {
                List<String> lines = FileUtils.readLines(new File(sourceFile.toUri().getPath()));
                buffer.append(compilationUnits.iterator().next().toString() + "\n");
                for (int i = 0; i < lines.size(); i++) {
                    buffer.append((i + 1) + ": " + lines.get(i) + "\n");
                }
            }
            logger.error(buffer.toString());
            return null;
        }
        return generated;
    } catch (IOException e) {
        logger.error("" + e, e);
        return null;
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Charset(java.nio.charset.Charset) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File)

Example 5 with TestSuiteWriter

use of org.evosuite.junit.writer.TestSuiteWriter in project evosuite by EvoSuite.

the class TestSuiteMinimizer method minimizeTests.

/**
 * Minimize test suite with respect to the isCovered Method of the goals
 * defined by the supplied TestFitnessFactory
 *
 * @param suite a {@link org.evosuite.testsuite.TestSuiteChromosome} object.
 */
private void minimizeTests(TestSuiteChromosome suite) {
    logger.info("Minimizing per test");
    ExecutionTracer.enableTraceCalls();
    for (TestChromosome test : suite.getTestChromosomes()) {
        // implies test.clearCachedResults();
        test.setChanged(true);
    }
    List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
    for (TestFitnessFactory<?> ff : testFitnessFactories) {
        goals.addAll(ff.getCoverageGoals());
    }
    filterJUnitCoveredGoals(goals);
    int currentGoal = 0;
    int numGoals = goals.size();
    if (Properties.MINIMIZE_SORT)
        Collections.sort(goals);
    Set<TestFitnessFunction> covered = new LinkedHashSet<TestFitnessFunction>();
    List<TestChromosome> minimizedTests = new ArrayList<TestChromosome>();
    TestSuiteWriter minimizedSuite = new TestSuiteWriter();
    for (TestFitnessFunction goal : goals) {
        updateClientStatus(numGoals > 0 ? 100 * currentGoal / numGoals : 100);
        currentGoal++;
        if (isTimeoutReached()) {
            /*
				 * FIXME: if timeout, this algorithm should be changed in a way that the modifications
				 * done so far are not lost
				 */
            logger.warn("Minimization timeout. Roll back to original test suite");
            return;
        }
        logger.info("Considering goal: " + goal);
        if (Properties.MINIMIZE_SKIP_COINCIDENTAL) {
            for (TestChromosome test : minimizedTests) {
                if (isTimeoutReached()) {
                    logger.warn("Minimization timeout. Roll back to original test suite");
                    return;
                }
                if (goal.isCovered(test)) {
                    logger.info("Covered by minimized test: " + goal);
                    covered.add(goal);
                    // test.getTestCase().addCoveredGoal(goal); // FIXME why? goal.isCovered(test) is already adding the goal
                    break;
                }
            }
        }
        if (covered.contains(goal)) {
            logger.info("Already covered: " + goal);
            logger.info("Now the suite covers " + covered.size() + "/" + goals.size() + " goals");
            continue;
        }
        List<TestChromosome> coveringTests = new ArrayList<TestChromosome>();
        for (TestChromosome test : suite.getTestChromosomes()) {
            if (goal.isCovered(test)) {
                coveringTests.add(test);
            }
        }
        Collections.sort(coveringTests);
        if (!coveringTests.isEmpty()) {
            TestChromosome test = coveringTests.get(0);
            org.evosuite.testcase.TestCaseMinimizer minimizer = new org.evosuite.testcase.TestCaseMinimizer(goal);
            TestChromosome copy = (TestChromosome) test.clone();
            minimizer.minimize(copy);
            if (isTimeoutReached()) {
                logger.warn("Minimization timeout. Roll back to original test suite");
                return;
            }
            // TODO: Need proper list of covered goals
            copy.getTestCase().clearCoveredGoals();
            // Add ALL goals covered by the minimized test
            for (TestFitnessFunction g : goals) {
                if (g.isCovered(copy)) {
                    // isCovered(copy) adds the goal
                    covered.add(g);
                    logger.info("Goal covered by minimized test: " + g);
                }
            }
            minimizedTests.add(copy);
            minimizedSuite.insertTest(copy.getTestCase());
            logger.info("After new test the suite covers " + covered.size() + "/" + goals.size() + " goals");
        } else {
            logger.info("Goal is not covered: " + goal);
        }
    }
    logger.info("Minimized suite covers " + covered.size() + "/" + goals.size() + " goals");
    suite.tests.clear();
    for (TestCase test : minimizedSuite.getTestCases()) {
        suite.addTest(test);
    }
    if (Properties.MINIMIZE_SECOND_PASS) {
        removeRedundantTestCases(suite, goals);
    }
    double suiteCoverage = suite.getCoverage();
    logger.info("Setting coverage to: " + suiteCoverage);
    ClientState state = ClientState.MINIMIZATION;
    ClientStateInformation information = new ClientStateInformation(state);
    information.setProgress(100);
    information.setCoverage((int) (Math.round(suiteCoverage * 100)));
    ClientServices.getInstance().getClientNode().changeState(state, information);
    for (TestFitnessFunction goal : goals) {
        if (!covered.contains(goal))
            logger.info("Failed to cover: " + goal);
    }
// suite.tests = minimizedTests;
}
Also used : ClientState(org.evosuite.rmi.service.ClientState) org.evosuite.testcase(org.evosuite.testcase) TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter) ClientStateInformation(org.evosuite.rmi.service.ClientStateInformation)

Aggregations

TestSuiteWriter (org.evosuite.junit.writer.TestSuiteWriter)6 File (java.io.File)2 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)2 TestCase (org.evosuite.testcase.TestCase)2 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 JavaCompiler (javax.tools.JavaCompiler)1 CompilationTask (javax.tools.JavaCompiler.CompilationTask)1 JavaFileObject (javax.tools.JavaFileObject)1 StandardJavaFileManager (javax.tools.StandardJavaFileManager)1 ClientState (org.evosuite.rmi.service.ClientState)1 ClientStateInformation (org.evosuite.rmi.service.ClientStateInformation)1 org.evosuite.testcase (org.evosuite.testcase)1