diff options
Diffstat (limited to 'org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller')
7 files changed, 978 insertions, 978 deletions
diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/ExecutorTestBase.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/ExecutorTestBase.java index e3c3d022..050eca0b 100644 --- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/ExecutorTestBase.java +++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/ExecutorTestBase.java @@ -1,59 +1,59 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import static org.junit.Assert.fail;
-
-import java.util.concurrent.Executor;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import org.junit.After;
-import org.junit.Before;
-
-/**
- * Unit tests base for tests that need an {@link Executor} for multithreaded
- * test scenarios.
- */
-public abstract class ExecutorTestBase {
-
- protected ExecutorService executor;
-
- @Before
- public void setup() throws Exception {
- executor = Executors.newSingleThreadExecutor();
- }
-
- @After
- public void teardown() throws Exception {
- executor.shutdown();
- }
-
- /**
- * Asserts that the given future blocks.
- *
- * @param future
- * future to test
- * @throws Exception
- */
- protected void assertBlocks(final Future<?> future) throws Exception {
- try {
- future.get(10, TimeUnit.MILLISECONDS);
- fail("Operation should block");
- } catch (TimeoutException e) {
- }
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import static org.junit.Assert.fail; + +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.junit.After; +import org.junit.Before; + +/** + * Unit tests base for tests that need an {@link Executor} for multithreaded + * test scenarios. + */ +public abstract class ExecutorTestBase { + + protected ExecutorService executor; + + @Before + public void setup() throws Exception { + executor = Executors.newSingleThreadExecutor(); + } + + @After + public void teardown() throws Exception { + executor.shutdown(); + } + + /** + * Asserts that the given future blocks. + * + * @param future + * future to test + * @throws Exception + */ + protected void assertBlocks(final Future<?> future) throws Exception { + try { + future.get(10, TimeUnit.MILLISECONDS); + fail("Operation should block"); + } catch (TimeoutException e) { + } + } + +} diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/LocalControllerTest.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/LocalControllerTest.java index b747dee8..c82f6921 100644 --- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/LocalControllerTest.java +++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/LocalControllerTest.java @@ -1,61 +1,61 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Brock Janiczak - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.io.IOException;
-
-import org.jacoco.agent.rt.StubRuntime;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.IRuntime;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-
-/**
- * Unit tests for {@link LocalController}.
- */
-public class LocalControllerTest {
-
- @Rule
- public TemporaryFolder folder = new TemporaryFolder();
-
- @Test
- public void testWriteData() throws Exception {
- File destFile = folder.newFile("jacoco.exec");
- AgentOptions options = new AgentOptions();
- options.setDestfile(destFile.getAbsolutePath());
-
- IRuntime runtime = new StubRuntime();
-
- LocalController controller = new LocalController();
- controller.startup(options, runtime);
- controller.writeExecutionData();
- controller.shutdown();
-
- assertTrue("Execution data file should be created", destFile.exists());
- }
-
- @Test(expected = IOException.class)
- public void testInvalidDestFile() throws Exception {
- AgentOptions options = new AgentOptions();
- options.setDestfile(folder.newFolder("folder").getAbsolutePath());
- IRuntime runtime = new StubRuntime();
- LocalController controller = new LocalController();
-
- // Startup should fail as the file can not be created:
- controller.startup(options, runtime);
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Brock Janiczak - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import org.jacoco.agent.rt.StubRuntime; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.IRuntime; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Unit tests for {@link LocalController}. + */ +public class LocalControllerTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testWriteData() throws Exception { + File destFile = folder.newFile("jacoco.exec"); + AgentOptions options = new AgentOptions(); + options.setDestfile(destFile.getAbsolutePath()); + + IRuntime runtime = new StubRuntime(); + + LocalController controller = new LocalController(); + controller.startup(options, runtime); + controller.writeExecutionData(); + controller.shutdown(); + + assertTrue("Execution data file should be created", destFile.exists()); + } + + @Test(expected = IOException.class) + public void testInvalidDestFile() throws Exception { + AgentOptions options = new AgentOptions(); + options.setDestfile(folder.newFolder("folder").getAbsolutePath()); + IRuntime runtime = new StubRuntime(); + LocalController controller = new LocalController(); + + // Startup should fail as the file can not be created: + controller.startup(options, runtime); + } + +} diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocket.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocket.java index 696ffe8b..05407cb3 100644 --- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocket.java +++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocket.java @@ -1,188 +1,188 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import java.io.IOException;
-import java.io.InterruptedIOException;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketAddress;
-import java.net.SocketException;
-import java.nio.channels.ServerSocketChannel;
-
-/**
- * Emulation of a {@link ServerSocket} for testing purposes without any physical
- * tcp/ip connections.
- */
-public class MockServerSocket extends ServerSocket {
-
- private final Object lock = new Object();
-
- private boolean closed;
-
- private Socket connection;
-
- private boolean inAccept;
-
- public MockServerSocket() throws IOException {
- super();
- closed = false;
- inAccept = false;
- }
-
- /**
- * Establishes a new mock connection. This method blocks until the other end
- * of the connection has been accepted.
- *
- * @return remote end of the mock connection
- */
- public Socket connect() throws Exception {
- synchronized (lock) {
- final MockSocketConnection c = new MockSocketConnection();
- connection = c.getSocketA();
- lock.notifyAll();
- while (connection != null) {
- lock.wait();
- }
- return c.getSocketB();
- }
- }
-
- /**
- * Blocks until another thread calls the {@link #accept()} method.
- */
- public void waitForAccept() throws Exception {
- synchronized (lock) {
- while (!inAccept) {
- lock.wait();
- }
- }
-
- }
-
- @Override
- public void close() throws IOException {
- synchronized (lock) {
- closed = true;
- lock.notifyAll();
- }
- }
-
- @Override
- public boolean isClosed() {
- return closed;
- }
-
- @Override
- public Socket accept() throws IOException {
- synchronized (lock) {
- inAccept = true;
- lock.notifyAll();
- try {
- while (connection == null) {
- if (closed) {
- throw new SocketException("socket closed");
- }
- lock.wait();
- }
- return connection;
- } catch (InterruptedException e) {
- throw new InterruptedIOException();
- } finally {
- connection = null;
- inAccept = false;
- lock.notifyAll();
- }
- }
- }
-
- // unsupported server socket methods:
-
- @Override
- public void bind(SocketAddress endpoint, int backlog) throws IOException {
- throw new AssertionError();
- }
-
- @Override
- public void bind(SocketAddress endpoint) throws IOException {
- throw new AssertionError();
- }
-
- @Override
- public ServerSocketChannel getChannel() {
- throw new AssertionError();
- }
-
- @Override
- public InetAddress getInetAddress() {
- throw new AssertionError();
- }
-
- @Override
- public int getLocalPort() {
- throw new AssertionError();
- }
-
- @Override
- public SocketAddress getLocalSocketAddress() {
- throw new AssertionError();
- }
-
- @Override
- public synchronized int getReceiveBufferSize() throws SocketException {
- throw new AssertionError();
- }
-
- @Override
- public boolean getReuseAddress() throws SocketException {
- throw new AssertionError();
- }
-
- @Override
- public synchronized int getSoTimeout() throws IOException {
- throw new AssertionError();
- }
-
- @Override
- public boolean isBound() {
- throw new AssertionError();
- }
-
- @Override
- public void setPerformancePreferences(int connectionTime, int latency,
- int bandwidth) {
- throw new AssertionError();
- }
-
- @Override
- public synchronized void setReceiveBufferSize(int size)
- throws SocketException {
- throw new AssertionError();
- }
-
- @Override
- public void setReuseAddress(boolean on) throws SocketException {
- throw new AssertionError();
- }
-
- @Override
- public synchronized void setSoTimeout(int timeout) throws SocketException {
- throw new AssertionError();
- }
-
- @Override
- public String toString() {
- throw new AssertionError();
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import java.io.IOException; +import java.io.InterruptedIOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.nio.channels.ServerSocketChannel; + +/** + * Emulation of a {@link ServerSocket} for testing purposes without any physical + * tcp/ip connections. + */ +public class MockServerSocket extends ServerSocket { + + private final Object lock = new Object(); + + private boolean closed; + + private Socket connection; + + private boolean inAccept; + + public MockServerSocket() throws IOException { + super(); + closed = false; + inAccept = false; + } + + /** + * Establishes a new mock connection. This method blocks until the other end + * of the connection has been accepted. + * + * @return remote end of the mock connection + */ + public Socket connect() throws Exception { + synchronized (lock) { + final MockSocketConnection c = new MockSocketConnection(); + connection = c.getSocketA(); + lock.notifyAll(); + while (connection != null) { + lock.wait(); + } + return c.getSocketB(); + } + } + + /** + * Blocks until another thread calls the {@link #accept()} method. + */ + public void waitForAccept() throws Exception { + synchronized (lock) { + while (!inAccept) { + lock.wait(); + } + } + + } + + @Override + public void close() throws IOException { + synchronized (lock) { + closed = true; + lock.notifyAll(); + } + } + + @Override + public boolean isClosed() { + return closed; + } + + @Override + public Socket accept() throws IOException { + synchronized (lock) { + inAccept = true; + lock.notifyAll(); + try { + while (connection == null) { + if (closed) { + throw new SocketException("socket closed"); + } + lock.wait(); + } + return connection; + } catch (InterruptedException e) { + throw new InterruptedIOException(); + } finally { + connection = null; + inAccept = false; + lock.notifyAll(); + } + } + } + + // unsupported server socket methods: + + @Override + public void bind(SocketAddress endpoint, int backlog) throws IOException { + throw new AssertionError(); + } + + @Override + public void bind(SocketAddress endpoint) throws IOException { + throw new AssertionError(); + } + + @Override + public ServerSocketChannel getChannel() { + throw new AssertionError(); + } + + @Override + public InetAddress getInetAddress() { + throw new AssertionError(); + } + + @Override + public int getLocalPort() { + throw new AssertionError(); + } + + @Override + public SocketAddress getLocalSocketAddress() { + throw new AssertionError(); + } + + @Override + public synchronized int getReceiveBufferSize() throws SocketException { + throw new AssertionError(); + } + + @Override + public boolean getReuseAddress() throws SocketException { + throw new AssertionError(); + } + + @Override + public synchronized int getSoTimeout() throws IOException { + throw new AssertionError(); + } + + @Override + public boolean isBound() { + throw new AssertionError(); + } + + @Override + public void setPerformancePreferences(int connectionTime, int latency, + int bandwidth) { + throw new AssertionError(); + } + + @Override + public synchronized void setReceiveBufferSize(int size) + throws SocketException { + throw new AssertionError(); + } + + @Override + public void setReuseAddress(boolean on) throws SocketException { + throw new AssertionError(); + } + + @Override + public synchronized void setSoTimeout(int timeout) throws SocketException { + throw new AssertionError(); + } + + @Override + public String toString() { + throw new AssertionError(); + } + +} diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocketTest.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocketTest.java index 8b2f9fae..07e8b4e1 100644 --- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocketTest.java +++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/MockServerSocketTest.java @@ -1,150 +1,150 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketException;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Unit tests for {@link MockServerSocket}.
- */
-public class MockServerSocketTest extends ExecutorTestBase {
-
- private ServerSocket serverSocket;
-
- /**
- * To verify that the tests reflect the behavior of real TCP sockets this
- * flag can be set to <code>true</code>.
- */
- private static final boolean REAL_SOCKETS = Boolean
- .getBoolean("MockServerSocketTest.realSockets");
-
- @Before
- @Override
- public void setup() throws Exception {
- super.setup();
- if (REAL_SOCKETS) {
- System.err.println("Using Real Sockets!");
- final InetAddress addr = InetAddress.getByName("localhost");
- serverSocket = new ServerSocket(16300, 1, addr);
- } else {
- serverSocket = new MockServerSocket();
- }
- }
-
- @After
- @Override
- public void teardown() throws Exception {
- serverSocket.close();
- super.teardown();
- }
-
- private Socket connect() throws Exception {
- if (REAL_SOCKETS) {
- final InetAddress addr = InetAddress.getByName("localhost");
- return new Socket(addr, 16300);
- } else {
- return ((MockServerSocket) serverSocket).connect();
- }
-
- }
-
- @Test
- public void testClose() throws Exception {
- assertFalse(serverSocket.isClosed());
- serverSocket.close();
- assertTrue(serverSocket.isClosed());
- }
-
- @Test(expected = SocketException.class)
- public void testCloseWhileAccept() throws Throwable {
- final Future<Socket> f = executor.submit(new Callable<Socket>() {
- public Socket call() throws Exception {
- return serverSocket.accept();
- }
- });
- assertBlocks(f);
- serverSocket.close();
- try {
- f.get();
- } catch (ExecutionException e) {
- throw e.getCause();
- }
- }
-
- @Test
- public void testAccept() throws Exception {
- final Future<Socket> f = executor.submit(new Callable<Socket>() {
- public Socket call() throws Exception {
- return serverSocket.accept();
- }
- });
- assertBlocks(f);
- connect().getOutputStream().write(123);
- final Socket socket = f.get();
- assertNotNull(socket);
- assertEquals(123, socket.getInputStream().read());
- }
-
- @Test(expected = SocketException.class)
- public void testAcceptOnClosedServerSocket() throws Exception {
- serverSocket.close();
- serverSocket.accept();
- }
-
- @Test
- public void testConnect() throws Exception {
- if (!REAL_SOCKETS) {
- final Future<Socket> f = executor.submit(new Callable<Socket>() {
- public Socket call() throws Exception {
- return ((MockServerSocket) serverSocket).connect();
- }
- });
- assertBlocks(f);
- serverSocket.accept().getOutputStream().write(42);
- final Socket socket = f.get();
- assertNotNull(socket);
- assertEquals(42, socket.getInputStream().read());
- }
- }
-
- @Test
- public void testWaitForAccept() throws Exception {
- if (!REAL_SOCKETS) {
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- ((MockServerSocket) serverSocket).waitForAccept();
- connect();
- return null;
- }
- });
- assertBlocks(f);
- serverSocket.accept();
- f.get();
- }
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests for {@link MockServerSocket}. + */ +public class MockServerSocketTest extends ExecutorTestBase { + + private ServerSocket serverSocket; + + /** + * To verify that the tests reflect the behavior of real TCP sockets this + * flag can be set to <code>true</code>. + */ + private static final boolean REAL_SOCKETS = Boolean + .getBoolean("MockServerSocketTest.realSockets"); + + @Before + @Override + public void setup() throws Exception { + super.setup(); + if (REAL_SOCKETS) { + System.err.println("Using Real Sockets!"); + final InetAddress addr = InetAddress.getByName("localhost"); + serverSocket = new ServerSocket(16300, 1, addr); + } else { + serverSocket = new MockServerSocket(); + } + } + + @After + @Override + public void teardown() throws Exception { + serverSocket.close(); + super.teardown(); + } + + private Socket connect() throws Exception { + if (REAL_SOCKETS) { + final InetAddress addr = InetAddress.getByName("localhost"); + return new Socket(addr, 16300); + } else { + return ((MockServerSocket) serverSocket).connect(); + } + + } + + @Test + public void testClose() throws Exception { + assertFalse(serverSocket.isClosed()); + serverSocket.close(); + assertTrue(serverSocket.isClosed()); + } + + @Test(expected = SocketException.class) + public void testCloseWhileAccept() throws Throwable { + final Future<Socket> f = executor.submit(new Callable<Socket>() { + public Socket call() throws Exception { + return serverSocket.accept(); + } + }); + assertBlocks(f); + serverSocket.close(); + try { + f.get(); + } catch (ExecutionException e) { + throw e.getCause(); + } + } + + @Test + public void testAccept() throws Exception { + final Future<Socket> f = executor.submit(new Callable<Socket>() { + public Socket call() throws Exception { + return serverSocket.accept(); + } + }); + assertBlocks(f); + connect().getOutputStream().write(123); + final Socket socket = f.get(); + assertNotNull(socket); + assertEquals(123, socket.getInputStream().read()); + } + + @Test(expected = SocketException.class) + public void testAcceptOnClosedServerSocket() throws Exception { + serverSocket.close(); + serverSocket.accept(); + } + + @Test + public void testConnect() throws Exception { + if (!REAL_SOCKETS) { + final Future<Socket> f = executor.submit(new Callable<Socket>() { + public Socket call() throws Exception { + return ((MockServerSocket) serverSocket).connect(); + } + }); + assertBlocks(f); + serverSocket.accept().getOutputStream().write(42); + final Socket socket = f.get(); + assertNotNull(socket); + assertEquals(42, socket.getInputStream().read()); + } + } + + @Test + public void testWaitForAccept() throws Exception { + if (!REAL_SOCKETS) { + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + ((MockServerSocket) serverSocket).waitForAccept(); + connect(); + return null; + } + }); + assertBlocks(f); + serverSocket.accept(); + f.get(); + } + } + +} diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpClientControllerTest.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpClientControllerTest.java index b34d4923..67ed2767 100644 --- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpClientControllerTest.java +++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpClientControllerTest.java @@ -1,113 +1,113 @@ -/*******************************************************************************
-/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Brock Janiczak - initial API and implementation
- * Marc R. Hoffmann - migration to mock socket
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-
-import java.io.IOException;
-import java.net.Socket;
-import java.util.List;
-
-import org.jacoco.agent.rt.ExceptionRecorder;
-import org.jacoco.agent.rt.StubRuntime;
-import org.jacoco.core.data.ExecutionDataStore;
-import org.jacoco.core.data.SessionInfo;
-import org.jacoco.core.data.SessionInfoStore;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.RemoteControlReader;
-import org.jacoco.core.runtime.RemoteControlWriter;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Unit tests for {@link TcpClientController}.
- */
-public class TcpClientControllerTest {
-
- private ExceptionRecorder logger;
-
- private IAgentController controller;
-
- private StubRuntime runtime;
-
- private Socket remoteSocket;
-
- private RemoteControlWriter remoteWriter;
-
- private RemoteControlReader remoteReader;
-
- @Before
- public void setup() throws Exception {
- logger = new ExceptionRecorder();
- final MockSocketConnection con = new MockSocketConnection();
- remoteSocket = con.getSocketB();
- remoteWriter = new RemoteControlWriter(remoteSocket.getOutputStream());
- controller = new TcpClientController(logger) {
- @Override
- protected Socket createSocket(AgentOptions options)
- throws IOException {
- return con.getSocketA();
- }
- };
- runtime = new StubRuntime();
- controller.startup(new AgentOptions(), runtime);
- remoteReader = new RemoteControlReader(remoteSocket.getInputStream());
- }
-
- @Test
- public void testShutdown() throws Exception {
- controller.shutdown();
- assertFalse(remoteReader.read());
- logger.assertEmpty();
- }
-
- @Test
- public void testRemoteClose() throws Exception {
- remoteSocket.close();
- controller.shutdown();
- logger.assertEmpty();
- }
-
- @Test
- public void testInvalidCommand() throws Exception {
- remoteWriter.visitSessionInfo(new SessionInfo("info", 1, 2));
- while (remoteReader.read()) {
- }
- controller.shutdown();
- logger.assertException(IOException.class, "No session info visitor.");
- }
-
- @Test
- public void testWriteExecutionData() throws Exception {
- controller.writeExecutionData();
-
- final ExecutionDataStore execStore = new ExecutionDataStore();
- remoteReader.setExecutionDataVisitor(execStore);
- final SessionInfoStore infoStore = new SessionInfoStore();
- remoteReader.setSessionInfoVisitor(infoStore);
-
- remoteReader.read();
-
- assertEquals("Foo", execStore.get(0x12345678).getName());
-
- final List<SessionInfo> infos = infoStore.getInfos();
- assertEquals(1, infos.size());
- assertEquals("stubid", infos.get(0).getId());
-
- logger.assertEmpty();
- controller.shutdown();
- }
-
-}
+/******************************************************************************* +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Brock Janiczak - initial API and implementation + * Marc R. Hoffmann - migration to mock socket + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.io.IOException; +import java.net.Socket; +import java.util.List; + +import org.jacoco.agent.rt.ExceptionRecorder; +import org.jacoco.agent.rt.StubRuntime; +import org.jacoco.core.data.ExecutionDataStore; +import org.jacoco.core.data.SessionInfo; +import org.jacoco.core.data.SessionInfoStore; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.RemoteControlReader; +import org.jacoco.core.runtime.RemoteControlWriter; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests for {@link TcpClientController}. + */ +public class TcpClientControllerTest { + + private ExceptionRecorder logger; + + private IAgentController controller; + + private StubRuntime runtime; + + private Socket remoteSocket; + + private RemoteControlWriter remoteWriter; + + private RemoteControlReader remoteReader; + + @Before + public void setup() throws Exception { + logger = new ExceptionRecorder(); + final MockSocketConnection con = new MockSocketConnection(); + remoteSocket = con.getSocketB(); + remoteWriter = new RemoteControlWriter(remoteSocket.getOutputStream()); + controller = new TcpClientController(logger) { + @Override + protected Socket createSocket(AgentOptions options) + throws IOException { + return con.getSocketA(); + } + }; + runtime = new StubRuntime(); + controller.startup(new AgentOptions(), runtime); + remoteReader = new RemoteControlReader(remoteSocket.getInputStream()); + } + + @Test + public void testShutdown() throws Exception { + controller.shutdown(); + assertFalse(remoteReader.read()); + logger.assertEmpty(); + } + + @Test + public void testRemoteClose() throws Exception { + remoteSocket.close(); + controller.shutdown(); + logger.assertEmpty(); + } + + @Test + public void testInvalidCommand() throws Exception { + remoteWriter.visitSessionInfo(new SessionInfo("info", 1, 2)); + while (remoteReader.read()) { + } + controller.shutdown(); + logger.assertException(IOException.class, "No session info visitor."); + } + + @Test + public void testWriteExecutionData() throws Exception { + controller.writeExecutionData(); + + final ExecutionDataStore execStore = new ExecutionDataStore(); + remoteReader.setExecutionDataVisitor(execStore); + final SessionInfoStore infoStore = new SessionInfoStore(); + remoteReader.setSessionInfoVisitor(infoStore); + + remoteReader.read(); + + assertEquals("Foo", execStore.get(0x12345678).getName()); + + final List<SessionInfo> infos = infoStore.getInfos(); + assertEquals(1, infos.size()); + assertEquals("stubid", infos.get(0).getId()); + + logger.assertEmpty(); + controller.shutdown(); + } + +} 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 49654668..94fd025d 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 @@ -1,265 +1,265 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Marc R. Hoffmann - initial API and implementation
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.List;
-import java.util.concurrent.Callable;
-import java.util.concurrent.Future;
-
-import org.jacoco.agent.rt.StubRuntime;
-import org.jacoco.core.data.ExecutionDataStore;
-import org.jacoco.core.data.ExecutionDataWriter;
-import org.jacoco.core.data.SessionInfo;
-import org.jacoco.core.data.SessionInfoStore;
-import org.jacoco.core.runtime.RemoteControlReader;
-import org.jacoco.core.runtime.RemoteControlWriter;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Unit tests for {@link TcpConnection}.
- */
-public class TcpConnectionTest extends ExecutorTestBase {
-
- private MockSocketConnection mockConnection;
-
- private StubRuntime runtime;
-
- @Before
- @Override
- public void setup() throws Exception {
- super.setup();
- mockConnection = new MockSocketConnection();
- runtime = new StubRuntime();
- }
-
- @Test(expected = IOException.class)
- public void testInvalidHeader() throws Exception {
- final OutputStream remoteOut = mockConnection.getSocketB()
- .getOutputStream();
- remoteOut.write(0x01);
- remoteOut.write(0xC0);
- remoteOut.write(0xCA);
- final TcpConnection connection = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- connection.init();
- connection.run();
- }
-
- @Test(expected = IOException.class)
- public void testInvalidContent() throws Exception {
- final OutputStream remoteOut = mockConnection.getSocketB()
- .getOutputStream();
- new ExecutionDataWriter(remoteOut);
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- con.init();
- remoteOut.write(123);
- con.run();
- }
-
- /**
- * Remote endpoint is closed after a valid header has been send.
- */
- @Test
- public void testRemoteClose() throws Exception {
- final OutputStream remoteOut = mockConnection.getSocketB()
- .getOutputStream();
- new ExecutionDataWriter(remoteOut);
-
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- con.init();
-
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- con.run();
- return null;
- }
- });
-
- assertBlocks(f);
-
- mockConnection.getSocketB().close();
- f.get();
- }
-
- /**
- * Remote endpoint is closed before even a valid header was send.
- */
- public void testRemoteCloseWithoutHeader() throws Throwable {
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
-
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- con.init();
- return null;
- }
- });
-
- assertBlocks(f);
-
- mockConnection.getSocketB().close();
- f.get();
- }
-
- /**
- * Local socket is closed while waiting for commands.
- *
- * @throws Exception
- */
- @Test
- public void testLocalClose() throws Exception {
- final OutputStream remoteOut = mockConnection.getSocketB()
- .getOutputStream();
- new ExecutionDataWriter(remoteOut);
-
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- con.init();
-
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- con.run();
- return null;
- }
- });
-
- assertBlocks(f);
-
- con.close();
- f.get();
- }
-
- @Test
- public void testRemoteDump() throws Exception {
- final RemoteControlWriter remoteWriter = new RemoteControlWriter(
- mockConnection.getSocketB().getOutputStream());
-
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- con.init();
-
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- con.run();
- return null;
- }
- });
-
- assertBlocks(f);
-
- remoteWriter.visitDumpCommand(true, false);
- readAndAssertData();
-
- con.close();
- f.get();
- }
-
- @Test
- public void testLocalDump() throws Exception {
- new RemoteControlWriter(mockConnection.getSocketB().getOutputStream());
-
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- con.init();
-
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- con.run();
- return null;
- }
- });
-
- assertBlocks(f);
-
- con.writeExecutionData();
- readAndAssertData();
-
- con.close();
- 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());
-
- final ExecutionDataStore execStore = new ExecutionDataStore();
- remoteReader.setExecutionDataVisitor(execStore);
- final SessionInfoStore infoStore = new SessionInfoStore();
- remoteReader.setSessionInfoVisitor(infoStore);
-
- assertTrue(remoteReader.read());
-
- final List<SessionInfo> infos = infoStore.getInfos();
- assertEquals(1, infos.size());
- assertEquals("stubid", infos.get(0).getId());
-
- assertEquals("Foo", execStore.get(0x12345678).getName());
- }
-
- @Test
- public void testRemoteReset() throws Exception {
- runtime.fillProbes();
-
- final RemoteControlWriter remoteWriter = new RemoteControlWriter(
- mockConnection.getSocketB().getOutputStream());
-
- final TcpConnection con = new TcpConnection(
- mockConnection.getSocketA(), runtime);
- con.init();
-
- final Future<Void> f = executor.submit(new Callable<Void>() {
- public Void call() throws Exception {
- con.run();
- return null;
- }
- });
-
- assertBlocks(f);
-
- remoteWriter.visitDumpCommand(false, true);
-
- final RemoteControlReader remoteReader = new RemoteControlReader(
- mockConnection.getSocketB().getInputStream());
-
- final ExecutionDataStore execStore = new ExecutionDataStore();
- remoteReader.setExecutionDataVisitor(execStore);
- final SessionInfoStore infoStore = new SessionInfoStore();
- remoteReader.setSessionInfoVisitor(infoStore);
-
- assertTrue(remoteReader.read());
- assertTrue(infoStore.getInfos().isEmpty());
- assertTrue(execStore.getContents().isEmpty());
- runtime.assertNoProbes();
-
- con.close();
- f.get();
- }
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; + +import org.jacoco.agent.rt.StubRuntime; +import org.jacoco.core.data.ExecutionDataStore; +import org.jacoco.core.data.ExecutionDataWriter; +import org.jacoco.core.data.SessionInfo; +import org.jacoco.core.data.SessionInfoStore; +import org.jacoco.core.runtime.RemoteControlReader; +import org.jacoco.core.runtime.RemoteControlWriter; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests for {@link TcpConnection}. + */ +public class TcpConnectionTest extends ExecutorTestBase { + + private MockSocketConnection mockConnection; + + private StubRuntime runtime; + + @Before + @Override + public void setup() throws Exception { + super.setup(); + mockConnection = new MockSocketConnection(); + runtime = new StubRuntime(); + } + + @Test(expected = IOException.class) + public void testInvalidHeader() throws Exception { + final OutputStream remoteOut = mockConnection.getSocketB() + .getOutputStream(); + remoteOut.write(0x01); + remoteOut.write(0xC0); + remoteOut.write(0xCA); + final TcpConnection connection = new TcpConnection( + mockConnection.getSocketA(), runtime); + connection.init(); + connection.run(); + } + + @Test(expected = IOException.class) + public void testInvalidContent() throws Exception { + final OutputStream remoteOut = mockConnection.getSocketB() + .getOutputStream(); + new ExecutionDataWriter(remoteOut); + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + con.init(); + remoteOut.write(123); + con.run(); + } + + /** + * Remote endpoint is closed after a valid header has been send. + */ + @Test + public void testRemoteClose() throws Exception { + final OutputStream remoteOut = mockConnection.getSocketB() + .getOutputStream(); + new ExecutionDataWriter(remoteOut); + + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + con.init(); + + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + con.run(); + return null; + } + }); + + assertBlocks(f); + + mockConnection.getSocketB().close(); + f.get(); + } + + /** + * Remote endpoint is closed before even a valid header was send. + */ + public void testRemoteCloseWithoutHeader() throws Throwable { + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + con.init(); + return null; + } + }); + + assertBlocks(f); + + mockConnection.getSocketB().close(); + f.get(); + } + + /** + * Local socket is closed while waiting for commands. + * + * @throws Exception + */ + @Test + public void testLocalClose() throws Exception { + final OutputStream remoteOut = mockConnection.getSocketB() + .getOutputStream(); + new ExecutionDataWriter(remoteOut); + + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + con.init(); + + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + con.run(); + return null; + } + }); + + assertBlocks(f); + + con.close(); + f.get(); + } + + @Test + public void testRemoteDump() throws Exception { + final RemoteControlWriter remoteWriter = new RemoteControlWriter( + mockConnection.getSocketB().getOutputStream()); + + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + con.init(); + + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + con.run(); + return null; + } + }); + + assertBlocks(f); + + remoteWriter.visitDumpCommand(true, false); + readAndAssertData(); + + con.close(); + f.get(); + } + + @Test + public void testLocalDump() throws Exception { + new RemoteControlWriter(mockConnection.getSocketB().getOutputStream()); + + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + con.init(); + + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + con.run(); + return null; + } + }); + + assertBlocks(f); + + con.writeExecutionData(); + readAndAssertData(); + + con.close(); + 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()); + + final ExecutionDataStore execStore = new ExecutionDataStore(); + remoteReader.setExecutionDataVisitor(execStore); + final SessionInfoStore infoStore = new SessionInfoStore(); + remoteReader.setSessionInfoVisitor(infoStore); + + assertTrue(remoteReader.read()); + + final List<SessionInfo> infos = infoStore.getInfos(); + assertEquals(1, infos.size()); + assertEquals("stubid", infos.get(0).getId()); + + assertEquals("Foo", execStore.get(0x12345678).getName()); + } + + @Test + public void testRemoteReset() throws Exception { + runtime.fillProbes(); + + final RemoteControlWriter remoteWriter = new RemoteControlWriter( + mockConnection.getSocketB().getOutputStream()); + + final TcpConnection con = new TcpConnection( + mockConnection.getSocketA(), runtime); + con.init(); + + final Future<Void> f = executor.submit(new Callable<Void>() { + public Void call() throws Exception { + con.run(); + return null; + } + }); + + assertBlocks(f); + + remoteWriter.visitDumpCommand(false, true); + + final RemoteControlReader remoteReader = new RemoteControlReader( + mockConnection.getSocketB().getInputStream()); + + final ExecutionDataStore execStore = new ExecutionDataStore(); + remoteReader.setExecutionDataVisitor(execStore); + final SessionInfoStore infoStore = new SessionInfoStore(); + remoteReader.setSessionInfoVisitor(infoStore); + + assertTrue(remoteReader.read()); + assertTrue(infoStore.getInfos().isEmpty()); + assertTrue(execStore.getContents().isEmpty()); + runtime.assertNoProbes(); + + con.close(); + f.get(); + } +} diff --git a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpServerControllerTest.java b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpServerControllerTest.java index 26c813c6..b965b238 100644 --- a/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpServerControllerTest.java +++ b/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/controller/TcpServerControllerTest.java @@ -1,142 +1,142 @@ -/*******************************************************************************
- * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Brock Janiczak - initial API and implementation
- * Marc R. Hoffmann - migration to mock socket
- *
- *******************************************************************************/
-package org.jacoco.agent.rt.controller;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.UnknownHostException;
-import java.util.List;
-
-import org.jacoco.agent.rt.ExceptionRecorder;
-import org.jacoco.agent.rt.StubRuntime;
-import org.jacoco.core.data.ExecutionDataStore;
-import org.jacoco.core.data.ExecutionDataWriter;
-import org.jacoco.core.data.SessionInfo;
-import org.jacoco.core.data.SessionInfoStore;
-import org.jacoco.core.runtime.AgentOptions;
-import org.jacoco.core.runtime.RemoteControlReader;
-import org.jacoco.core.runtime.RemoteControlWriter;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * Unit tests for {@link TcpServerController}.
- */
-public class TcpServerControllerTest {
-
- private ExceptionRecorder logger;
-
- private AgentOptions options;
-
- private StubRuntime runtime;
-
- private MockServerSocket serverSocket;
-
- private TcpServerController controller;
-
- @Before
- public void setup() throws Exception {
- options = new AgentOptions();
- runtime = new StubRuntime();
- logger = new ExceptionRecorder();
- serverSocket = new MockServerSocket();
- controller = new TcpServerController(logger) {
- @Override
- protected ServerSocket createServerSocket(AgentOptions options)
- throws IOException {
- return serverSocket;
- }
- };
- controller.startup(options, runtime);
- }
-
- @Test
- public void testShutdownWithoutConnection() throws Exception {
- serverSocket.waitForAccept();
- controller.shutdown();
- logger.assertEmpty();
- }
-
- @Test
- public void testShutdownWithConnection() throws Exception {
- serverSocket.waitForAccept();
- new ExecutionDataWriter(serverSocket.connect().getOutputStream());
- controller.shutdown();
- logger.assertEmpty();
- }
-
- @Test
- public void testWriteExecutionData() throws Exception {
- final Socket socket = serverSocket.connect();
- final RemoteControlWriter remoteWriter = new RemoteControlWriter(
- socket.getOutputStream());
- final RemoteControlReader remoteReader = new RemoteControlReader(
- socket.getInputStream());
-
- // First process a NOP command to ensure the connection is initialized:
- remoteWriter.visitDumpCommand(false, false);
- remoteReader.read();
-
- // Now the actual test starts:
- controller.writeExecutionData();
-
- final ExecutionDataStore execStore = new ExecutionDataStore();
- remoteReader.setExecutionDataVisitor(execStore);
- final SessionInfoStore infoStore = new SessionInfoStore();
- remoteReader.setSessionInfoVisitor(infoStore);
- remoteReader.read();
-
- assertEquals("Foo", execStore.get(0x12345678).getName());
-
- final List<SessionInfo> infos = infoStore.getInfos();
- assertEquals(1, infos.size());
- assertEquals("stubid", infos.get(0).getId());
-
- logger.assertEmpty();
- controller.shutdown();
- }
-
- @Test
- public void testInvalidHeader() throws Exception {
- final Socket socket = serverSocket.connect();
- final OutputStream out = socket.getOutputStream();
- out.write(0xca);
- out.write(0xfe);
- out.write(0xba);
- out.write(0xbe);
- serverSocket.waitForAccept();
- logger.assertException(IOException.class,
- "Invalid execution data file.");
- controller.shutdown();
- }
-
- @Test
- public void testGetInetAddressLoopback() throws UnknownHostException {
- final InetAddress addr = controller.getInetAddress(null);
- assertTrue(addr.isLoopbackAddress());
- }
-
- @Test
- public void testGetInetAddressAny() throws UnknownHostException {
- final InetAddress addr = controller.getInetAddress("*");
- assertNull(addr);
- }
-
-}
+/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Brock Janiczak - initial API and implementation + * Marc R. Hoffmann - migration to mock socket + * + *******************************************************************************/ +package org.jacoco.agent.rt.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.List; + +import org.jacoco.agent.rt.ExceptionRecorder; +import org.jacoco.agent.rt.StubRuntime; +import org.jacoco.core.data.ExecutionDataStore; +import org.jacoco.core.data.ExecutionDataWriter; +import org.jacoco.core.data.SessionInfo; +import org.jacoco.core.data.SessionInfoStore; +import org.jacoco.core.runtime.AgentOptions; +import org.jacoco.core.runtime.RemoteControlReader; +import org.jacoco.core.runtime.RemoteControlWriter; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests for {@link TcpServerController}. + */ +public class TcpServerControllerTest { + + private ExceptionRecorder logger; + + private AgentOptions options; + + private StubRuntime runtime; + + private MockServerSocket serverSocket; + + private TcpServerController controller; + + @Before + public void setup() throws Exception { + options = new AgentOptions(); + runtime = new StubRuntime(); + logger = new ExceptionRecorder(); + serverSocket = new MockServerSocket(); + controller = new TcpServerController(logger) { + @Override + protected ServerSocket createServerSocket(AgentOptions options) + throws IOException { + return serverSocket; + } + }; + controller.startup(options, runtime); + } + + @Test + public void testShutdownWithoutConnection() throws Exception { + serverSocket.waitForAccept(); + controller.shutdown(); + logger.assertEmpty(); + } + + @Test + public void testShutdownWithConnection() throws Exception { + serverSocket.waitForAccept(); + new ExecutionDataWriter(serverSocket.connect().getOutputStream()); + controller.shutdown(); + logger.assertEmpty(); + } + + @Test + public void testWriteExecutionData() throws Exception { + final Socket socket = serverSocket.connect(); + final RemoteControlWriter remoteWriter = new RemoteControlWriter( + socket.getOutputStream()); + final RemoteControlReader remoteReader = new RemoteControlReader( + socket.getInputStream()); + + // First process a NOP command to ensure the connection is initialized: + remoteWriter.visitDumpCommand(false, false); + remoteReader.read(); + + // Now the actual test starts: + controller.writeExecutionData(); + + final ExecutionDataStore execStore = new ExecutionDataStore(); + remoteReader.setExecutionDataVisitor(execStore); + final SessionInfoStore infoStore = new SessionInfoStore(); + remoteReader.setSessionInfoVisitor(infoStore); + remoteReader.read(); + + assertEquals("Foo", execStore.get(0x12345678).getName()); + + final List<SessionInfo> infos = infoStore.getInfos(); + assertEquals(1, infos.size()); + assertEquals("stubid", infos.get(0).getId()); + + logger.assertEmpty(); + controller.shutdown(); + } + + @Test + public void testInvalidHeader() throws Exception { + final Socket socket = serverSocket.connect(); + final OutputStream out = socket.getOutputStream(); + out.write(0xca); + out.write(0xfe); + out.write(0xba); + out.write(0xbe); + serverSocket.waitForAccept(); + logger.assertException(IOException.class, + "Invalid execution data file."); + controller.shutdown(); + } + + @Test + public void testGetInetAddressLoopback() throws UnknownHostException { + final InetAddress addr = controller.getInetAddress(null); + assertTrue(addr.isLoopbackAddress()); + } + + @Test + public void testGetInetAddressAny() throws UnknownHostException { + final InetAddress addr = controller.getInetAddress("*"); + assertNull(addr); + } + +} |