Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javah/4942232/Test.java
38813 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 494223226* @summary missing param class processes without error27* @build ParamClassTest Test28* @run main Test29*/3031import java.io.*;32import java.util.*;3334public class Test {35public static void main(String... args) throws Exception {36new Test().run();37}3839void run() throws Exception {40File testSrc = new File(System.getProperty("test.src"));41File testClasses = new File(System.getProperty("test.classes"));4243// standard use of javah on valid class file44String[] test1Args = {45"-d", mkdir("test1/out").getPath(),46"-classpath", testClasses.getPath(),47"ParamClassTest"48};49test(test1Args, 0);5051// extended use of javah on valid source file52String[] test2Args = {53"-d", mkdir("test2/out").getPath(),54"-classpath", testSrc.getPath(),55"ParamClassTest"56};57test(test2Args, 0);5859// javah on class file with missing referents60File test3Classes = mkdir("test3/classes");61copy(new File(testClasses, "ParamClassTest.class"), test3Classes);62String[] test3Args = {63"-d", mkdir("test3/out").getPath(),64"-classpath", test3Classes.getPath(),65"ParamClassTest"66};67test(test3Args, 1);6869// javah on source file with missing referents70File test4Src = mkdir("test4/src");71String paramClassTestSrc = readFile(new File(testSrc, "ParamClassTest.java"));72writeFile(new File(test4Src, "ParamClassTest.java"),73paramClassTestSrc.replaceAll("class Param \\{\\s+\\}", ""));74String[] test4Args = {75"-d", mkdir("test4/out").getPath(),76"-classpath", test4Src.getPath(),77"ParamClassTest"78};79test(test4Args, 15);8081if (errors > 0)82throw new Exception(errors + " errors occurred");83}8485void test(String[] args, int expect) {86System.err.println("test: " + Arrays.asList(args));87int rc = javah(args);88if (rc != expect)89error("Unexpected return code: " + rc + "; expected: " + expect);90}9192int javah(String... args) {93StringWriter sw = new StringWriter();94PrintWriter pw = new PrintWriter(sw);95int rc = com.sun.tools.javah.Main.run(args, pw);96pw.close();97String out = sw.toString();98if (!out.isEmpty())99System.err.println(out);100return rc;101}102103File mkdir(String path) {104File f = new File(path);105f.mkdirs();106return f;107}108109void copy(File from, File to) throws IOException {110if (to.isDirectory())111to = new File(to, from.getName());112try (DataInputStream in = new DataInputStream(new FileInputStream(from));113FileOutputStream out = new FileOutputStream(to)) {114byte[] buf = new byte[(int) from.length()];115in.readFully(buf);116out.write(buf);117}118}119120String readFile(File f) throws IOException {121try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {122byte[] buf = new byte[(int) f.length()];123in.readFully(buf);124return new String(buf);125}126}127128void writeFile(File f, String body) throws IOException {129try (FileWriter out = new FileWriter(f)) {130out.write(body);131}132}133134void error(String msg) {135System.err.println(msg);136errors++;137}138139int errors;140}141142143