author | lancea |
Tue, 26 Feb 2019 13:14:26 -0500 | |
changeset 53927 | 03924ad3f4d0 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
41376 | 1 |
/* |
53927
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
41376 | 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.IOException; |
|
32 |
import java.io.PrintWriter; |
|
33 |
import java.nio.file.Files; |
|
34 |
import java.nio.file.Path; |
|
35 |
import java.nio.file.Paths; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.spi.ToolProvider; |
|
38 |
||
39 |
public class ToolProviderTest { |
|
40 |
public static void main(String... args) throws Exception { |
|
41 |
ToolProviderTest t = new ToolProviderTest(); |
|
42 |
t.run(); |
|
43 |
} |
|
44 |
||
45 |
void run() throws Exception { |
|
46 |
initServices(); |
|
47 |
||
53927
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
48 |
System.out.println("Validate an NPE is thrown with null arguments"); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
49 |
|
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
50 |
testNullArgs(); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
51 |
|
41376 | 52 |
System.out.println("test without security manager present:"); |
53 |
test(); |
|
54 |
||
55 |
System.setSecurityManager(new SecurityManager()); |
|
56 |
||
57 |
System.out.println("test with security manager present:"); |
|
58 |
test(); |
|
59 |
} |
|
60 |
||
61 |
private void test() throws Exception { |
|
62 |
ToolProvider testProvider = ToolProvider.findFirst("test").get(); |
|
63 |
int rc = testProvider.run(System.out, System.err, "hello test"); |
|
64 |
if (rc != 0) { |
|
65 |
throw new Exception("unexpected exit code: " + rc); |
|
66 |
} |
|
67 |
} |
|
68 |
||
53927
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
69 |
private void testNullArgs() { |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
70 |
ToolProvider testProvider = ToolProvider.findFirst("test").get(); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
71 |
|
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
72 |
// out null check |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
73 |
expectNullPointerException(() -> testProvider.run(null, System.err)); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
74 |
|
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
75 |
// err null check |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
76 |
expectNullPointerException(() -> testProvider.run(System.out, null)); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
77 |
|
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
78 |
// args array null check |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
79 |
expectNullPointerException(() -> |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
80 |
testProvider.run(System.out, System.err, (String[]) null)); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
81 |
|
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
82 |
// args array elements null check |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
83 |
expectNullPointerException(() -> |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
84 |
testProvider.run(System.out, System.err, (String) null)); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
85 |
} |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
86 |
|
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
87 |
private static void expectNullPointerException(Runnable test) { |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
88 |
try { |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
89 |
test.run(); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
90 |
throw new Error("NullPointerException not thrown"); |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
91 |
} catch (NullPointerException e) { |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
92 |
// expected |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
93 |
} |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
94 |
} |
03924ad3f4d0
8219548: Better Null paramenter checking in ToolProvider
lancea
parents:
47216
diff
changeset
|
95 |
|
41376 | 96 |
private void initServices() throws IOException { |
97 |
Path testClasses = Paths.get(System.getProperty("test.classes")); |
|
98 |
Path services = testClasses.resolve(Paths.get("META-INF", "services")); |
|
99 |
Files.createDirectories(services); |
|
100 |
Files.write(services.resolve(ToolProvider.class.getName()), |
|
101 |
Arrays.asList(TestProvider.class.getName())); |
|
102 |
} |
|
103 |
||
104 |
public static class TestProvider implements ToolProvider { |
|
105 |
public TestProvider() { |
|
106 |
checkPrivileges(); |
|
107 |
} |
|
108 |
||
109 |
public String name() { |
|
110 |
return "test"; |
|
111 |
} |
|
112 |
||
113 |
public int run(PrintWriter out, PrintWriter err, String... args) { |
|
114 |
out.println("Test: " + Arrays.toString(args)); |
|
115 |
return 0; |
|
116 |
} |
|
117 |
||
118 |
private void checkPrivileges() { |
|
119 |
boolean haveSecurityManager = (System.getSecurityManager() != null); |
|
120 |
try { |
|
121 |
// validate appropriate privileges by checking access to a |
|
122 |
// system property |
|
123 |
System.getProperty("java.home"); |
|
124 |
if (haveSecurityManager) { |
|
125 |
throw new Error("exception exception not thrown"); |
|
126 |
} |
|
127 |
} catch (SecurityException e) { |
|
128 |
if (!haveSecurityManager) { |
|
129 |
throw new Error("unexpected exception: " + e); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |