hotspot/test/serviceability/jvmti/GetModulesInfo/JvmtiGetAllModulesTest.java
author alanb
Fri, 07 Apr 2017 08:04:46 +0000
changeset 44520 0553e129e0ec
parent 43665 4bb003cad9b9
child 46480 77e911558d21
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: lfoltan, sspitsyn Contributed-by: alan.bateman@oracle.com, harold.seigel@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     1
/*
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     4
 *
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     7
 * published by the Free Software Foundation.
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     8
 *
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    13
 * accompanied this code).
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    14
 *
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    18
 *
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    21
 * questions.
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    22
 */
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    23
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    24
/**
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    25
 * @test
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    26
 * @summary Verifies the JVMTI GetAllModules API
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40003
diff changeset
    27
 * @library /test/lib
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    28
 * @run main/othervm -agentlib:JvmtiGetAllModulesTest JvmtiGetAllModulesTest
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    29
 *
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    30
 */
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    31
import java.lang.module.ModuleReference;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    32
import java.lang.module.ModuleFinder;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    33
import java.lang.module.ModuleReader;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    34
import java.lang.module.ModuleDescriptor;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    35
import java.lang.module.Configuration;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    36
import java.util.Arrays;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    37
import java.util.Set;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    38
import java.util.Map;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    39
import java.util.function.Supplier;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    40
import java.util.Objects;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    41
import java.util.Optional;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    42
import java.net.URI;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    43
import java.util.HashSet;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    44
import java.util.HashMap;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    45
import java.util.stream.Collectors;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    46
import jdk.test.lib.Asserts;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    47
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    48
public class JvmtiGetAllModulesTest {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    49
42630
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    50
    static class MyModuleReference extends ModuleReference {
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    51
        public MyModuleReference(ModuleDescriptor descriptor, URI uri) {
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    52
            super(descriptor, uri);
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    53
        }
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    54
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    55
        // Trivial implementation to make the class non-abstract
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    56
        public ModuleReader open() {
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    57
            return null;
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    58
        }
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    59
    }
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    60
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    61
    private static native Module[] getModulesNative();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    62
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    63
    private static Set<Module> getModulesJVMTI() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    64
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    65
        Set<Module> modules = Arrays.stream(getModulesNative()).collect(Collectors.toSet());
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    66
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    67
        // JVMTI reports unnamed modules, Java API does not
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    68
        // remove the unnamed modules here, so the resulting report can be expected
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    69
        // to be equal to what Java reports
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    70
        modules.removeIf(mod -> !mod.isNamed());
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    71
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    72
        return modules;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    73
    }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    74
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    75
    public static void main(String[] args) throws Exception {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    76
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    77
        final String MY_MODULE_NAME = "myModule";
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    78
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    79
        // Verify that JVMTI reports exactly the same info as Java regarding the named modules
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
    80
        Asserts.assertEquals(ModuleLayer.boot().modules(), getModulesJVMTI());
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    81
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    82
        // Load a new named module
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42630
diff changeset
    83
        ModuleDescriptor descriptor = ModuleDescriptor.newModule(MY_MODULE_NAME).build();
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    84
        ModuleFinder finder = finderOf(descriptor);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    85
        ClassLoader loader = new ClassLoader() {};
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
    86
        Configuration parent = ModuleLayer.boot().configuration();
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42630
diff changeset
    87
        Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
    88
        ModuleLayer my = ModuleLayer.boot().defineModules(cf, m -> loader);
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    89
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    90
        // Verify that the loaded module is indeed reported by JVMTI
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    91
        Set<Module> jvmtiModules = getModulesJVMTI();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    92
        for (Module mod : my.modules()) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    93
            if (!jvmtiModules.contains(mod)) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    94
                throw new RuntimeException("JVMTI did not report the loaded named module: " + mod.getName());
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    95
            }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    96
        }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    97
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    98
    }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    99
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   100
    /**
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   101
     * Returns a ModuleFinder that finds modules with the given module
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   102
     * descriptors.
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   103
     */
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   104
    static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   105
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   106
        // Create a ModuleReference for each module
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   107
        Map<String, ModuleReference> namesToReference = new HashMap<>();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   108
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   109
        for (ModuleDescriptor descriptor : descriptors) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   110
            String name = descriptor.name();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   111
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   112
            URI uri = URI.create("module:/" + name);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   113
42630
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
   114
            ModuleReference mref = new MyModuleReference(descriptor, uri);
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   115
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   116
            namesToReference.put(name, mref);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   117
        }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   118
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   119
        return new ModuleFinder() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   120
            @Override
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   121
            public Optional<ModuleReference> find(String name) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   122
                Objects.requireNonNull(name);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   123
                return Optional.ofNullable(namesToReference.get(name));
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   124
            }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   125
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   126
            @Override
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   127
            public Set<ModuleReference> findAll() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   128
                return new HashSet<>(namesToReference.values());
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   129
            }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   130
        };
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   131
    }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   132
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   133
}