Search in sources :

Example 1 with CaptureLog

use of org.evosuite.testcarver.capture.CaptureLog in project evosuite by EvoSuite.

the class CaptureLogAnalyzer method analyze.

@SuppressWarnings("rawtypes")
public void analyze(final CaptureLog originalLog, final ICodeGenerator generator, final Set<Class<?>> blackList, final Class<?>... observedClasses) {
    if (originalLog == null)
        throw new IllegalArgumentException("captured log must not be null");
    if (generator == null)
        throw new IllegalArgumentException("code generator must not be null");
    if (blackList == null)
        throw new IllegalArgumentException("set containing black listed classes must not be null");
    if (observedClasses == null)
        throw new IllegalArgumentException("array of observed classes must not be null");
    if (observedClasses.length == 0)
        throw new IllegalArgumentException("array of observed classes must not be empty");
    final CaptureLog log = originalLog.clone();
    final HashSet<String> observedClassNames = extractObservedClassNames(observedClasses);
    CaptureLogAnalyzerException.check(!CollectionUtil.isNullOrEmpty(observedClassNames), "could not extract class names for ", Arrays.toString(observedClasses));
    final List<Integer> targetOIDs = log.getTargetOIDs(observedClassNames);
    if (targetOIDs.isEmpty()) {
        logger.info("could not find any oids for {} -> {} ==> no code is generated\n", observedClassNames, Arrays.toString(observedClasses));
        return;
    } else {
        logger.debug("Target oids: {}", targetOIDs);
    }
    final int[] oidExchange = analyzeLog(generator, blackList, log, targetOIDs);
    logger.debug("Going to postprocess stage");
    postProcessLog(originalLog, generator, blackList, log, oidExchange, observedClasses);
}
Also used : CaptureLog(org.evosuite.testcarver.capture.CaptureLog)

Example 2 with CaptureLog

use of org.evosuite.testcarver.capture.CaptureLog in project evosuite by EvoSuite.

the class CarvingRunListener method testFinished.

@Override
public void testFinished(Description description) throws Exception {
    final CaptureLog log = Capturer.stopCapture();
    if (TimeController.getInstance().isThereStillTimeInThisPhase()) {
        LoggingUtils.getEvoLogger().info(" - Carving test {}.{}", description.getClassName(), description.getMethodName());
        this.processLog(description, log);
    }
    Capturer.clear();
}
Also used : CaptureLog(org.evosuite.testcarver.capture.CaptureLog)

Example 3 with CaptureLog

use of org.evosuite.testcarver.capture.CaptureLog in project evosuite by EvoSuite.

the class CarvingTestRunner method stopCapture.

/**
 * Stops capture phase and creates carved TestCase
 */
private void stopCapture() {
    final CaptureLog log = Capturer.stopCapture();
    this.processLog(log);
    Capturer.clear();
}
Also used : CaptureLog(org.evosuite.testcarver.capture.CaptureLog)

Example 4 with CaptureLog

use of org.evosuite.testcarver.capture.CaptureLog in project evosuite by EvoSuite.

the class PostProcessor method process.

/**
 * @param logs   logs of captured interaction
 * @param packages  package names associated with logs. Mapping logs.get(i) belongs to packages.get(i)
 * @throws IOException
 */
