8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
authorbpb
Tue, 05 Jan 2016 16:03:36 -0800
changeset 34881 c519b013f50e
parent 34880 00d8bef75229
child 34882 ce2a8ec851c1
8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available Summary: Check for presence of netcat command using 'which' before running test Reviewed-by: alanb
jdk/test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java
--- a/jdk/test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java	Tue Jan 05 13:57:32 2016 -0800
+++ b/jdk/test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java	Tue Jan 05 16:03:36 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016 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
@@ -28,6 +28,7 @@
  */
 
 import java.io.File;
+import java.io.InputStream;
 import java.io.IOException;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
@@ -49,6 +50,17 @@
     public static void main(String[] args)
         throws InterruptedException, IOException {
 
+        // Use 'which' to verify that 'nc' is available and skip the test
+        // if it is not.
+        Process proc = Runtime.getRuntime().exec("which nc");
+        InputStream stdout = proc.getInputStream();
+        int b = stdout.read();
+        proc.destroy();
+        if (b == -1) {
+            System.err.println("Netcat command unavailable; skipping test.");
+            return;
+        }
+
         // Create a new sub-directory of the nominal test directory in which
         // 'nc' will create the socket file.
         String testSubDir = System.getProperty("test.dir", ".")
@@ -62,7 +74,6 @@
 
         // Create a process which executes the nc (netcat) utility to create
         // a socket file at the indicated location.
-        Process proc;
         FileSystem fs = FileSystems.getDefault();
         try (WatchService ws = fs.newWatchService()) {
             // Watch the test sub-directory to receive notification when an