aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.agent.rt.test/src/org/jacoco/agent/rt/internal/output/MockServerSocketTest.java
blob: 228b45cb827648632fb114818dfe14028ea515e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2009, 2015 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.internal.output;

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();
		}
	}

}