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 8022287
27  * @summary javac.sym.Profiles uses a static Map when it should not
28  */
29 
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 
35 import com.sun.tools.javac.sym.Profiles;
36 
37 public class ProfileTest {
main(String... args)38     public static void main(String... args) throws Exception {
39         new ProfileTest().run();
40     }
41 
run()42     public void run() throws Exception {
43         test("A");
44         test("B");
45 
46         if (errors > 0)
47             throw new Exception(errors + " occurred");
48     }
49 
test(String base)50     void test(String base) throws IOException {
51         System.err.println("test " + base);
52         File profileDesc = createFiles(base);
53         checkProfile(profileDesc, base);
54     }
55 
checkProfile(File profileDesc, String base)56     void checkProfile(File profileDesc, String base) throws IOException {
57         Profiles p = Profiles.read(profileDesc);
58         for (int i = 0; i < p.getProfileCount(); i++) {
59             System.err.println(p.getPackages(i));
60             for (String pkg: p.getPackages(i)) {
61                 if (!pkg.endsWith(base))
62                     error("unexpected package " + pkg + " for profile " + i);
63             }
64         }
65     }
66 
createFiles(String base)67     File createFiles(String base) throws IOException {
68         File baseDir = new File(base);
69         baseDir.mkdirs();
70         for (int p = 1; p <= 4; p++) {
71             String pkg = "pkg" + p + base;
72             File pkgDir = new File(baseDir, pkg);
73             pkgDir.mkdirs();
74             File clssFile = new File(pkgDir, pkg + "Class.java");
75             try (PrintWriter out = new PrintWriter(new FileWriter(clssFile))) {
76                 out.println("package " + pkgDir.getName() + ";");
77                 out.println("class " + clssFile.getName().replace(".java", ""));
78             }
79         }
80 
81         File profileDesc = new File(baseDir, "profiles" + base + ".txt");
82         try (PrintWriter out = new PrintWriter(new FileWriter(profileDesc))) {
83             for (int p = 1; p <= 4; p++) {
84                 String pkg = "pkg" + p + base;
85                 createPackage(baseDir, pkg, "Pkg" + p + base + "Class");
86                 out.println("PROFILE_" + p + "_RTJAR_INCLUDE_PACKAGES := " + pkg);
87                 out.println("PROFILE_" + p + "_RTJAR_INCLUDE_TYPES :=");
88                 out.println("PROFILE_" + p + "_RTJAR_EXCLUDE_TYPES :=");
89                 out.println("PROFILE_" + p + "_INCLUDE_METAINF_SERVICES := ");
90             }
91         }
92 
93         return profileDesc;
94     }
95 
createPackage(File baseDir, String pkg, String... classNames)96     void createPackage(File baseDir, String pkg, String... classNames) throws IOException {
97         File pkgDir = new File(baseDir, pkg);
98         pkgDir.mkdirs();
99         for (String className: classNames) {
100             File clssFile = new File(pkgDir, className + ".java");
101             try (PrintWriter out = new PrintWriter(new FileWriter(clssFile))) {
102                 out.println("package " + pkg + ";");
103                 out.println("public class " + className + " { }");
104             }
105         }
106     }
107 
error(String msg)108     void error(String msg) {
109         System.err.println("Error: " + msg);
110         errors++;
111     }
112 
113     int errors;
114 }
115