Search in sources :

Example 6 with RuntimeData

use of org.jacoco.core.runtime.RuntimeData 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 7 with RuntimeData

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

the class JacocoCoverageTest method jacocoOfflineInstShouldCalculateCoverageAfterPowerMockTransformation.

@Test
public void jacocoOfflineInstShouldCalculateCoverageAfterPowerMockTransformation() throws Exception {
    final RuntimeData data = new RuntimeData();
    runTargetTest(data);
    final CoverageBuilder coverageBuilder = collectCoverage(getExecutionDataStore(data));
    assertCodeCoverage(coverageBuilder);
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) Test(org.junit.Test)

Example 8 with RuntimeData

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

the class TcpConnectionTest method setup.

@Before
@Override
public void setup() throws Exception {
    super.setup();
    mockConnection = new MockSocketConnection();
    data = new RuntimeData();
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) Before(org.junit.Before)

Example 9 with RuntimeData

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

the class ExecuteInstrumentedCodeScenario method getInstrumentedCallable.

@Override
@SuppressWarnings("unchecked")
protected Callable<Void> getInstrumentedCallable() throws Exception {
    ClassReader reader = new ClassReader(TargetLoader.getClassData(target));
    IRuntime runtime = new LoggerRuntime();
    runtime.startup(new RuntimeData());
    final Instrumenter instr = new Instrumenter(runtime);
    final byte[] instrumentedBuffer = instr.instrument(reader);
    final TargetLoader loader = new TargetLoader();
    return (Callable<Void>) loader.add(target, instrumentedBuffer).newInstance();
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) TargetLoader(org.jacoco.core.test.TargetLoader) ClassReader(org.objectweb.asm.ClassReader) Instrumenter(org.jacoco.core.instr.Instrumenter) Callable(java.util.concurrent.Callable) IRuntime(org.jacoco.core.runtime.IRuntime)

Example 10 with RuntimeData

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

the class CyclomaticComplexityTest method setup.

@Before
public void setup() throws Exception {
    data = new RuntimeData();
    runtime = new SystemPropertiesRuntime();
    runtime.startup(data);
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) SystemPropertiesRuntime(org.jacoco.core.runtime.SystemPropertiesRuntime) Before(org.junit.Before)

Aggregations

RuntimeData (org.jacoco.core.runtime.RuntimeData)14 AgentOptions (org.jacoco.core.runtime.AgentOptions)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 File (java.io.File)4 Before (org.junit.Before)4 FileOutput (org.jacoco.agent.rt.internal.output.FileOutput)3 TestListener (fr.inria.stamp.test.listener.TestListener)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 ExceptionRecorder (org.jacoco.agent.rt.internal.ExceptionRecorder)2 IAgentOutput (org.jacoco.agent.rt.internal.output.IAgentOutput)2 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)2 ExecutionDataStore (org.jacoco.core.data.ExecutionDataStore)2 SessionInfoStore (org.jacoco.core.data.SessionInfoStore)2 Instrumenter (org.jacoco.core.instr.Instrumenter)2 IRuntime (org.jacoco.core.runtime.IRuntime)2 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)2