1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javah/4942232/Test.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,141 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 4942232 1.30 + * @summary missing param class processes without error 1.31 + * @build ParamClassTest Test 1.32 + * @run main Test 1.33 + */ 1.34 + 1.35 +import java.io.*; 1.36 +import java.util.*; 1.37 + 1.38 +public class Test { 1.39 + public static void main(String... args) throws Exception { 1.40 + new Test().run(); 1.41 + } 1.42 + 1.43 + void run() throws Exception { 1.44 + File testSrc = new File(System.getProperty("test.src")); 1.45 + File testClasses = new File(System.getProperty("test.classes")); 1.46 + 1.47 + // standard use of javah on valid class file 1.48 + String[] test1Args = { 1.49 + "-d", mkdir("test1/out").getPath(), 1.50 + "-classpath", testClasses.getPath(), 1.51 + "ParamClassTest" 1.52 + }; 1.53 + test(test1Args, 0); 1.54 + 1.55 + // extended use of javah on valid source file 1.56 + String[] test2Args = { 1.57 + "-d", mkdir("test2/out").getPath(), 1.58 + "-classpath", testSrc.getPath(), 1.59 + "ParamClassTest" 1.60 + }; 1.61 + test(test2Args, 0); 1.62 + 1.63 + // javah on class file with missing referents 1.64 + File test3Classes = mkdir("test3/classes"); 1.65 + copy(new File(testClasses, "ParamClassTest.class"), test3Classes); 1.66 + String[] test3Args = { 1.67 + "-d", mkdir("test3/out").getPath(), 1.68 + "-classpath", test3Classes.getPath(), 1.69 + "ParamClassTest" 1.70 + }; 1.71 + test(test3Args, 1); 1.72 + 1.73 + // javah on source file with missing referents 1.74 + File test4Src = mkdir("test4/src"); 1.75 + String paramClassTestSrc = readFile(new File(testSrc, "ParamClassTest.java")); 1.76 + writeFile(new File(test4Src, "ParamClassTest.java"), 1.77 + paramClassTestSrc.replaceAll("class Param \\{\\s+\\}", "")); 1.78 + String[] test4Args = { 1.79 + "-d", mkdir("test4/out").getPath(), 1.80 + "-classpath", test4Src.getPath(), 1.81 + "ParamClassTest" 1.82 + }; 1.83 + test(test4Args, 15); 1.84 + 1.85 + if (errors > 0) 1.86 + throw new Exception(errors + " errors occurred"); 1.87 + } 1.88 + 1.89 + void test(String[] args, int expect) { 1.90 + System.err.println("test: " + Arrays.asList(args)); 1.91 + int rc = javah(args); 1.92 + if (rc != expect) 1.93 + error("Unexpected return code: " + rc + "; expected: " + expect); 1.94 + } 1.95 + 1.96 + int javah(String... args) { 1.97 + StringWriter sw = new StringWriter(); 1.98 + PrintWriter pw = new PrintWriter(sw); 1.99 + int rc = com.sun.tools.javah.Main.run(args, pw); 1.100 + pw.close(); 1.101 + String out = sw.toString(); 1.102 + if (!out.isEmpty()) 1.103 + System.err.println(out); 1.104 + return rc; 1.105 + } 1.106 + 1.107 + File mkdir(String path) { 1.108 + File f = new File(path); 1.109 + f.mkdirs(); 1.110 + return f; 1.111 + } 1.112 + 1.113 + void copy(File from, File to) throws IOException { 1.114 + if (to.isDirectory()) 1.115 + to = new File(to, from.getName()); 1.116 + try (DataInputStream in = new DataInputStream(new FileInputStream(from)); 1.117 + FileOutputStream out = new FileOutputStream(to)) { 1.118 + byte[] buf = new byte[(int) from.length()]; 1.119 + in.readFully(buf); 1.120 + out.write(buf); 1.121 + } 1.122 + } 1.123 + 1.124 + String readFile(File f) throws IOException { 1.125 + try (DataInputStream in = new DataInputStream(new FileInputStream(f))) { 1.126 + byte[] buf = new byte[(int) f.length()]; 1.127 + in.readFully(buf); 1.128 + return new String(buf); 1.129 + } 1.130 + } 1.131 + 1.132 + void writeFile(File f, String body) throws IOException { 1.133 + try (FileWriter out = new FileWriter(f)) { 1.134 + out.write(body); 1.135 + } 1.136 + } 1.137 + 1.138 + void error(String msg) { 1.139 + System.err.println(msg); 1.140 + errors++; 1.141 + } 1.142 + 1.143 + int errors; 1.144 +}