test/jdk/java/util/spi/ToolProviderTest.java
changeset 53927 03924ad3f4d0
parent 47216 71c04702a3d5
equal deleted inserted replaced
53926:7272e7c5164b 53927:03924ad3f4d0
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26  * @bug 8159855
    26  * @bug 8159855
    27  * @summary test ToolProvider SPI
    27  * @summary test ToolProvider SPI
    28  * @run main/othervm ToolProviderTest
    28  * @run main/othervm ToolProviderTest
    29  */
    29  */
    30 
    30 
    31 import java.io.File;
       
    32 import java.io.IOException;
    31 import java.io.IOException;
    33 import java.io.PrintWriter;
    32 import java.io.PrintWriter;
    34 import java.nio.file.Files;
    33 import java.nio.file.Files;
    35 import java.nio.file.Path;
    34 import java.nio.file.Path;
    36 import java.nio.file.Paths;
    35 import java.nio.file.Paths;
    44     }
    43     }
    45 
    44 
    46     void run() throws Exception {
    45     void run() throws Exception {
    47         initServices();
    46         initServices();
    48 
    47 
       
    48         System.out.println("Validate an NPE is thrown with null arguments");
       
    49 
       
    50         testNullArgs();
       
    51 
    49         System.out.println("test without security manager present:");
    52         System.out.println("test without security manager present:");
    50         test();
    53         test();
    51 
    54 
    52         System.setSecurityManager(new SecurityManager());
    55         System.setSecurityManager(new SecurityManager());
    53 
    56 
    58     private void test() throws Exception {
    61     private void test() throws Exception {
    59         ToolProvider testProvider = ToolProvider.findFirst("test").get();
    62         ToolProvider testProvider = ToolProvider.findFirst("test").get();
    60         int rc = testProvider.run(System.out, System.err, "hello test");
    63         int rc = testProvider.run(System.out, System.err, "hello test");
    61         if (rc != 0) {
    64         if (rc != 0) {
    62             throw new Exception("unexpected exit code: " + rc);
    65             throw new Exception("unexpected exit code: " + rc);
       
    66         }
       
    67     }
       
    68 
       
    69     private void testNullArgs() {
       
    70         ToolProvider testProvider = ToolProvider.findFirst("test").get();
       
    71 
       
    72         // out null check
       
    73         expectNullPointerException(() -> testProvider.run(null, System.err));
       
    74 
       
    75         // err null check
       
    76         expectNullPointerException(() -> testProvider.run(System.out, null));
       
    77 
       
    78         // args array null check
       
    79         expectNullPointerException(() ->
       
    80                 testProvider.run(System.out, System.err, (String[]) null));
       
    81 
       
    82         // args array elements null check
       
    83         expectNullPointerException(() ->
       
    84                 testProvider.run(System.out, System.err, (String) null));
       
    85     }
       
    86 
       
    87     private static void expectNullPointerException(Runnable test) {
       
    88         try {
       
    89             test.run();
       
    90             throw new Error("NullPointerException not thrown");
       
    91         } catch (NullPointerException e) {
       
    92             // expected
    63         }
    93         }
    64     }
    94     }
    65 
    95 
    66     private void initServices() throws IOException {
    96     private void initServices() throws IOException {
    67         Path testClasses = Paths.get(System.getProperty("test.classes"));
    97         Path testClasses = Paths.get(System.getProperty("test.classes"));