aboutsummaryrefslogtreecommitdiff
path: root/test/sun/misc/URLClassPath/JarClassPathFileEntry.java
blob: 6f57ec514d6def168ebb9e136f3900fb4e127f51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import jdk.testlibrary.InMemoryJavaCompiler;
import jdk.testlibrary.JarUtils;

/*
 * @test
 * @bug 8216401
 * @summary Test loading of JAR Class-Path entry with file: scheme
 * @library /lib/testlibrary
 *
 * @run main/othervm JarClassPathFileEntry
 * @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=true JarClassPathFileEntry
 * @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=false JarClassPathFileEntry
 */

public class JarClassPathFileEntry {
    private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");

    private final static String TEST_CLASSES = System.getProperty("test.classes");
    private final static String OTHER_DIR = TEST_CLASSES + "/OTHER/";

    private final static Path OTHER_JAR_PATH = Paths.get(OTHER_DIR, "Other.jar");
    private final static Path CONTEXT_JAR_PATH = Paths.get(TEST_CLASSES, "Context.jar");

    public static void main(String[] args) throws Throwable {
        // Create Other.class in OTHER_DIR, off the default classpath
        byte klassbuf[] = InMemoryJavaCompiler.compile("Other",
                                                       "public class Other {}");
        ClassFileInstaller.writeClassToDisk("Other", klassbuf, OTHER_DIR);

        // Create Other.jar in OTHER_DIR
        JarUtils.createJarFile(OTHER_JAR_PATH,
                               Paths.get(OTHER_DIR),
                               Paths.get(OTHER_DIR, "Other.class"));

        // Create Context.class
        klassbuf = InMemoryJavaCompiler.compile("Context",
                                                "public class Context {}");
        ClassFileInstaller.writeClassToDisk("Context", klassbuf, TEST_CLASSES);

        // Create Context.jar w/ "file:" entry for Other.jar
        Manifest mf = new Manifest();
        Attributes attrs = mf.getMainAttributes();
        attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");

        String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())
                                                      :            OTHER_JAR_PATH.toString());
        attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);

        System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);
        JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,
                               Paths.get(TEST_CLASSES),
                               Paths.get(TEST_CLASSES, "Context.class"));

        // Use URLClassLoader w/ Context.jar to load Other.class, which will
        // load via the Class-Path entry
        URL url = CONTEXT_JAR_PATH.toUri().toURL();
        URLClassLoader ucl = new URLClassLoader(new URL[]{ url },
                                                null); // don't delegate to App CL
        Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail
        System.out.println("Loaded: " + otherClass);
    }

    /* Convert a Windows path to a unix-style path, and remove any drive letter */
    private static String toUnixPath(String orig) {
        String retVal = new File(orig).toURI().getPath();
        int colonAt = retVal.indexOf(':');

        if (colonAt != -1 && colonAt < 3) {
            retVal = retVal.substring(colonAt + 1); // Start after the drive letter
        }
        return retVal;
    }
}