Search in sources :

Example 1 with RuntimeData

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

the class FileOutputTest method testCreateDestFileOnStartup.

@Test
public void testCreateDestFileOnStartup() throws Exception {
    File destFile = folder.newFile("jacoco.exec");
    AgentOptions options = new AgentOptions();
    options.setDestfile(destFile.getAbsolutePath());
    FileOutput controller = new FileOutput();
    controller.startup(options, new RuntimeData());
    assertTrue("Execution data file should be created", destFile.exists());
    assertEquals("Execution data file should be empty", 0, destFile.length());
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) File(java.io.File) AgentOptions(org.jacoco.core.runtime.AgentOptions) Test(org.junit.Test)

Example 2 with RuntimeData

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

the class FileOutputTest method testWriteData.

@Test
public void testWriteData() throws Exception {
    File destFile = folder.newFile("jacoco.exec");
    AgentOptions options = new AgentOptions();
    options.setDestfile(destFile.getAbsolutePath());
    FileOutput controller = new FileOutput();
    controller.startup(options, new RuntimeData());
    controller.writeExecutionData(false);
    controller.shutdown();
    assertTrue("Execution data file should be created", destFile.exists());
    assertTrue("Execution data file should have contents", destFile.length() > 0);
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) File(java.io.File) AgentOptions(org.jacoco.core.runtime.AgentOptions) Test(org.junit.Test)

Example 3 with RuntimeData

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

the class AgentTest method shutdown_should_log_exception.

@Test
public void shutdown_should_log_exception() throws Exception {
    final Exception expected = new Exception();
    Agent agent = new Agent(options, this) {

        @Override
        IAgentOutput createAgentOutput() {
            return new IAgentOutput() {

                public void startup(AgentOptions options, RuntimeData data) {
                }

                public void shutdown() throws Exception {
                    throw expected;
                }

                public void writeExecutionData(boolean reset) {
                }
            };
        }
    };
    agent.startup();
    agent.shutdown();
    assertSame(expected, loggedException);
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) IAgentOutput(org.jacoco.agent.rt.internal.output.IAgentOutput) InstanceNotFoundException(javax.management.InstanceNotFoundException) AgentOptions(org.jacoco.core.runtime.AgentOptions) Test(org.junit.Test)

Example 4 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 5 with RuntimeData

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

the class TcpClientOutputTest method setup.

@Before
public void setup() throws Exception {
    logger = new ExceptionRecorder();
    final MockSocketConnection con = new MockSocketConnection();
    localSocket = con.getSocketA();
    remoteSocket = con.getSocketB();
    remoteWriter = new RemoteControlWriter(remoteSocket.getOutputStream());
    controller = new TcpClientOutput(logger) {

        @Override
        protected Socket createSocket(AgentOptions options) throws IOException {
            return localSocket;
        }
    };
    data = new RuntimeData();
    controller.startup(new AgentOptions(), data);
    remoteReader = new RemoteControlReader(remoteSocket.getInputStream());
}
Also used : RemoteControlWriter(org.jacoco.core.runtime.RemoteControlWriter) RuntimeData(org.jacoco.core.runtime.RuntimeData) ExceptionRecorder(org.jacoco.agent.rt.internal.ExceptionRecorder) IOException(java.io.IOException) AgentOptions(org.jacoco.core.runtime.AgentOptions) Socket(java.net.Socket) MockSocket(org.jacoco.agent.rt.internal.output.MockSocketConnection.MockSocket) RemoteControlReader(org.jacoco.core.runtime.RemoteControlReader) Before(org.junit.Before)

Aggregations

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