test/tools/javap/ListTest.java

Mon, 27 Oct 2008 14:25:59 -0700

author
jjg
date
Mon, 27 Oct 2008 14:25:59 -0700
changeset 164
5ebe90e0afff
parent 46
7708bd6d800d
permissions
-rw-r--r--

6764226: ListTest fails on javap output with bad characters
Reviewed-by: darcy

     1 /*
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 import java.io.*;
    25 import java.util.*;
    26 import javax.tools.*;
    28 /*
    29  * @test
    30  * @bug 6439940
    31  * @summary Cleanup javap implementation
    32  * @run main/othervm ListTest
    33  */
    34 public class ListTest {
    35     public static void main(String[] args) throws Exception {
    36         new ListTest().run();
    37     }
    39     ListTest() {
    40         String v = System.getProperty("view.cmd");
    41         //      v = "/opt/teamware/7.7/bin/filemerge -r";
    42         if (v != null) {
    43             viewResults = true;
    44             viewCmd = Arrays.asList(v.split(" +"));
    45         }
    46     }
    48     void run() throws Exception {
    49         StandardLocation[] locs = new StandardLocation[] {
    50             StandardLocation.PLATFORM_CLASS_PATH,
    51             StandardLocation.CLASS_PATH,
    52         };
    54         int count = 0;
    55         int pass = 0;
    56         for (StandardLocation loc: locs) {
    57             for (String testClassName: list(loc)) {
    58                 count++;
    59                 if (test(testClassName))
    60                     pass++;
    61             }
    62         }
    64         if (pass < count)
    65             throw new Error(pass + "/" + count + " test cases passed");
    66     }
    68     Iterable<String> list(StandardLocation loc) throws IOException {
    69         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    70         StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null);
    71         Set<JavaFileObject.Kind> kinds = Collections.singleton(JavaFileObject.Kind.CLASS);
    73         List<String> list = new ArrayList<String>();
    74         for (JavaFileObject fo: sfm.list(loc, testPackage, kinds, true)) {
    75             //System.err.println(com.sun.tools.javac.util.Old199.getPath(fo));
    76             list.add(sfm.inferBinaryName(loc, fo));
    77         }
    78         return list;
    79     }
    81     boolean test(String testClassName) throws Exception {
    82         String[] args = new String[options.size() + 1];
    83         options.toArray(args);
    84         args[args.length - 1] = testClassName;
    85         byte[] oldOut = runOldJavap(args);
    86         byte[] newOut = runNewJavap(args);
    87         boolean ok = equal(oldOut, newOut);
    88         System.err.println((ok ? "pass" : "FAIL") + ": " + testClassName);
    89         if (!ok && viewResults)
    90             view(oldOut, newOut);
    91         return ok;
    92     }
    94     byte[] runOldJavap(String[] args) {
    95         //System.err.println("OLD: " + Arrays.asList(args));
    96         PrintStream oldOut = System.out;
    97         ByteArrayOutputStream out = new ByteArrayOutputStream();
    98         System.setOut(new PrintStream(out));
    99         try {
   100             sun.tools.javap.Main.entry(args);
   101         } finally {
   102             System.setOut(oldOut);
   103         }
   104         return out.toByteArray();
   105     }
   107     byte[] runNewJavap(String[] args) {
   108         String[] nArgs = new String[args.length + 2];
   109         nArgs[0] = "-XDcompat";
   110         nArgs[1] = "-XDignore.symbol.file";
   111         System.arraycopy(args, 0, nArgs, 2, args.length);
   112         //System.err.println("NEW: " + Arrays.asList(nArgs));
   113         ByteArrayOutputStream out = new ByteArrayOutputStream();
   114         com.sun.tools.javap.Main.run(nArgs,
   115                                      new PrintWriter(new OutputStreamWriter(out), true));
   116         return out.toByteArray();
   117     }
   119     File write(byte[] text, String suffix) throws IOException {
   120         File f = new File("ListTest." + suffix);
   121         FileOutputStream out = new FileOutputStream(f);
   122         out.write(text);
   123         out.close();
   124         return f;
   125     }
   127     boolean equal(byte[] a1, byte[] a2) {
   128         return Arrays.equals(a1, a2);
   129     }
   131     void view(byte[] oldOut, byte[] newOut) throws Exception {
   132         File oldFile = write(oldOut, "old");
   133         File newFile = write(newOut, "new");
   134         List<String> cmd = new ArrayList<String>();
   135         cmd.addAll(viewCmd);
   136         cmd.add(oldFile.getPath());
   137         cmd.add(newFile.getPath());
   138         Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
   139         p.getOutputStream().close();
   140         String line;
   141         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   142         while ((line = in.readLine()) != null)
   143             System.err.println(line);
   144         in.close();
   145         p.waitFor();
   146     }
   148     String testPackage = "java.lang";
   149     List<String> options = Arrays.asList("-v");
   150     boolean viewResults;
   151     List<String> viewCmd;
   152 }

mercurial