public static void process(final List<CaptureLog> logs, final List<String> packages, final List<Class<?>[]> observedClasses) throws IOException {
    if (logs == null) {
        throw new NullPointerException("list of CaptureLogs must not be null");
    }
    if (packages == null) {
        throw new NullPointerException("list of package names associated with logs must not be null");
    }
    if (observedClasses == null) {
        throw new NullPointerException("list of classes to be observed must not be null");
    }
    final int size = logs.size();
    if (packages.size() != size || observedClasses.size() != size) {
        throw new IllegalArgumentException("given lists must have same size");
    }
    // create post-processing sources in os specific temp folder
    final File tempDir = new File(System.getProperty("java.io.tmpdir"), "postprocessing_" + System.currentTimeMillis());
    tempDir.mkdir();
    CaptureLog log;
    String packageName;
    Class<?>[] classes;
    String targetFolder;
    File targetFile;
    final StringBuilder testClassNameBuilder = new StringBuilder();
    String testClassName;
    for (int i = 0; i < size; i++) {
        // =============== prepare carved test for post-processing ================================================
        packageName = packages.get(i);
        targetFolder = packageName.replace(".", File.separator);
        classes = observedClasses.get(i);
        if (classes.length == 0) {
            throw new IllegalArgumentException("there must be at least one class to be observed");
        }
        // for(int j = 0; j < classes.length; j++)
        // {
        // testClassNameBuilder.append(classes[j].getSimpleName());
        // if(testClassNameBuilder.length() >= 10)
        // {
        // break;
        // }
        // }
        testClassNameBuilder.append("CarvedTest");
        log = logs.get(i);
        long s = System.currentTimeMillis();
        logger.debug(">>>> (postprocess) start test create ");
        targetFile = new File(tempDir, targetFolder);
        targetFile.mkdirs();
        testClassName = getFreeClassName(targetFile, testClassNameBuilder.toString());
        targetFile = new File(targetFile, testClassName + ".java");
        // write out test files containing post-processing statements
        writeTest(log, packageName, testClassName, classes, targetFile, true);
        logger.debug(">>>> (postprocess) end test creation -> " + (System.currentTimeMillis() - s) / 1000);
        // =============== compile generated post-processing test ================================================
        final JavaCompiler compiler = new EclipseCompiler();
        // final JavaCompiler 			  compiler    = ToolProvider.getSystemJavaCompiler();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File[] { targetFile }));
        // --- add modified bins (see Transformer and ClassPreparer.createPreparedBin()) to class path
        String classPath = Configuration.INSTANCE.getProperty(Configuration.MODIFIED_BIN_LOC);
        classPath += File.pathSeparator + System.getProperty("java.class.path");
        final Boolean wasCompilationSuccess = compiler.getTask(null, fileManager, null, Arrays.asList(new String[] { "-cp", classPath }), null, compilationUnits).call();
        if (!wasCompilationSuccess) {
            logger.error("Compilation was not not successful for " + targetFile);
            fileManager.close();
            continue;
        }
        fileManager.close();
        // =============== execute + observe post-processing test run ================================================
        final PostProcessorClassLoader cl = new PostProcessorClassLoader(tempDir);
        final Class<?> testClass = cl.findClass(packageName + '.' + testClassName);
        BlockJUnit4ClassRunner testRunner;
        try {
            testRunner = new BlockJUnit4ClassRunner(testClass);
            testRunner.run(new RunNotifier());
        } catch (InitializationError e) {
            logger.error("" + e, e);
        }
        // ============== generate final test file ================================================================
        final String targetDir = Configuration.INSTANCE.getProperty(Configuration.GEN_TESTS_LOC);
        targetFile = new File(new File(targetDir), targetFolder);
        targetFile.mkdirs();
        testClassName = getFreeClassName(targetFile, testClassNameBuilder.toString());
        targetFile = new File(targetFile, testClassName + ".java");
        writeTest(log, packageName, testClassName, classes, targetFile, false);
        // recycle StringBuilder for testClassName
        testClassNameBuilder.setLength(0);
    }
    // clean up post-processing stuff
    tempDir.delete();
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) InitializationError(org.junit.runners.model.InitializationError) CaptureLog(org.evosuite.testcarver.capture.CaptureLog) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) File(java.io.File)

Aggregations

CaptureLog (org.evosuite.testcarver.capture.CaptureLog)4 File (java.io.File)1 EclipseCompiler (org.eclipse.jdt.internal.compiler.tool.EclipseCompiler)1 RunNotifier (org.junit.runner.notification.RunNotifier)1 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)1 InitializationError (org.junit.runners.model.InitializationError)1