8205984: javax/net/ssl/compatibility/Compatibility.java failed to access port log file jdk-11+21
authorjjiang
Tue, 03 Jul 2018 09:27:41 +0800
changeset 50968 14708e1acdc3
parent 50967 1e24c7152e47
child 50969 3f879ff34084
child 50972 51e49f77f7eb
8205984: javax/net/ssl/compatibility/Compatibility.java failed to access port log file Summary: Release resource after reading port log file Reviewed-by: xuelei
test/jdk/javax/net/ssl/compatibility/Compatibility.java
test/jdk/javax/net/ssl/compatibility/Server.java
--- a/test/jdk/javax/net/ssl/compatibility/Compatibility.java	Mon Jul 02 17:54:36 2018 -0700
+++ b/test/jdk/javax/net/ssl/compatibility/Compatibility.java	Tue Jul 03 09:27:41 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -51,6 +51,7 @@
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import jdk.test.lib.process.OutputAnalyzer;
 
@@ -157,10 +158,6 @@
                                 "ServerStatus=%s, ClientStatus=%s, CaseStatus=%s%n",
                                 serverStatus, clientStatus, testCase.getStatus());
 
-                        // Confirm the server has stopped.
-                        if(new File(Utils.PORT_LOG).exists()) {
-                            throw new RuntimeException("Server doesn't stop.");
-                        }
                         System.out.println("----- Case end -----");
                     }
                 }
@@ -236,10 +233,12 @@
     private static List<String> jdkList(String listFileProp) throws IOException {
         String listFile = System.getProperty(listFileProp);
         System.out.println(listFileProp + "=" + listFile);
-        if (listFile != null && new File(listFile).exists()) {
-            return Files.lines(Paths.get(listFile))
-                        .filter(line -> { return !line.trim().isEmpty(); })
-                        .collect(Collectors.toList());
+        if (listFile != null && Files.exists(Paths.get(listFile))) {
+            try (Stream<String> lines = Files.lines(Paths.get(listFile))) {
+                return lines.filter(line -> {
+                    return !line.trim().isEmpty();
+                }).collect(Collectors.toList());
+            }
         } else {
             return new ArrayList<>();
         }
@@ -263,12 +262,13 @@
 
     // Retrieves the latest server port from port.log.
     private static int getServerPort() throws IOException {
-        if (!new File(Utils.PORT_LOG).exists()) {
+        if (!Files.exists(Paths.get(Utils.PORT_LOG))) {
             return -1;
         }
 
-        return Integer.valueOf(
-                Files.lines(Paths.get(Utils.PORT_LOG)).findFirst().get());
+        try (Stream<String> lines = Files.lines(Paths.get(Utils.PORT_LOG))) {
+            return Integer.valueOf(lines.findFirst().get());
+        }
     }
 
     private static OutputAnalyzer runServer(String jdkPath,
--- a/test/jdk/javax/net/ssl/compatibility/Server.java	Mon Jul 02 17:54:36 2018 -0700
+++ b/test/jdk/javax/net/ssl/compatibility/Server.java	Tue Jul 03 09:27:41 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -146,10 +146,9 @@
                 server.close();
             }
 
-            // Cleanups port.log.
-            File file = new File(Utils.PORT_LOG);
-            if (file.exists()) {
-                file.delete();
+            // Cleanups port log.
+            if (!new File(Utils.PORT_LOG).delete()) {
+                throw new RuntimeException("Cannot delete port log");
             }
         }