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