test/tools/javap/AccessModifiers.java

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

mercurial