< prev index next >
test/jdk/com/sun/jdi/BasicJDWPConnectionTest.java
Print this page
@@ -27,21 +27,19 @@
* @library /test/lib
* @run driver BasicJDWPConnectionTest
*/
import java.io.IOException;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import jdk.test.lib.apps.LingeredApp;
-import jdk.test.lib.Utils;
import java.util.ArrayList;
-import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class BasicJDWPConnectionTest {
public static int handshake(int port) throws IOException {
@@ -62,44 +60,59 @@
}
}
return res;
}
- public static ArrayList<String> prepareCmd(int port, String allowOpt) {
- String address = "*:" + String.valueOf(port);
+ public static ArrayList<String> prepareCmd(String allowOpt) {
ArrayList<String> cmd = new ArrayList<>();
String jdwpArgs = "-agentlib:jdwp=transport=dt_socket,server=y," +
- "suspend=n,address=" + address + allowOpt;
+ "suspend=n,address=*:0" + allowOpt;
cmd.add(jdwpArgs);
return cmd;
}
+ private static Pattern listenRegexp = Pattern.compile("Listening for transport \\b(.+)\\b at address: \\b(\\d+)\\b");
+ private static int detectPort(String s) {
+ Matcher m = listenRegexp.matcher(s);
+ if (!m.find()) {
+ throw new RuntimeException("Could not detect port from '" + s + "'");
+ }
+ // m.group(1) is transport, m.group(2) is port
+ return Integer.parseInt(m.group(2));
+ }
+
public static void positiveTest(String testName, String allowOpt)
throws InterruptedException, IOException {
System.err.println("\nStarting " + testName);
- int port = Utils.getFreePort();
- ArrayList<String> cmd = prepareCmd(port, allowOpt);
+ ArrayList<String> cmd = prepareCmd(allowOpt);
LingeredApp a = LingeredApp.startApp(cmd);
- int res = handshake(port);
+ int res;
+ try {
+ res = handshake(detectPort(a.getProcessStdout()));
+ } finally {
a.stopApp();
+ }
if (res < 0) {
throw new RuntimeException(testName + " FAILED");
}
System.err.println(testName + " PASSED");
}
public static void negativeTest(String testName, String allowOpt)
throws InterruptedException, IOException {
System.err.println("\nStarting " + testName);
- int port = Utils.getFreePort();
- ArrayList<String> cmd = prepareCmd(port, allowOpt);
+ ArrayList<String> cmd = prepareCmd(allowOpt);
LingeredApp a = LingeredApp.startApp(cmd);
- int res = handshake(port);
+ int res;
+ try {
+ res = handshake(detectPort(a.getProcessStdout()));
+ } finally {
a.stopApp();
+ }
if (res > 0) {
System.err.println(testName + ": res=" + res);
throw new RuntimeException(testName + " FAILED");
}
System.err.println(testName + ": returned a negative code as expected: " + res);
@@ -107,15 +120,23 @@
}
public static void badAllowOptionTest(String testName, String allowOpt)
throws InterruptedException, IOException {
System.err.println("\nStarting " + testName);
- int port = Utils.getFreePort();
- ArrayList<String> cmd = prepareCmd(port, allowOpt);
+ ArrayList<String> cmd = prepareCmd(allowOpt);
try {
LingeredApp a = LingeredApp.startApp(cmd);
+
+ // startApp is expected to fail, but if not, terminate the app
+ try {
+ a.stopApp();
+ } catch (IOException e) {
+ // print and let the test fail
+ System.err.println("LingeredApp.stopApp failed");
+ e.printStackTrace();
+ }
} catch (IOException ex) {
System.err.println(testName + ": caught expected IOException");
System.err.println(testName + " PASSED");
return;
}
@@ -172,28 +193,18 @@
// Bad mix of allow address value with '*'
String allowOpt = ",allow=allow=127.0.0.1+*";
badAllowOptionTest("ExplicitMultiDefault2Test", allowOpt);
}
- public static void main(String[] args) {
- try {
+ public static void main(String[] args) throws Exception {
DefaultTest();
ExplicitDefaultTest();
AllowTest();
MultiAllowTest();
DenyTest();
MultiDenyTest();
EmptyAllowOptionTest();
ExplicitMultiDefault1Test();
ExplicitMultiDefault2Test();
System.err.println("\nTest PASSED");
- } catch (InterruptedException ex) {
- System.err.println("\nTest ERROR, getFreePort");
- ex.printStackTrace();
- System.exit(3);
- } catch (IOException ex) {
- System.err.println("\nTest ERROR");
- ex.printStackTrace();
- System.exit(3);
- }
}
}
< prev index next >