aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.agent.rt.test/src/org
diff options
context:
space:
mode:
authorMarc Hoffmann <marc.hoffmann@sbb.ch>2013-01-08 18:57:10 +0100
committerMarc Hoffmann <marc.hoffmann@sbb.ch>2013-01-08 18:57:10 +0100
commit60147addf9937875f2ff01f71fe5fb046e006b3f (patch)
treea8e87c5261c1e28b16319d19b41f530123ccfc63 /org.jacoco.agent.rt.test/src/org
parent113816bd78744e268b58cdb496c2b1131849eb14 (diff)
downloadjacoco-60147addf9937875f2ff01f71fe5fb046e006b3f.tar.gz
More tests, update change log.
Diffstat (limited to 'org.jacoco.agent.rt.test/src/org')
-rw-r--r--org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java168
1 files changed, 147 insertions, 21 deletions
diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java
index e98dcfed..495b5cef 100644
--- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java
+++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/AgentTest.java
@@ -14,19 +14,26 @@ package org.jacoco.agent.rt.internal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
-import org.jacoco.agent.rt.internal.Agent;
-import org.jacoco.agent.rt.internal.IExceptionLogger;
+import org.jacoco.agent.rt.internal.controller.IAgentController;
import org.jacoco.agent.rt.internal.controller.LocalController;
import org.jacoco.agent.rt.internal.controller.MBeanController;
import org.jacoco.agent.rt.internal.controller.TcpClientController;
import org.jacoco.agent.rt.internal.controller.TcpServerController;
+import org.jacoco.core.JaCoCo;
+import org.jacoco.core.data.ExecutionDataReader;
+import org.jacoco.core.data.ExecutionDataStore;
+import org.jacoco.core.data.SessionInfoStore;
import org.jacoco.core.runtime.AgentOptions;
import org.jacoco.core.runtime.AgentOptions.OutputMode;
+import org.jacoco.core.runtime.RuntimeData;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -38,32 +45,43 @@ public class AgentTest implements IExceptionLogger {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
+
+ private AgentOptions options;
+ private File execfile;
+
private Exception exception;
+ @Before
+ public void setup() {
+ options = new AgentOptions();
+ execfile = new File(folder.getRoot(), "jacoco.exec");
+ options.setOutput(OutputMode.file);
+ options.setDestfile(execfile.getAbsolutePath());
+ }
+
@Test
public void testCreateController() {
- AgentOptions options = new AgentOptions();
Agent agent = new Agent(options, this);
options.setOutput(OutputMode.file);
- assertTrue(agent.createAgentController() instanceof LocalController);
+ assertEquals(LocalController.class, agent.createAgentController()
+ .getClass());
options.setOutput(OutputMode.tcpserver);
- assertTrue(agent.createAgentController() instanceof TcpServerController);
+ assertEquals(TcpServerController.class, agent.createAgentController()
+ .getClass());
options.setOutput(OutputMode.tcpclient);
- assertTrue(agent.createAgentController() instanceof TcpClientController);
+ assertEquals(TcpClientController.class, agent.createAgentController()
+ .getClass());
options.setOutput(OutputMode.mbean);
- assertTrue(agent.createAgentController() instanceof MBeanController);
+ assertEquals(MBeanController.class, agent.createAgentController()
+ .getClass());
}
@Test
public void testStartupShutdown() throws Exception {
- final File execfile = new File(folder.getRoot(), "jacoco.exec");
- AgentOptions options = new AgentOptions();
- options.setOutput(OutputMode.file);
- options.setDestfile(execfile.getAbsolutePath());
options.setSessionId("testsession");
Agent agent = new Agent(options, this);
agent.startup();
@@ -78,11 +96,33 @@ public class AgentTest implements IExceptionLogger {
}
@Test
+ public void testShutdownWithException() throws Exception {
+ final Exception expected = new Exception();
+ Agent agent = new Agent(options, this) {
+ @Override
+ IAgentController createAgentController() {
+ return new IAgentController() {
+ 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, exception);
+ }
+
+ @Test
public void testNoSessionId() throws Exception {
- final File execfile = new File(folder.getRoot(), "jacoco.exec");
- AgentOptions options = new AgentOptions();
- options.setOutput(OutputMode.file);
- options.setDestfile(execfile.getAbsolutePath());
Agent agent = new Agent(options, this);
final String defaultId = agent.getData().getSessionId();
@@ -95,10 +135,6 @@ public class AgentTest implements IExceptionLogger {
@Test
public void testNoDumpOnExit() throws Exception {
- final File execfile = new File(folder.getRoot(), "jacoco.exec");
- AgentOptions options = new AgentOptions();
- options.setOutput(OutputMode.file);
- options.setDestfile(execfile.getAbsolutePath());
options.setDumpOnExit(false);
Agent agent = new Agent(options, this);
@@ -111,8 +147,6 @@ public class AgentTest implements IExceptionLogger {
@Test
public void testInvalidExecFile() throws Exception {
- AgentOptions options = new AgentOptions();
- options.setOutput(OutputMode.file);
options.setDestfile(folder.getRoot().getAbsolutePath());
Agent agent = new Agent(options, this);
@@ -121,6 +155,98 @@ public class AgentTest implements IExceptionLogger {
assertTrue(exception instanceof IOException);
}
+ @Test
+ public void testGetVersion() {
+ Agent agent = new Agent(options, this);
+ assertEquals(JaCoCo.VERSION, agent.getVersion());
+ }
+
+ @Test
+ public void testGetSetSessionId() throws IOException {
+ Agent agent = new Agent(options, this);
+ agent.startup();
+ agent.setSessionId("agenttestid");
+ assertEquals("agenttestid", agent.getSessionId());
+
+ SessionInfoStore sessionStore = new SessionInfoStore();
+ ExecutionDataReader reader = new ExecutionDataReader(
+ new ByteArrayInputStream(agent.getExecutionData(false)));
+ reader.setSessionInfoVisitor(sessionStore);
+ reader.read();
+ assertEquals("agenttestid", sessionStore.getInfos().get(0).getId());
+ }
+
+ @Test
+ public void testReset() {
+ Agent agent = new Agent(options, this);
+
+ boolean[] probes = agent.getData()
+ .getExecutionData(Long.valueOf(0x12345678), "Foo", 1)
+ .getProbes();
+ probes[0] = true;
+
+ agent.reset();
+
+ assertFalse(probes[0]);
+ }
+
+ @Test
+ public void testGetExecutionData() throws IOException {
+ options.setSessionId("agenttestid");
+ Agent agent = new Agent(options, this);
+ agent.startup();
+
+ boolean[] probes = agent.getData()
+ .getExecutionData(Long.valueOf(0x12345678), "Foo", 1)
+ .getProbes();
+ probes[0] = true;
+
+ byte[] data = agent.getExecutionData(true);
+
+ // ensure reset has been executed
+ assertFalse(probes[0]);
+
+ ExecutionDataStore execStore = new ExecutionDataStore();
+ SessionInfoStore sessionStore = new SessionInfoStore();
+
+ ExecutionDataReader reader = new ExecutionDataReader(
+ new ByteArrayInputStream(data));
+ reader.setExecutionDataVisitor(execStore);
+ reader.setSessionInfoVisitor(sessionStore);
+ reader.read();
+
+ assertEquals("Foo", execStore.get(0x12345678).getName());
+ assertEquals(1, sessionStore.getInfos().size());
+ assertEquals("agenttestid", sessionStore.getInfos().get(0).getId());
+ }
+
+ @Test
+ public void testDump() throws Exception {
+ final boolean[] called = new boolean[1];
+ Agent agent = new Agent(options, this) {
+ @Override
+ IAgentController createAgentController() {
+ return new IAgentController() {
+ public void startup(AgentOptions options, RuntimeData data) {
+ }
+
+ public void shutdown() throws Exception {
+ }
+
+ public void writeExecutionData(boolean reset) {
+ assertTrue(reset);
+ called[0] = true;
+ }
+ };
+ }
+ };
+ agent.startup();
+
+ agent.dump(true);
+
+ assertTrue(called[0]);
+ }
+
// === IExceptionLogger ===
public void logExeption(Exception ex) {