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