|
1 /* |
|
2 * Copyright (c) 2010, 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 4942232 |
|
27 * @summary missing param class processes without error |
|
28 * @build ParamClassTest Test |
|
29 * @run main Test |
|
30 */ |
|
31 |
|
32 import java.io.*; |
|
33 import java.util.*; |
|
34 |
|
35 public class Test { |
|
36 public static void main(String... args) throws Exception { |
|
37 new Test().run(); |
|
38 } |
|
39 |
|
40 void run() throws Exception { |
|
41 File testSrc = new File(System.getProperty("test.src")); |
|
42 File testClasses = new File(System.getProperty("test.classes")); |
|
43 |
|
44 // standard use of javah on valid class file |
|
45 String[] test1Args = { |
|
46 "-d", mkdir("test1/out").getPath(), |
|
47 "-classpath", testClasses.getPath(), |
|
48 "ParamClassTest" |
|
49 }; |
|
50 test(test1Args, 0); |
|
51 |
|
52 // extended use of javah on valid source file |
|
53 String[] test2Args = { |
|
54 "-d", mkdir("test2/out").getPath(), |
|
55 "-classpath", testSrc.getPath(), |
|
56 "ParamClassTest" |
|
57 }; |
|
58 test(test2Args, 0); |
|
59 |
|
60 // javah on class file with missing referents |
|
61 File test3Classes = mkdir("test3/classes"); |
|
62 copy(new File(testClasses, "ParamClassTest.class"), test3Classes); |
|
63 String[] test3Args = { |
|
64 "-d", mkdir("test3/out").getPath(), |
|
65 "-classpath", test3Classes.getPath(), |
|
66 "ParamClassTest" |
|
67 }; |
|
68 test(test3Args, 1); |
|
69 |
|
70 // javah on source file with missing referents |
|
71 File test4Src = mkdir("test4/src"); |
|
72 String paramClassTestSrc = readFile(new File(testSrc, "ParamClassTest.java")); |
|
73 writeFile(new File(test4Src, "ParamClassTest.java"), |
|
74 paramClassTestSrc.replaceAll("class Param \\{\\s+\\}", "")); |
|
75 String[] test4Args = { |
|
76 "-d", mkdir("test4/out").getPath(), |
|
77 "-classpath", test4Src.getPath(), |
|
78 "ParamClassTest" |
|
79 }; |
|
80 test(test4Args, 15); |
|
81 |
|
82 if (errors > 0) |
|
83 throw new Exception(errors + " errors occurred"); |
|
84 } |
|
85 |
|
86 void test(String[] args, int expect) { |
|
87 System.err.println("test: " + Arrays.asList(args)); |
|
88 int rc = javah(args); |
|
89 if (rc != expect) |
|
90 error("Unexpected return code: " + rc + "; expected: " + expect); |
|
91 } |
|
92 |
|
93 int javah(String... args) { |
|
94 StringWriter sw = new StringWriter(); |
|
95 PrintWriter pw = new PrintWriter(sw); |
|
96 int rc = com.sun.tools.javah.Main.run(args, pw); |
|
97 pw.close(); |
|
98 String out = sw.toString(); |
|
99 if (!out.isEmpty()) |
|
100 System.err.println(out); |
|
101 return rc; |
|
102 } |
|
103 |
|
104 File mkdir(String path) { |
|
105 File f = new File(path); |
|
106 f.mkdirs(); |
|
107 return f; |
|
108 } |
|
109 |
|
110 void copy(File from, File to) throws IOException { |
|
111 if (to.isDirectory()) |
|
112 to = new File(to, from.getName()); |
|
113 try (DataInputStream in = new DataInputStream(new FileInputStream(from)); |
|
114 FileOutputStream out = new FileOutputStream(to)) { |
|
115 byte[] buf = new byte[(int) from.length()]; |
|
116 in.readFully(buf); |
|
117 out.write(buf); |
|
118 } |
|
119 } |
|
120 |
|
121 String readFile(File f) throws IOException { |
|
122 try (DataInputStream in = new DataInputStream(new FileInputStream(f))) { |
|
123 byte[] buf = new byte[(int) f.length()]; |
|
124 in.readFully(buf); |
|
125 return new String(buf); |
|
126 } |
|
127 } |
|
128 |
|
129 void writeFile(File f, String body) throws IOException { |
|
130 try (FileWriter out = new FileWriter(f)) { |
|
131 out.write(body); |
|
132 } |
|
133 } |
|
134 |
|
135 void error(String msg) { |
|
136 System.err.println(msg); |
|
137 errors++; |
|
138 } |
|
139 |
|
140 int errors; |
|
141 } |