Skip to content

Commit a65bad7

Browse files
authored
Merge pull request eugenp#762 from khatwaniNikhil/BAEL-418-Changes
Fixed Failing EchoTest
2 parents 1d3ac08 + 4d54080 commit a65bad7

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎core-java/src/main/java/com/baeldung/java/nio/selector/EchoServer.java‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.baeldung.java.nio.selector;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.InetSocketAddress;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.SelectionKey;
8+
import java.nio.channels.Selector;
39
import java.nio.channels.ServerSocketChannel;
410
import java.nio.channels.SocketChannel;
5-
import java.nio.channels.Selector;
6-
import java.nio.channels.SelectionKey;
7-
import java.nio.ByteBuffer;
8-
import java.io.IOException;
9-
import java.util.Set;
1011
import java.util.Iterator;
11-
import java.net.InetSocketAddress;
12+
import java.util.Set;
1213

1314
public class EchoServer {
1415

@@ -47,4 +48,15 @@ public static void main(String[] args)
4748
}
4849
}
4950
}
50-
}
51+
52+
public static Process start() throws IOException, InterruptedException {
53+
String javaHome = System.getProperty("java.home");
54+
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
55+
String classpath = System.getProperty("java.class.path");
56+
String className = EchoServer.class.getCanonicalName();
57+
58+
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
59+
60+
return builder.start();
61+
}
62+
}

‎core-java/src/test/java/com/baeldung/java/nio/selector/EchoTest.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import java.io.IOException;
6+
57
import org.junit.Test;
68

79
public class EchoTest {
810

911
@Test
10-
public void givenClient_whenServerEchosMessage_thenCorrect() {
12+
public void givenClient_whenServerEchosMessage_thenCorrect() throws IOException, InterruptedException {
13+
Process process = EchoServer.start();
1114
EchoClient client = EchoClient.start();
1215
String resp1 = client.sendMessage("hello");
1316
String resp2 = client.sendMessage("world");
1417
assertEquals("hello", resp1);
1518
assertEquals("world", resp2);
19+
process.destroy();
1620
}
17-
1821
}

0 commit comments

Comments
 (0)