changeset 1531:643f3bb4c378

8171415: Remove Java 7 features from testlibrary Reviewed-by: omajid
author andrew
date Tue, 20 Dec 2016 00:45:12 +0000
parents be3f4ae45028
children ab15fb826138
files test/lib/testlibrary/AssertsTest.java test/lib/testlibrary/jdk/testlibrary/FileUtils.java test/lib/testlibrary/jdk/testlibrary/JDKToolFinder.java test/lib/testlibrary/jdk/testlibrary/OSInfo.java test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java test/lib/testlibrary/jdk/testlibrary/ProcessTools.java test/lib/testlibrary/jdk/testlibrary/StreamPumper.java
diffstat 7 files changed, 117 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/test/lib/testlibrary/AssertsTest.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/AssertsTest.java	Tue Dec 20 00:45:12 2016 +0000
@@ -38,6 +38,8 @@
         }
     }
 
+    private static final Foo NULL = null;
+
     public static void main(String[] args) throws Exception {
         testLessThan();
         testLessThanOrEqual();
@@ -71,7 +73,7 @@
 
     private static void testEquals() throws Exception {
         expectPass(Assertion.EQ, 1, 1);
-        expectPass(Assertion.EQ, null, null);
+        expectPass(Assertion.EQ, NULL, NULL);
 
         Foo f1 = new Foo(1);
         expectPass(Assertion.EQ, f1, f1);
@@ -108,13 +110,13 @@
         Foo f2 = new Foo(1);
         expectPass(Assertion.NE, f1, f2);
 
-        expectFail(Assertion.NE, null, null);
+        expectFail(Assertion.NE, NULL, NULL);
         expectFail(Assertion.NE, f1, f1);
         expectFail(Assertion.NE, 1, 1);
     }
 
     private static void testNull() throws Exception {
-        expectPass(Assertion.NULL, null);
+        expectPass(Assertion.NULL, NULL);
 
         expectFail(Assertion.NULL, 1);
     }
@@ -122,19 +124,19 @@
     private static void testNotNull() throws Exception {
         expectPass(Assertion.NOTNULL, 1);
 
-        expectFail(Assertion.NOTNULL, null);
+        expectFail(Assertion.NOTNULL, NULL);
     }
 
     private static void testTrue() throws Exception {
-        expectPass(Assertion.TRUE, true);
+        expectPassBool(Assertion.TRUE, true);
 
-        expectFail(Assertion.TRUE, false);
+        expectFailBool(Assertion.TRUE, false);
     }
 
     private static void testFalse() throws Exception {
-        expectPass(Assertion.FALSE, false);
+        expectPassBool(Assertion.FALSE, false);
 
-        expectFail(Assertion.FALSE, true);
+        expectFailBool(Assertion.FALSE, true);
     }
 
     private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args)
@@ -142,6 +144,11 @@
         Assertion.run(assertion, args);
     }
 
+    private static void expectPassBool(Assertion assertion, boolean bool)
+        throws Exception {
+        Assertion.runBool(assertion, bool);
+    }
+
     private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args)
         throws Exception {
         try {
@@ -153,13 +160,23 @@
                             " to throw a RuntimeException");
     }
 
+    private static void expectFailBool(Assertion assertion, boolean bool)
+        throws Exception {
+        try {
+            Assertion.runBool(assertion, bool);
+        } catch (RuntimeException e) {
+            return;
+        }
+        throw new Exception("Expected " + Assertion.format(assertion, bool +
+                                                           " to throw a RuntimeException"));
+    }
 }
 
 enum Assertion {
     LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE;
 
     public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) {
-        String msg = "Expected " + format(assertion, args) + " to pass";
+        String msg = "Expected " + format(assertion, (Object[]) args) + " to pass";
         switch (assertion) {
             case LT:
                 assertLessThan(args[0], args[1], msg);
@@ -185,17 +202,27 @@
             case NOTNULL:
                 assertNotNull(args == null ? args : args[0], msg);
                 break;
-            case FALSE:
-                assertFalse((Boolean) args[0], msg);
-                break;
-            case TRUE:
-                assertTrue((Boolean) args[0], msg);
-                break;
-            default:
+           default:
                 // do nothing
         }
     }
 
