Search in sources :

Example 6 with TestGenerationResult

use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.

the class TestGeneration method generateTestsLegacy.

private static List<List<TestGenerationResult>> generateTestsLegacy(Properties.Strategy strategy, List<String> args) {
    List<List<TestGenerationResult>> results = new ArrayList<List<TestGenerationResult>>();
    ClassPathHandler.getInstance().getTargetProjectClasspath();
    LoggingUtils.getEvoLogger().info("* Using .task files in " + Properties.OUTPUT_DIR + " [deprecated]");
    File directory = new File(Properties.OUTPUT_DIR);
    String[] extensions = { "task" };
    for (File file : FileUtils.listFiles(directory, extensions, false)) {
        results.addAll(generateTests(strategy, file.getName().replace(".task", ""), args));
    }
    return results;
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) ResourceList(org.evosuite.classpath.ResourceList) File(java.io.File)

Example 7 with TestGenerationResult

use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.

the class TestGeneration method executeTestGeneration.

public static List<List<TestGenerationResult>> executeTestGeneration(Options options, List<String> javaOpts, CommandLine line) {
    Strategy strategy = getChosenStrategy(javaOpts, line);
    if (strategy == null) {
        strategy = Strategy.EVOSUITE;
    }
    List<List<TestGenerationResult>> results = new ArrayList<List<TestGenerationResult>>();
    if (line.getOptions().length == 0) {
        Help.execute(options);
        return results;
    }
    String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
    if (cp == null || cp.isEmpty()) {
        LoggingUtils.getEvoLogger().error("No classpath has been defined for the target project.\nOn the command line you can set it with the -projectCP option\n");
        Help.execute(options);
        return results;
    }
    if (line.hasOption("class")) {
        results.addAll(generateTests(strategy, line.getOptionValue("class"), javaOpts));
    } else if (line.hasOption("prefix")) {
        results.addAll(generateTestsPrefix(strategy, line.getOptionValue("prefix"), javaOpts));
    } else if (line.hasOption("target")) {
        String target = line.getOptionValue("target");
        results.addAll(generateTestsTarget(strategy, target, javaOpts));
    } else if (EvoSuite.hasLegacyTargets()) {
        results.addAll(generateTestsLegacy(strategy, javaOpts));
    } else {
        LoggingUtils.getEvoLogger().error("Please specify either target class ('-class' option), prefix ('-prefix' option), or " + "classpath entry ('-target' option)\n");
        Help.execute(options);
    }
    return results;
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) Strategy(org.evosuite.Properties.Strategy) ResourceList(org.evosuite.classpath.ResourceList)

Example 8 with TestGenerationResult

use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.

the class TestSuiteGenerator method generateTestSuite.

/**
 * Generate a test suite for the target class
 *
 * @return a {@link java.lang.String} object.
 */
