< prev index next >

test/jdk/java/lang/ProcessHandle/InfoTest.java

Print this page




 281 
 282     /**
 283      * Spawn a child with arguments and check they are visible via the ProcessHandle.
 284      */
 285     @Test
 286     public static void test3() {
 287         try {
 288             for (long sleepTime : Arrays.asList(Utils.adjustTimeout(30), Utils.adjustTimeout(32))) {
 289                 Process p = spawn("sleep", String.valueOf(sleepTime));
 290 
 291                 ProcessHandle.Info info = p.info();
 292                 System.out.printf(" info: %s%n", info);
 293 
 294                 if (info.user().isPresent()) {
 295                     String user = info.user().get();
 296                     Assert.assertNotNull(user);
 297                     Assert.assertEquals(user, whoami);
 298                 }
 299                 if (info.command().isPresent()) {
 300                     String command = info.command().get();
 301                     String expected = Platform.isWindows() ? "sleep.exe" : "sleep";







 302                     Assert.assertTrue(command.endsWith(expected), "Command: expected: \'" +
 303                             expected + "\', actual: " + command);
 304 
 305                     // Verify the command exists and is executable
 306                     File exe = new File(command);
 307                     Assert.assertTrue(exe.exists(), "command must exist: " + exe);
 308                     Assert.assertTrue(exe.canExecute(), "command must be executable: " + exe);
 309                 }
 310                 if (info.arguments().isPresent()) {
 311                     String[] args = info.arguments().get();
 312                     if (args.length > 0) {
 313                         Assert.assertEquals(args[0], String.valueOf(sleepTime));
 314                     }
 315                 }
 316                 p.destroy();
 317                 Assert.assertTrue(p.waitFor(Utils.adjustTimeout(30), TimeUnit.SECONDS),
 318                         "timeout waiting for process to terminate");
 319             }
 320         } catch (IOException | InterruptedException ex) {
 321             ex.printStackTrace(System.out);




 281 
 282     /**
 283      * Spawn a child with arguments and check they are visible via the ProcessHandle.
 284      */
 285     @Test
 286     public static void test3() {
 287         try {
 288             for (long sleepTime : Arrays.asList(Utils.adjustTimeout(30), Utils.adjustTimeout(32))) {
 289                 Process p = spawn("sleep", String.valueOf(sleepTime));
 290 
 291                 ProcessHandle.Info info = p.info();
 292                 System.out.printf(" info: %s%n", info);
 293 
 294                 if (info.user().isPresent()) {
 295                     String user = info.user().get();
 296                     Assert.assertNotNull(user);
 297                     Assert.assertEquals(user, whoami);
 298                 }
 299                 if (info.command().isPresent()) {
 300                     String command = info.command().get();
 301                     String expected = "sleep";
 302                     if (Platform.isWindows()) {
 303                         expected = "sleep.exe";
 304                     } else if (new File("/bin/busybox").exists()) {
 305                         // With busybox sleep is just a sym link to busybox.
 306                         // The busbox executable is seen as ProcessHandle.Info command.
 307                         expected = "busybox";
 308                     }
 309                     Assert.assertTrue(command.endsWith(expected), "Command: expected: \'" +
 310                             expected + "\', actual: " + command);
 311 
 312                     // Verify the command exists and is executable
 313                     File exe = new File(command);
 314                     Assert.assertTrue(exe.exists(), "command must exist: " + exe);
 315                     Assert.assertTrue(exe.canExecute(), "command must be executable: " + exe);
 316                 }
 317                 if (info.arguments().isPresent()) {
 318                     String[] args = info.arguments().get();
 319                     if (args.length > 0) {
 320                         Assert.assertEquals(args[0], String.valueOf(sleepTime));
 321                     }
 322                 }
 323                 p.destroy();
 324                 Assert.assertTrue(p.waitFor(Utils.adjustTimeout(30), TimeUnit.SECONDS),
 325                         "timeout waiting for process to terminate");
 326             }
 327         } catch (IOException | InterruptedException ex) {
 328             ex.printStackTrace(System.out);


< prev index next >