test/langtools/tools/javap/AccessModifiers.java
changeset 47216 71c04702a3d5
parent 36526 3b41f1c69604
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8027530
       
    27  * @summary test -public, -protected, -package, -private options
       
    28  * @modules jdk.jdeps/com.sun.tools.javap
       
    29  */
       
    30 
       
    31 import java.io.*;
       
    32 import java.util.*;
       
    33 import java.lang.StringBuilder;
       
    34 
       
    35 public class AccessModifiers {
       
    36     public int errorCount;
       
    37     protected String protectedField;
       
    38     String packageField;
       
    39     private String privateField;
       
    40 
       
    41     public static void main(String[] args) throws Exception {
       
    42         new AccessModifiers().run();
       
    43     }
       
    44 
       
    45     private void run() throws Exception {
       
    46         List<String> pubMembers = new ArrayList<String>();
       
    47         pubMembers.add("public int errorCount");
       
    48         pubMembers.add("public AccessModifiers");
       
    49         pubMembers.add("public static void main");
       
    50 
       
    51         List<String> proMembers = new ArrayList<String>();
       
    52         proMembers.add("protected java.lang.String protectedField");
       
    53         proMembers.add("protected java.lang.String runJavap");
       
    54 
       
    55         List<String> pkgMembers = new ArrayList<String>();
       
    56         pkgMembers.add("java.lang.String packageField");
       
    57         pkgMembers.add("boolean verify");
       
    58         pkgMembers.add("void error");
       
    59 
       
    60         List<String> priMembers = new ArrayList<String>();
       
    61         priMembers.add("private java.lang.String privateField");
       
    62         priMembers.add("private void run() throws java.lang.Exception");
       
    63         priMembers.add("private void test");
       
    64 
       
    65         List<String> expectedList = new ArrayList<String>();
       
    66 
       
    67         expectedList.addAll(pubMembers);
       
    68         test("-public", expectedList);
       
    69 
       
    70         expectedList.addAll(proMembers);
       
    71         test("-protected", expectedList);
       
    72 
       
    73         expectedList.addAll(pkgMembers);
       
    74         test("-package", expectedList);
       
    75 
       
    76         expectedList.addAll(priMembers);
       
    77         test("-private", expectedList);
       
    78 
       
    79         if (errorCount > 0)
       
    80             throw new Exception(errorCount + " errors received");
       
    81     }
       
    82 
       
    83     private void test(String option, List<String> expectedStrs) throws Exception {
       
    84         String output = runJavap(0, option);
       
    85         if (verify(output, expectedStrs))
       
    86             System.out.println(option + " test passed");
       
    87     }
       
    88 
       
    89     protected String runJavap(int expect, String... options) {
       
    90         // convert the varargs to a list in order to add class name
       
    91         List<String> optlist = new ArrayList<String>();
       
    92         optlist.addAll(Arrays.asList(options));
       
    93         optlist.add("AccessModifiers");
       
    94         String[] newoptions = optlist.toArray(new String[optlist.size()]);
       
    95         StringWriter sw = new StringWriter();
       
    96         PrintWriter pw = new PrintWriter(sw);
       
    97         System.out.printf("\nRun javap " + optlist + "\n\n");
       
    98         int rc = com.sun.tools.javap.Main.run(newoptions, pw);
       
    99         pw.close();
       
   100         System.out.println(sw);
       
   101         if (rc != expect)
       
   102            throw new Error("Expect to return " + expect + ", but return " + rc);
       
   103         return sw.toString();
       
   104     }
       
   105 
       
   106     boolean verify(String output, List<String> expects) {
       
   107         boolean pass = true;
       
   108         for (String expect: expects) {
       
   109             if (!output.contains(expect)) {
       
   110                 error(expect + " not found");
       
   111                 pass = false;
       
   112             }
       
   113         }
       
   114         return pass;
       
   115     }
       
   116 
       
   117     void error(String msg) {
       
   118         System.err.println(msg);
       
   119         errorCount++;
       
   120     }
       
   121 }