jdk/test/java/lang/Class/getResource/Main.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 42338 a60f280f803c
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@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.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.net.URL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
/**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    34
 * Basic test of Class getResource and getResourceAsStream to locate/read
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    35
 * resources in named modules.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
public class Main {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
    static final String NAME = "myresource";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
    public static void main(String[] args) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    44
        // create resources in m1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    45
        createResource("m1", Paths.get("."), "m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    46
        createResource("m1", Paths.get("p1"), "m1/p1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    47
        createResource("m1", Paths.get("p1", "impl"), "m1/p1.impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    48
        createResource("m1", Paths.get("p1", "resources"), "m1/p1.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    49
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    50
        // create resources in m2
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    51
        createResource("m2", Paths.get("."), "m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    52
        createResource("m2", Paths.get("p2"), "m2/p2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    53
        createResource("m2", Paths.get("p2", "impl"), "m2/p2.impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    54
        createResource("m2", Paths.get("p2", "resources"), "m2/p2.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    55
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    56
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    57
        // invoke Class getResource from an unnamed module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    58
        URL url = Main.class.getResource("/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    59
        assertNotNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    60
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    61
        url = Main.class.getResource("/p1/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    62
        assertNull(url);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    64
        url = Main.class.getResource("/p1/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    65
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    66
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    67
        url = Main.class.getResource("/p1/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    68
        assertEquals(readAllAsString(url), "m1/p1.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    69
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    70
        url = Main.class.getResource("/p2/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    71
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    72
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    73
        url = Main.class.getResource("/p2/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    74
        assertNull(url);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    76
        // m1: exports private p1.resources
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    77
        url = Main.class.getResource("/p2/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    78
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    79
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    80
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    81
        // invoke Class getResource from module m1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    82
        url = p1.Main.getResource("/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    83
        assertEquals(readAllAsString(url), "m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    84
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    85
        url = p1.Main.getResource("/p1/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    86
        assertEquals(readAllAsString(url), "m1/p1");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    88
        url = p1.Main.getResource("/p1/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    89
        assertEquals(readAllAsString(url), "m1/p1.impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    90
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    91
        url = p1.Main.getResource("/p1/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    92
        assertEquals(readAllAsString(url), "m1/p1.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    93
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    94
        // m2: not a named package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    95
        url = p1.Main.getResource(p2.Main.class, "/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    96
        assertEquals(readAllAsString(url), "m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    97
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    98
        url = p1.Main.getResource(p2.Main.class, "/p2/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
    99
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   100
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   101
        // m2: exports p2.impl to m1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   102
        url = p1.Main.getResource(p2.Main.class, "/p2/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   103
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   104
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   105
        // m2: exports private p2.resources to m1;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   106
        url = p1.Main.getResource(p2.Main.class, "/p2/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   107
        assertEquals(readAllAsString(url), "m2/p2.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   108
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   110
        // invoke Class getResource from module m2
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   111
        url = p2.Main.getResource("/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   112
        assertEquals(readAllAsString(url), "m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   113
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   114
        url = p2.Main.getResource("/p2/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   115
        assertEquals(readAllAsString(url), "m2/p2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   116
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   117
        url = p2.Main.getResource("/p2/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   118
        assertEquals(readAllAsString(url), "m2/p2.impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   119
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   120
        url = p2.Main.getResource("/p2/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   121
        assertEquals(readAllAsString(url), "m2/p2.resources");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   123
        // m1: not a named package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   124
        url = p2.Main.getResource(p1.Main.class, "/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   125
        assertEquals(readAllAsString(url), "m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   126
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   127
        url = p2.Main.getResource(p1.Main.class, "/p1/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   128
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   129
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   130
        url = p2.Main.getResource(p1.Main.class, "/p1/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   131
        assertNull(url);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   132
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   133
        // m1: exports private p1.resources;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   134
        url = p2.Main.getResource(p1.Main.class, "/p1/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   135
        assertEquals(readAllAsString(url), "m1/p1.resources");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   137
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   138
        // invoke Class getResourceAsStream from an unnamed module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   139
        InputStream in = Main.class.getResourceAsStream("/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   140
        assertNotNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   141
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   142
        in = Main.class.getResourceAsStream("/p1/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   143
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   144
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   145
        in = Main.class.getResourceAsStream("/p1/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   146
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   147
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   148
        in = Main.class.getResourceAsStream("/p1/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   149
        assertEquals(readAllAsString(in), "m1/p1.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   150
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   151
        in = Main.class.getResourceAsStream("/p2/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   152
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   153
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   154
        in = Main.class.getResourceAsStream("/p2/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   155
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   156
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   157
        in = Main.class.getResourceAsStream("/p2/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   158
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   159
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   160
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   161
        // invoke Class getResourceAsStream from module m1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   162
        in = p1.Main.getResourceAsStream("/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   163
        assertEquals(readAllAsString(in), "m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   164
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   165
        in = p1.Main.getResourceAsStream("/p1/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   166
        assertEquals(readAllAsString(in), "m1/p1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   167
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   168
        in = p1.Main.getResourceAsStream("/p1/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   169
        assertEquals(readAllAsString(in), "m1/p1.impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   170
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   171
        in = p1.Main.getResourceAsStream("/p1/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   172
        assertEquals(readAllAsString(in), "m1/p1.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   173
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   174
        in = p1.Main.getResourceAsStream(p2.Main.class, "/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   175
        assertEquals(readAllAsString(in), "m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   176
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   177
        in = p1.Main.getResourceAsStream(p2.Main.class, "/p2/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   178
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   179
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   180
        in = p1.Main.getResourceAsStream(p2.Main.class, "/p2/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   181
        assertNull(in);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   183
        in = p1.Main.getResourceAsStream(p2.Main.class, "/p2/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   184
        assertEquals(readAllAsString(in), "m2/p2.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   185
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   186
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   187
        // invoke Class getResourceAsStream from module m2
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   188
        in = p2.Main.getResourceAsStream("/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   189
        assertEquals(readAllAsString(in), "m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   190
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   191
        in = p2.Main.getResourceAsStream("/p2/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   192
        assertEquals(readAllAsString(in), "m2/p2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   193
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   194
        in = p2.Main.getResourceAsStream("/p2/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   195
        assertEquals(readAllAsString(in), "m2/p2.impl");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   196
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   197
        in = p2.Main.getResourceAsStream("/p2/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   198
        assertEquals(readAllAsString(in), "m2/p2.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   199
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   200
        in = p2.Main.getResourceAsStream(p1.Main.class, "/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   201
        assertEquals(readAllAsString(in), "m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   202
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   203
        in = p2.Main.getResourceAsStream(p1.Main.class, "/p1/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   204
        assertNull(in);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   206
        in = p2.Main.getResourceAsStream(p1.Main.class, "/p1/impl/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   207
        assertNull(in);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   208
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   209
        in = p2.Main.getResourceAsStream(p1.Main.class, "/p1/resources/" + NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   210
        assertEquals(readAllAsString(in), "m1/p1.resources");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   211
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   213
        // Nulls
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   214
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   215
            Main.class.getResource(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   216
            assertTrue(false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   217
        } catch (NullPointerException expected) { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   218
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   219
            Main.class.getResourceAsStream(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   220
            assertTrue(false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   221
        } catch (NullPointerException expected) { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   222
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   223
            p1.Main.class.getResource(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   224
            assertTrue(false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   225
        } catch (NullPointerException expected) { }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   226
        try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   227
            p1.Main.class.getResourceAsStream(null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   228
            assertTrue(false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   229
        } catch (NullPointerException expected) { }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        // SecurityManager case
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        System.setSecurityManager(new SecurityManager());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        assertNull(Main.class.getResource("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
        assertNull(p1.Main.getResource("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        assertNull(p2.Main.getResource("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        assertNull(Main.class.getResourceAsStream("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        assertNull(p1.Main.getResourceAsStream("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
        assertNull(p2.Main.getResourceAsStream("/" + NAME));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        System.out.println("Success!");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   246
     * Create a resource in the sub-directory of the given exploded module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   247
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   248
    static void createResource(String mn, Path subdir, String msg) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   249
        Path dir = directoryFor(mn).resolve(subdir);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   250
        Path file = dir.resolve(NAME);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   251
        Files.write(file, msg.getBytes("UTF-8"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   252
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   253
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   254
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
     * Returns the directory for the given module (by name).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
    static Path directoryFor(String name) {
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   258
        Configuration cf = ModuleLayer.boot().configuration();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        ResolvedModule resolvedModule = cf.findModule(name).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        if (resolvedModule == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            throw new RuntimeException("not found: " + name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        Path dir = Paths.get(resolvedModule.reference().location().get());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        if (!Files.isDirectory(dir))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
            throw new RuntimeException("not a directory: " + dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
        return dir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   268
    static String readAllAsString(InputStream in) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   269
        if (in == null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   270
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   271
        try (in) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   272
            return new String(in.readAllBytes(), "UTF-8");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   276
    static String readAllAsString(URL url) throws IOException {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   277
        if (url == null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   278
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   279
        InputStream in = url.openStream();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   280
        return readAllAsString(url.openStream());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   281
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   282
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
    static void assertTrue(boolean condition) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
        if (!condition) throw new RuntimeException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
    static void assertNull(Object o) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        assertTrue(o == null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    static void assertNotNull(Object o) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        assertTrue(o != null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
    static void assertEquals(Object actual, Object expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        if (expected == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            assertNull(actual);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            assertTrue(expected.equals(actual));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
    static void assertNotEquals(Object actual, Object expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        if (expected == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            assertNotNull(actual);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            assertTrue(!expected.equals(actual));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311