import java.io.IOException;
import java.net.ServerSocket;
import java.util.List;
-import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
*
* @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
*/
-public class FakeTcpServer {
+public class FakeTcpServer implements AutoCloseable {
private final ServerSocket serverSocket;
private final ExecutorService executorService;
}
public Future<?> connect() throws IOException {
- return executorService.submit(new Callable<Void>() {
- @Override
- public Void call() throws Exception {
- clientSocket.set(new TextSocket(serverSocket.accept()));
- return null;
- }
+ return executorService.submit(() -> {
+ clientSocket.set(new TextSocket(serverSocket.accept()));
+ return null;
});
}
return clientSocket.get().collectUntil(lineMatcher);
}
- public void writeLine(String line) throws IOException {
- clientSocket.get().writeLine(line);
+ public void writeLine(String... lines) throws IOException {
+ for (String line : lines) {
+ clientSocket.get().writeLine(line);
+ }
}
public String readLine() throws IOException {
return clientSocket.get().readLine();
}
+ @Override
+ public void close() throws IOException {
+ TextSocket textSocket = clientSocket.get();
+ if (textSocket != null) {
+ textSocket.close();
+ }
+ }
+
}