Search in sources :

Example 1 with IRuntime

use of org.jacoco.core.runtime.IRuntime in project powermock by powermock.

the class JacocoCoverageTest method runTargetTest.

private void runTargetTest(RuntimeData data) throws Exception {
    IRuntime runtime = new LoggerRuntime();
    instrumentClasses(runtime);
    runtime.startup(data);
    JUnitCore.runClasses(TargetTest.class);
    runtime.shutdown();
    restoreOriginalClasses();
}
Also used : LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) IRuntime(org.jacoco.core.runtime.IRuntime)

Example 2 with IRuntime

use of org.jacoco.core.runtime.IRuntime in project jacoco by jacoco.

the class PreMain method premain.

/**
 * This method is called by the JVM to initialize Java agents.
 *
 * @param options
 *            agent options
 * @param inst
 *            instrumentation callback provided by the JVM
 * @throws Exception
 *             in case initialization fails
 */
public static void premain(final String options, final Instrumentation inst) throws Exception {
    final AgentOptions agentOptions = new AgentOptions(options);
    final Agent agent = Agent.getInstance(agentOptions);
    final IRuntime runtime = createRuntime(inst);
    runtime.startup(agent.getData());
    inst.addTransformer(new CoverageTransformer(runtime, agentOptions, IExceptionLogger.SYSTEM_ERR));
}
Also used : AgentOptions(org.jacoco.core.runtime.AgentOptions) IRuntime(org.jacoco.core.runtime.IRuntime)

Example 3 with IRuntime

use of org.jacoco.core.runtime.IRuntime in project jacoco by jacoco.

the class CoreTutorial method execute.

/**
 * Run this example.
 *
 * @throws Exception
 *             in case of errors
 */
public void execute() throws Exception {
    final String targetName = TestTarget.class.getName();
    // For instrumentation and runtime we need a IRuntime instance
    // to collect execution data:
    final IRuntime runtime = new LoggerRuntime();
    // The Instrumenter creates a modified version of our test target class
    // that contains additional probes for execution data recording:
    final Instrumenter instr = new Instrumenter(runtime);
    InputStream original = getTargetClass(targetName);
    final byte[] instrumented = instr.instrument(original, targetName);
    original.close();
    // Now we're ready to run our instrumented class and need to startup the
    // runtime first:
    final RuntimeData data = new RuntimeData();
    runtime.startup(data);
    // In this tutorial we use a special class loader to directly load the
    // instrumented class definition from a byte[] instances.
    final MemoryClassLoader memoryClassLoader = new MemoryClassLoader();
    memoryClassLoader.addDefinition(targetName, instrumented);
    final Class<?> targetClass = memoryClassLoader.loadClass(targetName);
    // Here we execute our test target class through its Runnable interface:
    final Runnable targetInstance = (Runnable) targetClass.newInstance();
    targetInstance.run();
    // At the end of test execution we collect execution data and shutdown
    // the runtime:
    final ExecutionDataStore executionData = new ExecutionDataStore();
    final SessionInfoStore sessionInfos = new SessionInfoStore();
    data.collect(executionData, sessionInfos, false);
    runtime.shutdown();
    // Together with the original class definition we can calculate coverage
    // information:
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
    original = getTargetClass(targetName);
    analyzer.analyzeClass(original, targetName);
    original.close();
    // Let's dump some metrics and line coverage information:
    for (final IClassCoverage cc : coverageBuilder.getClasses()) {
        out.printf("Coverage of class %s%n", cc.getName());
        printCounter("instructions", cc.getInstructionCounter());
        printCounter("branches", cc.getBranchCounter());
        printCounter("lines", cc.getLineCounter());
        printCounter("methods", cc.getMethodCounter());
        printCounter("complexity", cc.getComplexityCounter());
        for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) {
            out.printf("Line %s: %s%n", Integer.valueOf(i), getColor(cc.getLine(i).getStatus()));
        }
    }
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) InputStream(java.io.InputStream) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) Analyzer(org.jacoco.core.analysis.Analyzer) IRuntime(org.jacoco.core.runtime.IRuntime) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) Instrumenter(org.jacoco.core.instr.Instrumenter) SessionInfoStore(org.jacoco.core.data.SessionInfoStore)

Example 4 with IRuntime

use of org.jacoco.core.runtime.IRuntime in project jacoco by jacoco.

the class InstrumentationSizeSzenario method run.

public void run(IPerfOutput output) throws Exception {
    final IRuntime runtime = new LoggerRuntime();
    ClassReader reader = new ClassReader(TargetLoader.getClassData(target));
    final Instrumenter instr = new Instrumenter(runtime);
    instr.instrument(reader);
    output.writeByteResult("instrumented class", instr.instrument(reader).length, reader.b.length);
}
Also used : LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) ClassReader(org.objectweb.asm.ClassReader) Instrumenter(org.jacoco.core.instr.Instrumenter) IRuntime(org.jacoco.core.runtime.IRuntime)

Example 5 with IRuntime

use of org.jacoco.core.runtime.IRuntime in project jacoco by jacoco.

the class ClassFileVersionsTest method testVersion.

private void testVersion(int version, boolean frames) throws IOException {
    final byte[] original = createClass(version);
    IRuntime runtime = new SystemPropertiesRuntime();
    Instrumenter instrumenter = new Instrumenter(runtime);
    byte[] instrumented = instrumenter.instrument(original, "TestTarget");
    assertFrames(instrumented, frames);
}
Also used : SystemPropertiesRuntime(org.jacoco.core.runtime.SystemPropertiesRuntime) Instrumenter(org.jacoco.core.instr.Instrumenter) IRuntime(org.jacoco.core.runtime.IRuntime)

Aggregations

IRuntime (org.jacoco.core.runtime.IRuntime)8 Instrumenter (org.jacoco.core.instr.Instrumenter)6 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)4 SystemPropertiesRuntime (org.jacoco.core.runtime.SystemPropertiesRuntime)3 ClassReader (org.objectweb.asm.ClassReader)3 RuntimeData (org.jacoco.core.runtime.RuntimeData)2 InputStream (java.io.InputStream)1 Callable (java.util.concurrent.Callable)1 Analyzer (org.jacoco.core.analysis.Analyzer)1 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)1 IClassCoverage (org.jacoco.core.analysis.IClassCoverage)1 ExecutionDataStore (org.jacoco.core.data.ExecutionDataStore)1 SessionInfoStore (org.jacoco.core.data.SessionInfoStore)1 AgentOptions (org.jacoco.core.runtime.AgentOptions)1 TargetLoader (org.jacoco.core.test.TargetLoader)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 MethodNode (org.objectweb.asm.tree.MethodNode)1