public TestGenerationResult generateTestSuite() {
    LoggingUtils.getEvoLogger().info("* Analyzing classpath: ");
    ClientServices.getInstance().getClientNode().changeState(ClientState.INITIALIZATION);
    // Deactivate loop counter to make sure classes initialize properly
    LoopCounter.getInstance().setActive(false);
    TestCaseExecutor.initExecutor();
    try {
        initializeTargetClass();
    } catch (Throwable e) {
        // If the bytecode for a method exceeds 64K, Java will complain
        // Very often this is due to mutation instrumentation, so this dirty
        // hack adds a fallback mode without mutation.
        // This currently breaks statistics and assertions, so we have to also set these properties
        boolean error = true;
        String message = e.getMessage();
        if (message != null && (message.contains("Method code too large") || message.contains("Class file too large"))) {
            LoggingUtils.getEvoLogger().info("* Instrumentation exceeds Java's 64K limit per method in target class");
            Properties.Criterion[] newCriteria = Arrays.stream(Properties.CRITERION).filter(t -> !t.equals(Properties.Criterion.STRONGMUTATION) && !t.equals(Properties.Criterion.WEAKMUTATION) && !t.equals(Properties.Criterion.MUTATION)).toArray(Properties.Criterion[]::new);
            if (newCriteria.length < Properties.CRITERION.length) {
                TestGenerationContext.getInstance().resetContext();
                LoggingUtils.getEvoLogger().info("* Attempting re-instrumentation without mutation");
                Properties.CRITERION = newCriteria;
                if (Properties.NEW_STATISTICS) {
                    LoggingUtils.getEvoLogger().info("* Deactivating EvoSuite statistics because of instrumentation problem");
                    Properties.NEW_STATISTICS = false;
                }
                try {
                    initializeTargetClass();
                    error = false;
                } catch (Throwable t) {
                // No-op, error handled below
                }
                if (Properties.ASSERTIONS && Properties.ASSERTION_STRATEGY == AssertionStrategy.MUTATION) {
                    LoggingUtils.getEvoLogger().info("* Deactivating assertion minimization because mutation instrumentation does not work");
                    Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
                }
            }
        }
        if (error) {
            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 TestGenerationResultBuilder.buildErrorResult(e.getMessage() != null ? e.getMessage() : e.toString());
        }
    } finally {
        if (Properties.RESET_STATIC_FIELDS) {
            configureClassReInitializer();
        }
        // Once class loading is complete we can start checking loops
        // without risking to interfere with class initialisation
        LoopCounter.getInstance().setActive(true);
    }
    /*
		 * Initialises the object pool with objects carved from SELECTED_JUNIT
		 * classes
		 */
    // TODO: Do parts of this need to be wrapped into sandbox statements?
    ObjectPoolManager.getInstance();
    LoggingUtils.getEvoLogger().info("* Generating tests for class " + Properties.TARGET_CLASS);
    TestSuiteGeneratorHelper.printTestCriterion();
    if (!Properties.hasTargetClassBeenLoaded()) {
        // initialization failed, then build error message
        return TestGenerationResultBuilder.buildErrorResult("Could not load target class");
    }
    if (Properties.isRegression() && Properties.REGRESSION_SKIP_SIMILAR) {
        // Sanity checks
        if (Properties.getTargetClassRegression(true) == null) {
            Properties.IGNORE_MISSING_STATISTICS = false;
            logger.error("class {} was not on the regression projectCP", Properties.TARGET_CLASS);
            return TestGenerationResultBuilder.buildErrorResult("Could not load target regression class");
        }
        if (!ResourceList.getInstance(TestGenerationContext.getInstance().getRegressionClassLoaderForSUT()).hasClass(Properties.TARGET_CLASS)) {
            Properties.IGNORE_MISSING_STATISTICS = false;
            logger.error("class {} was not on the regression_cp", Properties.TARGET_CLASS);
            return TestGenerationResultBuilder.buildErrorResult("Class " + Properties.TARGET_CLASS + " did not exist on regression classpath");
        }
        boolean areDifferent = RegressionClassDiff.differentAcrossClassloaders(Properties.TARGET_CLASS);
        // TODO: report it to master to create a nice regression report
        if (!areDifferent) {
            Properties.IGNORE_MISSING_STATISTICS = false;
            logger.error("class {} was equal on both versions", Properties.TARGET_CLASS);
            return TestGenerationResultBuilder.buildErrorResult("Class " + Properties.TARGET_CLASS + " was not changed between the two versions");
        }
    }
    if (Properties.isRegression() && Properties.REGRESSION_SKIP_DIFFERENT_CFG) {
        // Does the class have the same CFG across the two versions of the program?
        boolean sameBranches = RegressionClassDiff.sameCFG();
        if (!sameBranches) {
            Properties.IGNORE_MISSING_STATISTICS = false;
            logger.error("Could not match the branches across the two versions.");
            return TestGenerationResultBuilder.buildErrorResult("Could not match the branches across the two versions.");
        }
    }
    TestSuiteChromosome testCases = generateTests();
    postProcessTests(testCases);
    ClientServices.getInstance().getClientNode().publishPermissionStatistics();
    PermissionStatistics.getInstance().printStatistics(LoggingUtils.getEvoLogger());
    // progressMonitor.setCurrentPhase("Writing JUnit test cases");
    TestGenerationResult result = writeJUnitTestsAndCreateResult(testCases);
    writeJUnitFailingTests();
    TestCaseExecutor.pullDown();
    /*
		 * TODO: when we will have several processes running in parallel, we ll
		 * need to handle the gathering of the statistics.
		 */
    ClientServices.getInstance().getClientNode().changeState(ClientState.WRITING_STATISTICS);
    LoggingUtils.getEvoLogger().info("* Done!");
    LoggingUtils.getEvoLogger().info("");
    return result;
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) Criterion(org.evosuite.Properties.Criterion)

