jdk/test/java/lang/ClassLoader/getResource/modules/Main.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
child 42338 a60f280f803c
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.lang.module.ResolvedModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * Basic test of ClassLoader getResource and getResourceAsStream when
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 * invoked from code in named modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
public class Main {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
    static final String NAME = "myresource";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
    public static void main(String[] args) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        // create m1/myresource containing "m1"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        Path file = directoryFor("m1").resolve(NAME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        Files.write(file, "m1".getBytes("UTF-8"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        // create m2/myresource containing "m2"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        file = directoryFor("m2").resolve(NAME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        Files.write(file, "m2".getBytes("UTF-8"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        // check that m3/myresource does not exist
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        assertTrue(Files.notExists(directoryFor("m3").resolve(NAME)));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        // invoke ClassLoader getResource from the unnamed module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        assertNull(Main.class.getClassLoader().getResource("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        // invoke ClassLoader getResource from modules m1-m3
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        // Resources in a named module are private to that module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        // ClassLoader.getResource should not find resource in named modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        assertNull(p3.Main.getResourceInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        // invoke ClassLoader getResourceAsStream from the unnamed module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        // invoke ClassLoader getResourceAsStream from modules m1-m3
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        // Resources in a named module are private to that module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        // ClassLoader.getResourceAsStream should not find resource in named modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        assertNull(p3.Main.getResourceAsStreamInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        // SecurityManager case
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        System.setSecurityManager(new SecurityManager());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        assertNull(Main.class.getClassLoader().getResource("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        assertNull(p3.Main.getResourceInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        assertNull(p3.Main.getResourceAsStreamInClassLoader("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        System.out.println("Success!");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
     * Returns the directory for the given module (by name).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    static Path directoryFor(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        Configuration cf = Layer.boot().configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        ResolvedModule resolvedModule = cf.findModule(mn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        if (resolvedModule == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            throw new RuntimeException("not found: " + mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        Path dir = Paths.get(resolvedModule.reference().location().get());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        if (!Files.isDirectory(dir))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            throw new RuntimeException("not a directory: " + dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        return dir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    static void assertTrue(boolean condition) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        if (!condition) throw new RuntimeException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    static void assertNull(Object o) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        assertTrue(o == null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112