|
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 javac's ToolProvider |
|
28 * @library /tools/lib |
|
29 * @build toolbox.TestRunner toolbox.ToolBox |
|
30 * @run main ToolProviderTest |
|
31 */ |
|
32 |
|
33 import java.io.*; |
|
34 import java.nio.file.Path; |
|
35 import java.nio.file.Paths; |
|
36 import java.util.*; |
|
37 import java.util.spi.ToolProvider; |
|
38 |
|
39 import toolbox.TestRunner; |
|
40 import toolbox.ToolBox; |
|
41 |
|
42 public class ToolProviderTest extends TestRunner { |
|
43 public static void main(String... args) throws Exception { |
|
44 new ToolProviderTest().runTests(); |
|
45 } |
|
46 |
|
47 ToolBox tb = new ToolBox(); |
|
48 ToolProvider javac; |
|
49 |
|
50 ToolProviderTest() { |
|
51 super(System.err); |
|
52 javac = ToolProvider.findFirst("javac").get(); |
|
53 } |
|
54 |
|
55 @Test |
|
56 public void testProviders() throws Exception { |
|
57 Map<String, ToolProvider> providers = new LinkedHashMap<>(); |
|
58 for (ToolProvider tp : ServiceLoader.load(ToolProvider.class, |
|
59 ClassLoader.getSystemClassLoader())) { |
|
60 System.out.println("Provider: " + tp.name() + ": " + tp.getClass().getName()); |
|
61 providers.put(tp.name(), tp); |
|
62 } |
|
63 if (!providers.containsKey("javac")) { |
|
64 error("javac ToolProvider not found"); |
|
65 } |
|
66 } |
|
67 |
|
68 @Test |
|
69 public void testOneStream() throws Exception { |
|
70 StringWriter sw = new StringWriter(); |
|
71 try (PrintWriter pw = new PrintWriter(sw)) { |
|
72 int rc = javac.run(pw, pw, "-help"); |
|
73 if (rc != 0) { |
|
74 error("unexpected exit code: " + rc); |
|
75 } |
|
76 } |
|
77 String out = sw.toString(); |
|
78 if (!out.contains("Usage:")) { |
|
79 error("expected output not found"); |
|
80 } |
|
81 } |
|
82 |
|
83 @Test |
|
84 public void testTwoStreamsOut() throws Exception { |
|
85 StringWriter swOut = new StringWriter(); |
|
86 StringWriter swErr = new StringWriter(); |
|
87 try (PrintWriter pwOut = new PrintWriter(swOut); |
|
88 PrintWriter pwErr = new PrintWriter(swErr)) { |
|
89 int rc = javac.run(pwOut, pwErr, "-help"); |
|
90 if (rc != 0) { |
|
91 error("unexpected exit code: " + rc); |
|
92 } |
|
93 } |
|
94 String out = swOut.toString(); |
|
95 String err = swErr.toString(); |
|
96 if (!out.contains("Usage:")) { |
|
97 error("stdout: expected output not found"); |
|
98 } |
|
99 if (!err.isEmpty()) { |
|
100 error("stderr: unexpected output"); |
|
101 } |
|
102 } |
|
103 |
|
104 @Test |
|
105 public void testTwoStreamsErr() throws Exception { |
|
106 Path src = Paths.get("src"); |
|
107 Path classes = Paths.get("classes"); |
|
108 tb.writeJavaFiles(src, |
|
109 "import java.util.*; class C { # }"); |
|
110 |
|
111 StringWriter swOut = new StringWriter(); |
|
112 StringWriter swErr = new StringWriter(); |
|
113 try (PrintWriter pwOut = new PrintWriter(swOut); |
|
114 PrintWriter pwErr = new PrintWriter(swErr)) { |
|
115 int rc = javac.run(pwOut, pwErr, |
|
116 "-d", classes.toString(), |
|
117 src.resolve("C.java").toString()); |
|
118 if (rc != 1) { |
|
119 error("unexpected exit code: " + rc); |
|
120 } |
|
121 } |
|
122 |
|
123 String out = swOut.toString(); |
|
124 String err = swErr.toString(); |
|
125 if (!out.isEmpty()) { |
|
126 error("stdout: unexpected output"); |
|
127 } |
|
128 if (!err.contains("illegal character")) { |
|
129 error("stderr: expected output not found"); |
|
130 } |
|
131 } |
|
132 } |