+    public static void runBool(Assertion assertion, Boolean bool) {
+        String msg = "Expected " + format(assertion, bool) + " to pass";
+        switch (assertion) {
+            case FALSE:
+                System.err.println("assertFalse(" + bool + ", " + msg + ")");
+                assertFalse(bool, msg);
+                break;
+            case TRUE:
+                System.err.println("assertTrue(" + bool + ", " + msg + ")");
+                assertTrue(bool, msg);
+                break;
+           default:
+                // do nothing
+        }
+     }
+
     public static String format(Assertion assertion, Object ... args) {
         switch (assertion) {
             case LT:
--- a/test/lib/testlibrary/jdk/testlibrary/FileUtils.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/FileUtils.java	Tue Dec 20 00:45:12 2016 +0000
@@ -23,14 +23,8 @@
 
 package jdk.testlibrary;
 
+import java.io.File;
 import java.io.IOException;
-import java.nio.file.DirectoryNotEmptyException;
-import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
-import java.nio.file.NoSuchFileException;
-import java.nio.file.Path;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -58,7 +52,7 @@
      * @throws IOException
      *         if an I/O error occurs
      */
-    public static void deleteFileWithRetry(Path path)
+    public static void deleteFileWithRetry(File path)
         throws IOException
     {
         try {
@@ -82,46 +76,28 @@
      * @throws IOException
      *         if an I/O error occurs
      */
-    public static void deleteFileIfExistsWithRetry(Path path)
+    public static void deleteFileIfExistsWithRetry(File path)
         throws IOException
     {
         try {
-            if(Files.exists(path))
+            if (path.exists())
                 deleteFileWithRetry0(path);
         } catch (InterruptedException x) {
             throw new IOException("Interrupted while deleting.", x);
         }
     }
 
-    private static void deleteFileWithRetry0(Path path)
+    private static void deleteFileWithRetry0(File path)
         throws IOException, InterruptedException
     {
         int times = 0;
-        IOException ioe = null;
-        while (true) {
-            try {
-                Files.delete(path);
-                while (Files.exists(path)) {
-                    times++;
-                    if (times > MAX_RETRY_DELETE_TIMES)
-                        throw new IOException("File still exists after " + times + " waits.");
-                    Thread.sleep(RETRY_DELETE_MILLIS);
-                }
-                break;
-            } catch (NoSuchFileException | DirectoryNotEmptyException x) {
-                throw x;
-            } catch (IOException x) {
-                // Backoff/retry in case another process is accessing the file
-                times++;
-                if (ioe == null)
-                    ioe = x;
-                else
-                    ioe.addSuppressed(x);
-
-                if (times > MAX_RETRY_DELETE_TIMES)
-                    throw ioe;
-                Thread.sleep(RETRY_DELETE_MILLIS);
-            }
+        boolean result = path.delete();
+        while (!result) {
+            times++;
+            if (times > MAX_RETRY_DELETE_TIMES)
+                throw new IOException("File still exists after " + times + " waits.");
+            Thread.sleep(RETRY_DELETE_MILLIS);
+            result = path.delete();
         }
     }
 
@@ -137,58 +113,25 @@
      *          following exceptions are added as suppressed exceptions of the
      *          first one caught, which is then re-thrown.
      */
-    public static void deleteFileTreeWithRetry(Path dir)
+    public static void deleteFileTreeWithRetry(File dir)
          throws IOException
     {
-        IOException ioe = null;
-        final List<IOException> excs = deleteFileTreeUnchecked(dir);
-        if (!excs.isEmpty()) {
-            ioe = excs.remove(0);
-            for (IOException x : excs)
-                ioe.addSuppressed(x);
-        }
-        if (ioe != null)
-            throw ioe;
+        boolean failed = false;
+        final List<Boolean> results = deleteFileTreeUnchecked(dir);
+        failed = !results.isEmpty();
+        if (failed)
+            throw new IOException();
     }
 
-    public static List<IOException> deleteFileTreeUnchecked(Path dir) {
-        final List<IOException> excs = new ArrayList<>();
-        try {
-            java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
-                @Override
-                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
-                    try {
-                        deleteFileWithRetry0(file);
-                    } catch (IOException x) {
-                        excs.add(x);
-                    } catch (InterruptedException x) {
-                        excs.add(new IOException("Interrupted while deleting.", x));
-                        return FileVisitResult.TERMINATE;
-                    }
-                    return FileVisitResult.CONTINUE;
-                }
-                @Override
-                public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
-                    try {
-                        deleteFileWithRetry0(dir);
-                    } catch (IOException x) {
-                        excs.add(x);
-                    } catch (InterruptedException x) {
-                        excs.add(new IOException("Interrupted while deleting.", x));
-                        return FileVisitResult.TERMINATE;
-                    }
-                    return FileVisitResult.CONTINUE;
-                }
-                @Override
-                public FileVisitResult visitFileFailed(Path file, IOException exc) {
-                    excs.add(exc);
-                    return FileVisitResult.CONTINUE;
-                }
-            });
-        } catch (IOException x) {
-            excs.add(x);
+    public static List<Boolean> deleteFileTreeUnchecked(File dir) {
+        final List<Boolean> results = new ArrayList<Boolean>();
+        for (File file : dir.listFiles()) {
+            if (file.isDirectory()) {
+                results.addAll(deleteFileTreeUnchecked(file));
+            } else {
+                results.add(file.delete());
+            }
         }
-        return excs;
+        return results;
     }
 }
-
--- a/test/lib/testlibrary/jdk/testlibrary/JDKToolFinder.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/JDKToolFinder.java	Tue Dec 20 00:45:12 2016 +0000
@@ -23,9 +23,8 @@
 
 package jdk.testlibrary;
 
+import java.io.File;
 import java.io.FileNotFoundException;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 
 public final class JDKToolFinder {
 
@@ -94,13 +93,13 @@
                     + "When running test separately, set this property using '-D" + property + "=/path/to/jdk'.");
         }
 
-        Path toolName = Paths.get("bin", tool + (Platform.isWindows() ? ".exe" : ""));
+        File toolName = new File("bin", tool + (Platform.isWindows() ? ".exe" : ""));
 
-        Path jdkTool = Paths.get(jdkPath, toolName.toString());
-        if (!jdkTool.toFile().exists()) {
-            throw new FileNotFoundException("Could not find file " + jdkTool.toAbsolutePath());
+        File jdkTool = new File(jdkPath, toolName.toString());
+        if (!jdkTool.exists()) {
+            throw new FileNotFoundException("Could not find file " + jdkTool.getAbsolutePath());
         }
 
-        return jdkTool.toAbsolutePath().toString();
+        return jdkTool.getAbsolutePath();
     }
 }
--- a/test/lib/testlibrary/jdk/testlibrary/OSInfo.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/OSInfo.java	Tue Dec 20 00:45:12 2016 +0000
@@ -188,4 +188,3 @@
         }
     }
 }
-
--- a/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Tue Dec 20 00:45:12 2016 +0000
@@ -408,7 +408,7 @@
     }
 
     private List<String> asLines(String buffer) {
-        List<String> l = new ArrayList<>();
+        List<String> l = new ArrayList<String>();
         String[] a = buffer.split(Utils.NEW_LINE);
         for (String string : a) {
             l.add(string);
--- a/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Tue Dec 20 00:45:12 2016 +0000
@@ -35,9 +35,10 @@
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CyclicBarrier;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import java.util.concurrent.Phaser;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
@@ -67,14 +68,17 @@
      * @param processBuilder The process builder
      * @return Returns the initialized process
      * @throws IOException
+     * @throws BrokenBarrierException
      */
     public static Process startProcess(String name,
                                        ProcessBuilder processBuilder)
-    throws IOException {
+    throws IOException, BrokenBarrierException {
         Process p = null;
         try {
             p = startProcess(name, processBuilder, -1, TimeUnit.NANOSECONDS);
-        } catch (InterruptedException | TimeoutException e) {
+        } catch (InterruptedException e) {
+            // can't ever happen
+        } catch (TimeoutException e) {
             // can't ever happen
         }
         return p;
@@ -91,27 +95,44 @@
      * @throws IOException
      * @throws InterruptedException
      * @throws TimeoutException
+     * @throws BrokenBarrierException
      */
     public static Process startProcess(String name,
                                        ProcessBuilder processBuilder,
                                        long timeout,
                                        TimeUnit unit)
-    throws IOException, InterruptedException, TimeoutException {
+    throws IOException, InterruptedException, TimeoutException, BrokenBarrierException {
         Process p = processBuilder.start();
         StreamPumper stdout = new StreamPumper(p.getInputStream());
         StreamPumper stderr = new StreamPumper(p.getErrorStream());
 
         stdout.addPump(new LineForwarder(name, System.out));
         stderr.addPump(new LineForwarder(name, System.err));
-        final Phaser phs = new Phaser(1);
+        final CyclicBarrier cb = new CyclicBarrier(1);
         Future<Void> stdoutTask = stdout.process();
         Future<Void> stderrTask = stderr.process();
 
         try {
             if (timeout > -1) {
-                phs.awaitAdvanceInterruptibly(0, timeout, unit);
+                cb.await(timeout, unit);
+            }
+        } catch (TimeoutException e) {
+            System.err.println("Failed to start a process (thread dump follows)");
+            for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
+                printStack(s.getKey(), s.getValue());
             }
-        } catch (TimeoutException | InterruptedException e) {
+            stdoutTask.cancel(true);
+            stderrTask.cancel(true);
+            throw e;
+        } catch (InterruptedException e) {
+            System.err.println("Failed to start a process (thread dump follows)");
+            for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
+                printStack(s.getKey(), s.getValue());
+            }
+            stdoutTask.cancel(true);
+            stderrTask.cancel(true);
+            throw e;
+        } catch (BrokenBarrierException e) {
             System.err.println("Failed to start a process (thread dump follows)");
             for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
                 printStack(s.getKey(), s.getValue());
@@ -221,7 +242,7 @@
             throws Exception {
         String javapath = JDKToolFinder.getJDKTool("java");
 
-        ArrayList<String> args = new ArrayList<>();
+        ArrayList<String> args = new ArrayList<String>();
         args.add(javapath);
         Collections.addAll(args, getPlatformSpecificVMArgs());
         Collections.addAll(args, command);
--- a/test/lib/testlibrary/jdk/testlibrary/StreamPumper.java	Fri Oct 14 17:18:07 2016 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/StreamPumper.java	Tue Dec 20 00:45:12 2016 +0000
@@ -73,11 +73,11 @@
     }
 
     private final InputStream in;
-    private final Set<OutputStream> outStreams = new HashSet<>();
-    private final Set<LinePump> linePumps = new HashSet<>();
+    private final Set<OutputStream> outStreams = new HashSet<OutputStream>();
+    private final Set<LinePump> linePumps = new HashSet<LinePump>();
 
     private final AtomicBoolean processing = new AtomicBoolean(false);
-    private final FutureTask<Void> processingTask = new FutureTask(this, null);
+    private final FutureTask<Void> processingTask = new FutureTask<Void>(this, null);
 
     public StreamPumper(InputStream in) {
         this.in = in;
@@ -103,7 +103,9 @@
      */
     @Override
     public void run() {
-        try (BufferedInputStream is = new BufferedInputStream(in)) {
+        BufferedInputStream is = null;
+        try {
+            is = new BufferedInputStream(in);
             ByteArrayOutputStream lineBos = new ByteArrayOutputStream();
             byte[] buf = new byte[BUF_SIZE];
             int len = 0;
@@ -156,6 +158,11 @@
                     out.flush();
                 } catch (IOException e) {}
             }
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {}
+            }
             try {
                 in.close();
             } catch (IOException e) {}