Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javah/6572945/T6572945.java
38813 views
/*1* Copyright (c) 2007, 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 657294526* @summary rewrite javah as an annotation processor, instead of as a doclet27* @build TestClass1 TestClass2 TestClass328* @run main T657294529*/3031import java.io.*;32import java.util.*;33import com.sun.tools.javah.Main;3435public class T657294536{37static File testSrc = new File(System.getProperty("test.src", "."));38static File testClasses = new File(System.getProperty("test.classes", "."));39static boolean isWindows = System.getProperty("os.name").startsWith("Windows");4041public static void main(String... args)42throws IOException, InterruptedException43{44boolean ok = new T6572945().run(args);45if (!ok)46throw new Error("Test Failed");47}4849public boolean run(String[] args)50throws IOException, InterruptedException51{52if (args.length == 1)53jdk = new File(args[0]);5455test("-o", "jni.file.1", "-jni", "TestClass1");56test("-o", "jni.file.2", "-jni", "TestClass1", "TestClass2");57test("-d", "jni.dir.1", "-jni", "TestClass1", "TestClass2");58test("-o", "jni.file.3", "-jni", "TestClass3");5960// The following tests are disabled because llni support has been61// discontinued, and because bugs in old javah means that character62// for character testing against output from old javah does not work.63// In fact, the LLNI impl in new javah is actually better than the64// impl in old javah because of a couple of significant bug fixes.6566// test("-o", "llni.file.1", "-llni", "TestClass1");67// test("-o", "llni.file.2", "-llni", "TestClass1", "TestClass2");68// test("-d", "llni.dir.1", "-llni", "TestClass1", "TestClass2");69// test("-o", "llni.file.3", "-llni", "TestClass3");7071return (errors == 0);72}7374void test(String... args)75throws IOException, InterruptedException76{77String[] cp_args = new String[args.length + 2];78cp_args[0] = "-classpath";79cp_args[1] = testClasses.getPath();80System.arraycopy(args, 0, cp_args, 2, args.length);8182if (jdk != null)83init(cp_args);8485File out = null;86for (int i = 0; i < args.length; i++) {87if (args[i].equals("-o")) {88out = new File(args[++i]);89break;90} else if (args[i].equals("-d")) {91out = new File(args[++i]);92out.mkdirs();93break;94}95}9697try {98System.out.println("test: " + Arrays.asList(cp_args));99100// // Uncomment and use the following lines to execute javah via the101// // command line -- for example, to run old javah and set up the golden files102// List<String> cmd = new ArrayList<String>();103// File javaHome = new File(System.getProperty("java.home"));104// if (javaHome.getName().equals("jre"))105// javaHome = javaHome.getParentFile();106// File javah = new File(new File(javaHome, "bin"), "javah");107// cmd.add(javah.getPath());108// cmd.addAll(Arrays.asList(cp_args));109// ProcessBuilder pb = new ProcessBuilder(cmd);110// pb.redirectErrorStream(true);111// pb.start();112// Process p = pb.start();113// String line;114// BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));115// while ((line = in.readLine()) != null)116// System.err.println(line);117// in.close();118// int rc = p.waitFor();119120// Use new javah121PrintWriter err = new PrintWriter(System.err, true);122int rc = Main.run(cp_args, err);123124if (rc != 0) {125error("javah failed: rc=" + rc);126return;127}128129// The golden files use the LL suffix for long constants, which130// is used on Linux and Solaris. On Windows, the suffix is i64,131// so compare will update the golden files on the fly before the132// final comparison.133compare(new File(new File(testSrc, "gold"), out.getName()), out);134} catch (Throwable t) {135t.printStackTrace();136error("javah threw exception");137}138}139140void init(String[] args) throws IOException, InterruptedException {141String[] cmdArgs = new String[args.length + 1];142cmdArgs[0] = new File(new File(jdk, "bin"), "javah").getPath();143System.arraycopy(args, 0, cmdArgs, 1, args.length);144145System.out.println("init: " + Arrays.asList(cmdArgs));146147ProcessBuilder pb = new ProcessBuilder(cmdArgs);148pb.directory(new File(testSrc, "gold"));149pb.redirectErrorStream(true);150Process p = pb.start();151BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));152String line;153while ((line = in.readLine()) != null)154System.out.println("javah: " + line);155int rc = p.waitFor();156if (rc != 0)157error("javah: exit code " + rc);158}159160/** Compare two directories.161* @param f1 The golden directory162* @param f2 The directory to be compared163*/164void compare(File f1, File f2) {165compare(f1, f2, null);166}167168/** Compare two files or directories169* @param f1 The golden directory170* @param f2 The directory to be compared171* @param p An optional path identifying a file within the two directories172*/173void compare(File f1, File f2, String p) {174File f1p = (p == null ? f1 : new File(f1, p));175File f2p = (p == null ? f2 : new File(f2, p));176System.out.println("compare " + f1p + " " + f2p);177if (f1p.isDirectory() && f2p.isDirectory()) {178Set<String> children = new HashSet<String>();179children.addAll(Arrays.asList(f1p.list()));180children.addAll(Arrays.asList(f2p.list()));181for (String c: children) {182compare(f1, f2, new File(p, c).getPath()); // null-safe for p183}184}185else if (f1p.isFile() && f2p.isFile()) {186String s1 = read(f1p);187if (isWindows) {188// f1/s1 is the golden file189// on Windows, long constants use the i64 suffix, not LL190s1 = s1.replaceAll("( [0-9]+)LL\n", "$1i64\n");191}192String s2 = read(f2p);193if (!s1.equals(s2)) {194System.out.println("File: " + f1p + "\n" + s1);195System.out.println("File: " + f2p + "\n" + s2);196error("Files differ: " + f1p + " " + f2p);197}198}199else if (f1p.exists() && !f2p.exists())200error("Only in " + f1 + ": " + p);201else if (f2p.exists() && !f1p.exists())202error("Only in " + f2 + ": " + p);203else204error("Files differ: " + f1p + " " + f2p);205}206207private String read(File f) {208try {209BufferedReader in = new BufferedReader(new FileReader(f));210try {211StringBuilder sb = new StringBuilder((int) f.length());212String line;213while ((line = in.readLine()) != null) {214sb.append(line);215sb.append("\n");216}217return sb.toString();218} finally {219try {220in.close();221} catch (IOException e) {222}223}224} catch (IOException e) {225error("error reading " + f + ": " + e);226return "";227}228}229230231private void error(String msg) {232System.out.println(msg);233errors++;234}235236private int errors;237private File jdk;238}239240241