test/jdk/java/util/spi/ToolProviderTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 41376 jdk/test/java/util/spi/ToolProviderTest.java@0908484888a7
child 53927 03924ad3f4d0
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     1
/*
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     4
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     8
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    13
 * accompanied this code).
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    14
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    18
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    21
 * questions.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    22
 */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    23
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    24
/*
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    25
 * @test
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    26
 * @bug 8159855
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    27
 * @summary test ToolProvider SPI
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    28
 * @run main/othervm ToolProviderTest
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    29
 */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    30
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    31
import java.io.File;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    32
import java.io.IOException;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    33
import java.io.PrintWriter;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    34
import java.nio.file.Files;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    35
import java.nio.file.Path;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    36
import java.nio.file.Paths;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    37
import java.util.Arrays;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    38
import java.util.spi.ToolProvider;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    39
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    40
public class ToolProviderTest {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    41
    public static void main(String... args) throws Exception {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    42
        ToolProviderTest t = new ToolProviderTest();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    43
        t.run();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    44
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    45
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    46
    void run() throws Exception {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    47
        initServices();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    48
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    49
        System.out.println("test without security manager present:");
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    50
        test();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    51
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    52
        System.setSecurityManager(new SecurityManager());
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    53
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    54
        System.out.println("test with security manager present:");
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    55
        test();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    56
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    57
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    58
    private void test() throws Exception {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    59
        ToolProvider testProvider = ToolProvider.findFirst("test").get();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    60
        int rc = testProvider.run(System.out, System.err, "hello test");
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    61
        if (rc != 0) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    62
            throw new Exception("unexpected exit code: " + rc);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    63
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    64
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    65
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    66
    private void initServices() throws IOException {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    67
        Path testClasses = Paths.get(System.getProperty("test.classes"));
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    68
        Path services = testClasses.resolve(Paths.get("META-INF", "services"));
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    69
        Files.createDirectories(services);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    70
        Files.write(services.resolve(ToolProvider.class.getName()),
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    71
                Arrays.asList(TestProvider.class.getName()));
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    72
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    73
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    74
    public static class TestProvider implements ToolProvider {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    75
        public TestProvider() {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    76
            checkPrivileges();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    77
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    78
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    79
        public String name() {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    80
            return "test";
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    81
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    82
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    83
        public int run(PrintWriter out, PrintWriter err, String... args) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    84
            out.println("Test: " + Arrays.toString(args));
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    85
            return 0;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    86
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    87
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    88
        private void checkPrivileges() {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    89
            boolean haveSecurityManager = (System.getSecurityManager() != null);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    90
            try {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    91
                // validate appropriate privileges by checking access to a
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    92
                // system property
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    93
                System.getProperty("java.home");
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    94
                if (haveSecurityManager) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    95
                    throw new Error("exception exception not thrown");
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    96
                }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    97
            } catch (SecurityException e) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    98
                if (!haveSecurityManager) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    99
                    throw new Error("unexpected exception: " + e);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   100
                }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   101
            }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   102
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   103
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   104
}
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   105