Example 9 with TestGenerationResult

use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.

the class TestGenerationJob method launchProcess.

private ArrayList<TestGenerationResult> launchProcess(String[] evoSuiteOptions) throws IOException {
    EvoSuite evosuite = new EvoSuite();
    Vector<String> javaCmd = new Vector<String>();
    // javaCmd.add("java");
    // javaCmd.add("-jar");
    // javaCmd.add(TestGenerationAction.getEvoSuiteJar());
    Collections.addAll(javaCmd, evoSuiteOptions);
    String[] command = javaCmd.toArray(new String[] {});
    @SuppressWarnings("unchecked") List<List<TestGenerationResult>> results = (List<List<TestGenerationResult>>) evosuite.parseCommandLine(command);
    ArrayList<TestGenerationResult> tgrs = new ArrayList<TestGenerationResult>();
    System.out.println("Results: " + results.size());
    for (List<TestGenerationResult> list : results) {
        for (TestGenerationResult result : list) {
            tgrs.add(result);
            System.out.println("Covered lines: " + result.getCoveredLines());
        }
    }
    return tgrs;
// ProcessBuilder builder = new ProcessBuilder(command);
// builder.directory(new File(baseDir));
// builder.redirectErrorStream(true);
// 
// Process process = builder.start();
// InputStream stdout = process.getInputStream();
// do {
// readInputStream("EvoSuite process output - ", stdout);
// } while (!isFinished(process));
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) EvoSuite(org.evosuite.EvoSuite) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ResourceList(org.evosuite.classpath.ResourceList) Vector(java.util.Vector)

Example 10 with TestGenerationResult

use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.

the class ClientNodeImpl method startNewSearch.

@Override
public void startNewSearch() throws RemoteException, IllegalStateException {
    if (!state.equals(ClientState.NOT_STARTED)) {
        throw new IllegalArgumentException("Search has already been started");
    }
    /*
		 * Needs to be done on separated thread, otherwise the master will block on this
		 * function call until end of the search, even if it is on remote process
		 */
    searchExecutor.submit(new Runnable() {

        @Override
        public void run() {
            changeState(ClientState.STARTED);
            // Before starting search, let's activate the sandbox
            if (Properties.SANDBOX) {
                Sandbox.initializeSecurityManagerForSUT();
            }
            List<TestGenerationResult> results = new ArrayList<TestGenerationResult>();
            try {
                // Starting a new search
                TestSuiteGenerator generator = new TestSuiteGenerator();
                results.add(generator.generateTestSuite());
                // TODO: Why?
                // GeneticAlgorithm<?> ga = generator.getEmployedGeneticAlgorithm();
                masterNode.evosuite_collectTestGenerationResult(clientRmiIdentifier, results);
            } catch (Throwable t) {
                logger.error("Error when generating tests for: " + Properties.TARGET_CLASS + " with seed " + Randomness.getSeed() + ". Configuration id : " + Properties.CONFIGURATION_ID, t);
                results.add(TestGenerationResultBuilder.buildErrorResult("Error when generating tests for: " + Properties.TARGET_CLASS + ": " + t));
            }
            changeState(ClientState.DONE);
            if (Properties.SANDBOX) {
                /*
					 * Note: this is mainly done for debugging purposes, to simplify how test cases are run/written 
					 */
                Sandbox.resetDefaultSecurityManager();
            }
        /*
				 * System is special due to the handling of properties
				 * 
				 *  TODO: re-add it once we save JUnit code in the 
				 *  best individual. Otherwise, we wouldn't
				 *  be able to properly create the JUnit files in the
				 *  system test cases after the search
				 */
        // org.evosuite.runtime.System.fullReset();
        }
    });
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) TestSuiteGenerator(org.evosuite.TestSuiteGenerator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TestGenerationResult (org.evosuite.result.TestGenerationResult)13 List (java.util.List)6 EvoSuite (org.evosuite.EvoSuite)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 ResourceList (org.evosuite.classpath.ResourceList)4 File (java.io.File)3 IOException (java.io.IOException)2 CoreException (org.eclipse.core.runtime.CoreException)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 PartInitException (org.eclipse.ui.PartInitException)2 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)2 FileNotFoundException (java.io.FileNotFoundException)1 RemoteException (java.rmi.RemoteException)1 HashMap (java.util.HashMap)1 Vector (java.util.Vector)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1