test/jdk/java/lang/ClassLoader/GetSystemPackage.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 40684 jdk/test/java/lang/ClassLoader/GetSystemPackage.java@2e37c119dc2a
child 51675 b487c1e914d0
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     1
/*
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 36511
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     4
 *
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     8
 *
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    13
 * accompanied this code).
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    14
 *
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    18
 *
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    21
 * questions.
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    22
 */
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    23
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    24
/*
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    25
 * @test
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    26
 * @bug 8060130
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    27
 * @library /lib/testlibrary
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    28
 * @build package2.Class2 GetSystemPackage jdk.testlibrary.*
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    29
 * @summary Test if getSystemPackage() return consistent values for cases
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    30
 *          where a manifest is provided or not and ensure only jars on
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    31
 *          bootclasspath gets resolved via Package.getSystemPackage
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    32
 * @run main GetSystemPackage
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    33
 */
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    34
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    35
import java.io.File;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    36
import java.io.FileInputStream;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    37
import java.io.FileNotFoundException;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    38
import java.io.FileOutputStream;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    39
import java.io.IOException;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    40
import java.util.jar.Attributes;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    41
import java.util.jar.JarEntry;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    42
import java.util.jar.JarOutputStream;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    43
import java.util.jar.Manifest;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    44
import jdk.testlibrary.ProcessTools;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    45
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    46
public class GetSystemPackage {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    47
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    48
    static final String testClassesDir = System.getProperty("test.classes", ".");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    49
    static final File tmpFolder = new File(testClassesDir);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    50
    static final String manifestTitle = "Special JAR";
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    51
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    52
    public static void main(String ... args) throws Exception {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    53
        if (args.length == 0) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    54
            buildJarsAndInitiateSystemPackageTest();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    55
            return;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    56
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    57
        switch (args[0]) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    58
            case "system-manifest":
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    59
                verifyPackage(true, true);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    60
                break;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    61
            case "system-no-manifest":
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    62
                verifyPackage(false, true);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    63
                break;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    64
            case "non-system-manifest":
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    65
                verifyPackage(true, false);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    66
                break;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    67
            case "non-system-no-manifest":
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    68
            default:
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    69
                verifyPackage(false, false);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    70
                break;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    71
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    72
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    73
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    74
    private static void buildJarsAndInitiateSystemPackageTest()
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    75
            throws Exception
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    76
    {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    77
        Manifest m = new Manifest();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    78
        // not setting MANIFEST_VERSION prevents META-INF/MANIFEST.MF from
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    79
        // getting written
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    80
        m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    81
        m.getMainAttributes().put(Attributes.Name.SPECIFICATION_TITLE,
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    82
                                  manifestTitle);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    83
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    84
        buildJar("manifest.jar", m);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    85
        buildJar("no-manifest.jar", null);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    86
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    87
        runSubProcess("System package with manifest improperly resolved.",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
    88
                "-Xbootclasspath/a:" + testClassesDir + "/manifest.jar",
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    89
                "GetSystemPackage", "system-manifest");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    90
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    91
        runSubProcess("System package from directory improperly resolved.",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
    92
                "-Xbootclasspath/a:" + testClassesDir, "GetSystemPackage",
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    93
                "system-no-manifest");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    94
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    95
        runSubProcess("System package with no manifest improperly resolved",
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
    96
                "-Xbootclasspath/a:" + testClassesDir + "/no-manifest.jar",
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    97
                "GetSystemPackage", "system-no-manifest");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    98
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
    99
        runSubProcess("Classpath package with manifest improperly resolved",
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   100
                "-cp", testClassesDir + "/manifest.jar", "GetSystemPackage",
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   101
                "non-system-manifest");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   102
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   103
        runSubProcess("Classpath package with no manifest improperly resolved",
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   104
                "-cp", testClassesDir + "/no-manifest.jar", "GetSystemPackage",
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   105
                "non-system-no-manifest");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   106
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   107
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   108
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   109
    private static void buildJar(String name, Manifest man) throws Exception {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   110
        JarBuilder jar = new JarBuilder(tmpFolder, name, man);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   111
        jar.addClassFile("package2/Class2.class",
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   112
                testClassesDir + "/package2/Class2.class");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   113
        jar.addClassFile("GetSystemPackage.class",
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   114
                testClassesDir + "/GetSystemPackage.class");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   115
        jar.build();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   116
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   117
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   118
    private static void runSubProcess(String messageOnError, String ... args)
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   119
            throws Exception
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   120
    {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   121
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   122
        int res = pb.directory(tmpFolder).inheritIO().start().waitFor();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   123
        if (res != 0) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   124
            throw new RuntimeException(messageOnError);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   125
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   126
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   127
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   128
    private static void verifyPackage(boolean hasManifest,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
   129
                                      boolean isSystemPackage)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
   130
            throws Exception
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   131
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
   132
        Class<?> c = Class.forName("package2.Class2");
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   133
        Package pkg = c.getPackage();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   134
        if (pkg == null || pkg != Package.getPackage("package2") ||
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   135
                !"package2".equals(pkg.getName())) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   136
            fail("package2 not found via Package.getPackage()");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   137
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   138
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   139
        String specificationTitle = pkg.getSpecificationTitle();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   140
        if (!"package2".equals(pkg.getName())) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   141
            fail("Invalid package for Class2");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   142
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   143
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   144
        if (hasManifest && (specificationTitle == null
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   145
                || !manifestTitle.equals(specificationTitle))) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   146
            fail("Invalid manifest for package " + pkg.getName());
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   147
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   148
        if (!hasManifest && specificationTitle != null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   149
            fail("Invalid manifest for package " + pkg.getName() + ": was " +
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
   150
                    specificationTitle + " expected: null");
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   151
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   152
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
   153
        ClassLoader ld = c.getClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32230
diff changeset
   154
        Package systemPkg = ld != null ? null : Package.getPackage("package2");
27348
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   155
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   156
        if (findPackage("java.lang") == null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   157
            fail("java.lang not found via Package.getPackages()");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   158
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   159
        Package foundPackage = findPackage("package2");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   160
        if (isSystemPackage) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   161
            if (systemPkg == null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   162
                fail("System package could not be found via getSystemPackage");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   163
            }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   164
            if (foundPackage != systemPkg || systemPkg != pkg) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   165
                fail("Inconsistent package found via Package.getPackages()");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   166
            }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   167
        } else {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   168
            if (systemPkg != null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   169
                fail("Non-system package could be found via getSystemPackage");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   170
            }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   171
            if (foundPackage == null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   172
                fail("Non-system package not found via Package.getPackages()");
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   173
            }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   174
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   175
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   176
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   177
    private static Package findPackage(String name) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   178
        Package[] packages = Package.getPackages();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   179
        for (Package p : packages) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   180
            if (p.getName().equals(name)) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   181
                return p;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   182
            }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   183
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   184
        return null;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   185
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   186
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   187
    private static void fail(String message) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   188
        throw new RuntimeException(message);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   189
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   190
}
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   191
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   192
/*
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   193
 * Helper class for building jar files
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   194
 */
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   195
class JarBuilder {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   196
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   197
    private JarOutputStream os;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   198
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   199
    public JarBuilder(File tmpFolder, String jarName, Manifest manifest)
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   200
            throws FileNotFoundException, IOException
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   201
    {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   202
        File jarFile = new File(tmpFolder, jarName);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   203
        if (manifest != null) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   204
            this.os = new JarOutputStream(new FileOutputStream(jarFile),
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   205
                                          manifest);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   206
        } else {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   207
            this.os = new JarOutputStream(new FileOutputStream(jarFile));
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   208
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   209
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   210
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   211
    public void addClassFile(String pathFromRoot, String file)
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   212
            throws IOException
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   213
    {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   214
        byte[] buf = new byte[1024];
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   215
        try (FileInputStream in = new FileInputStream(file)) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   216
            JarEntry entry = new JarEntry(pathFromRoot);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   217
            os.putNextEntry(entry);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   218
            int len;
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   219
            while ((len = in.read(buf)) > 0) {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   220
                os.write(buf, 0, len);
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   221
            }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   222
            os.closeEntry();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   223
        }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   224
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   225
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   226
    public void build() throws IOException {
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   227
        os.close();
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   228
    }
1614e0fd2b9b 8060130: Simplify the synchronization of defining and getting java.lang.Package
redestad
parents:
diff changeset
   229
}