langtools/test/tools/javap/OptionTest.java
changeset 2987 2e0ca9f4893d
parent 2986 5370c4ae4f6f
child 2988 094272984b53
equal deleted inserted replaced
2986:5370c4ae4f6f 2987:2e0ca9f4893d
     1 /*
       
     2  * Copyright 2007-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  */
       
    23 
       
    24 import java.io.*;
       
    25 import java.util.*;
       
    26 
       
    27 /*
       
    28  * @test
       
    29  * @bug 6439940
       
    30  * @summary Cleanup javap implementation
       
    31  * @run main/othervm OptionTest
       
    32  */
       
    33 public class OptionTest {
       
    34     public static void main(String[] args) throws Exception {
       
    35         new OptionTest().run();
       
    36     }
       
    37 
       
    38     OptionTest() {
       
    39         String v = System.getProperty("view.cmd");
       
    40         if (v != null) {
       
    41             viewResults = true;
       
    42             viewCmd = Arrays.asList(v.split(" +"));
       
    43         }
       
    44     }
       
    45 
       
    46 
       
    47     void run() throws Exception {
       
    48         int count = 0;
       
    49         int pass = 0;
       
    50         // try combinations of options and compare old javap against new javap
       
    51         for (int i = 0; i < (1<<8); i++) {
       
    52             List<String> options = new ArrayList<String>();
       
    53             if ((i & 0x01) != 0)
       
    54                 options.add("-c");
       
    55             if ((i & 0x02) != 0)
       
    56                 options.add("-l");
       
    57             if ((i & 0x04) != 0)
       
    58                 options.add("-public");
       
    59             if ((i & 0x08) != 0)
       
    60                 options.add("-protected");
       
    61             if ((i & 0x10) != 0)
       
    62                 options.add("-package");
       
    63             if ((i & 0x20) != 0)
       
    64                 options.add("-private");
       
    65             if ((i & 0x40) != 0)
       
    66                 options.add("-s");
       
    67             if ((i & 0x80) != 0)
       
    68                 options.add("-verbose");
       
    69             count++;
       
    70             if (test(options))
       
    71                 pass++;
       
    72         }
       
    73 
       
    74         if (pass < count)
       
    75             throw new Error(pass + "/" + count + " test cases passed");
       
    76     }
       
    77 
       
    78     boolean test(List<String> options) throws Exception {
       
    79         String[] args = new String[options.size() + 1];
       
    80         options.toArray(args);
       
    81         args[args.length - 1] = testClassName;
       
    82         String oldOut = runOldJavap(args);
       
    83         String newOut = runNewJavap(args);
       
    84         boolean ok = oldOut.equals(newOut);
       
    85         System.err.println((ok ? "pass" : "FAIL") + ": " + options);
       
    86         if (!ok && viewResults)
       
    87             view(oldOut, newOut);
       
    88         return ok;
       
    89     }
       
    90 
       
    91     String runOldJavap(String[] args) {
       
    92         //System.err.println("OLD: " + Arrays.asList(args));
       
    93         PrintStream oldOut = System.out;
       
    94         ByteArrayOutputStream out = new ByteArrayOutputStream();
       
    95         System.setOut(new PrintStream(out));
       
    96         try {
       
    97             sun.tools.javap.Main.entry(args);
       
    98         } finally {
       
    99             System.setOut(oldOut);
       
   100         }
       
   101         return out.toString();
       
   102     }
       
   103 
       
   104     String runNewJavap(String[] args) {
       
   105         String[] nArgs = new String[args.length + 2];
       
   106         nArgs[0] = "-XDcompat";
       
   107         nArgs[1] = "-XDignore.symbol.file";
       
   108         System.arraycopy(args, 0, nArgs, 2, args.length);
       
   109         //System.err.println("NEW: " + Arrays.asList(nArgs));
       
   110         StringWriter out = new StringWriter();
       
   111         com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true));
       
   112         return out.toString();
       
   113     }
       
   114 
       
   115     File write(String text, String suffix) throws IOException {
       
   116         File f = File.createTempFile("OptionTest", suffix);
       
   117         FileWriter out = new FileWriter(f);
       
   118         out.write(text);
       
   119         out.close();
       
   120         return f;
       
   121     }
       
   122 
       
   123     void view(String oldOut, String newOut) throws Exception {
       
   124         File oldFile = write(oldOut, "old");
       
   125         File newFile = write(newOut, "new");
       
   126         List<String> cmd = new ArrayList<String>();
       
   127         cmd.addAll(viewCmd);
       
   128         cmd.add(oldFile.getPath());
       
   129         cmd.add(newFile.getPath());
       
   130         Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
       
   131         p.getOutputStream().close();
       
   132         String line;
       
   133         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
   134         while ((line = in.readLine()) != null)
       
   135             System.err.println(line);
       
   136         in.close();
       
   137         p.waitFor();
       
   138     }
       
   139 
       
   140     String testClassName = "java.lang.SecurityManager";
       
   141     boolean viewResults;
       
   142     List<String> viewCmd;
       
   143 }