Trac #117: Fixed potential NPE at shutdown when running agent in tcpserver mode.
diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpConnectionTest.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpConnectionTest.java
index e346ae6..20d34ef 100644
--- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpConnectionTest.java
+++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpConnectionTest.java
@@ -203,6 +203,17 @@
f.get();
}
+ @Test
+ public void testLocalDumpWithoutInit() throws Exception {
+ final TcpConnection con = new TcpConnection(
+ mockConnection.getSocketA(), runtime);
+ // Must not write any data as we're not initialized:
+ con.writeExecutionData();
+
+ assertEquals(0, mockConnection.getSocketB().getInputStream()
+ .available());
+ }
+
private void readAndAssertData() throws IOException {
final RemoteControlReader remoteReader = new RemoteControlReader(
mockConnection.getSocketB().getInputStream());
diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java
index 9a8c08f..24f54c1 100644
--- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java
+++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/controller/TcpConnection.java
@@ -37,15 +37,19 @@
private RemoteControlReader reader;
+ private boolean initialized;
+
public TcpConnection(Socket socket, IRuntime runtime) {
this.socket = socket;
this.runtime = runtime;
+ this.initialized = false;
}
public void init() throws IOException {
this.writer = new RemoteControlWriter(socket.getOutputStream());
this.reader = new RemoteControlReader(socket.getInputStream());
this.reader.setRemoteCommandVisitor(this);
+ this.initialized = true;
}
/**
@@ -67,12 +71,13 @@
}
/**
- * Dumps the current execution data if the underlying socket is still open.
+ * Dumps the current execution data if the connection is already initialized
+ * and the underlying socket is still open.
*
* @throws IOException
*/
public void writeExecutionData() throws IOException {
- if (!socket.isClosed()) {
+ if (initialized && !socket.isClosed()) {
visitDumpCommand(true, false);
}
}
diff --git a/org.jacoco.doc/docroot/doc/changes.html b/org.jacoco.doc/docroot/doc/changes.html
index 5bcc2c8..3eead4f 100644
--- a/org.jacoco.doc/docroot/doc/changes.html
+++ b/org.jacoco.doc/docroot/doc/changes.html
@@ -30,6 +30,8 @@
<ul>
<li>Better error message when multiple JaCoCo agents are specified
(Track #103).</li>
+ <li>Fixed potential NPE at shutdown when running agent in
+ <code>tcpserver</code> mode (Track #117).</li>
</ul>
<h2>Release 0.4.0 (2010/06/04)</h2>