Improve CliFixture messages and Win behavior

Original commit: elastic/x-pack-elasticsearch@332d1744a2
This commit is contained in:
Costin Leau 2017-09-27 18:50:07 +03:00
parent fdd98f01ed
commit de85a2cd2b

View File

@ -22,6 +22,7 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import static java.util.Collections.singleton;
@ -42,12 +43,18 @@ public class CliFixture {
if (false == Files.isRegularFile(cliJar)) {
throw new IllegalArgumentException(cliJar + " is not a regular file");
}
Path javaExecutable = Paths.get(System.getProperty("java.home"), "bin", "java");
String javaExec = "java";
boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win");
if (isWindows) {
javaExec += ".exe";
}
Path javaExecutable = Paths.get(System.getProperty("java.home"), "bin", javaExec);
if (false == Files.exists(javaExecutable)) {
throw new IllegalArgumentException(cliJar + " doesn't exist");
throw new IllegalArgumentException(javaExec + " doesn't exist");
}
if (false == Files.isExecutable(javaExecutable)) {
throw new IllegalArgumentException(cliJar + " isn't executable");
throw new IllegalArgumentException(javaExec + " isn't executable");
}
try (ServerSocket server = new ServerSocket()) {
@ -63,13 +70,15 @@ public class CliFixture {
InetSocketAddress bound = (InetSocketAddress) server.getLocalSocketAddress();
if (bound.getAddress() instanceof Inet6Address) {
Files.write(tmp, singleton("[" + bound.getHostString() + "]:" + bound.getPort()));
} else {
}
else {
Files.write(tmp, singleton(bound.getHostString() + ":" + bound.getPort()));
}
Files.move(tmp, dir.resolve("ports"), StandardCopyOption.ATOMIC_MOVE);
boolean run = true;
// Run forever until killed
while (true) {
while (run) {
try {
println("accepting on localhost:" + server.getLocalPort());
Socket s = server.accept();