hotspot/test/serviceability/jvmti/GetModulesInfo/JvmtiGetAllModulesTest.java
author alanb
Fri, 10 Feb 2017 09:03:55 +0000
changeset 43665 4bb003cad9b9
parent 42630 ae91fbc4b59f
child 44520 0553e129e0ec
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: lfoltan, acorn, mchung Contributed-by: alan.bateman@oracle.com, george.triantafillou@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
/*
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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.reflect.Layer;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    32
import java.lang.reflect.Module;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    33
import java.lang.module.ModuleReference;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    34
import java.lang.module.ModuleFinder;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    35
import java.lang.module.ModuleReader;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    36
import java.lang.module.ModuleDescriptor;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    37
import java.lang.module.Configuration;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    38
import java.util.Arrays;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    39
import java.util.Set;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    40
import java.util.Map;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    41
import java.util.function.Supplier;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    42
import java.util.Objects;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    43
import java.util.Optional;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    44
import java.net.URI;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    45
import java.util.HashSet;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    46
import java.util.HashMap;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    47
import java.util.stream.Collectors;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    48
import jdk.test.lib.Asserts;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    49
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    50
public class JvmtiGetAllModulesTest {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    51
42630
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    52
    static class MyModuleReference extends ModuleReference {
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    53
        public MyModuleReference(ModuleDescriptor descriptor, URI uri) {
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    54
            super(descriptor, uri);
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    55
        }
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    56
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    57
        // Trivial implementation to make the class non-abstract
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    58
        public ModuleReader open() {
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    59
            return null;
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    60
        }
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    61
    }
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
    62
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    63
    private static native Module[] getModulesNative();
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
    private static Set<Module> getModulesJVMTI() {
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
        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
    68
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    69
        // 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
    70
        // 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
    71
        // 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
    72
        modules.removeIf(mod -> !mod.isNamed());
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
        return modules;
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    75
    }
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
    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
    78
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    79
        final String MY_MODULE_NAME = "myModule";
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    80
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    81
        // Verify that JVMTI reports exactly the same info as Java regarding the named modules
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    82
        Asserts.assertEquals(Layer.boot().modules(), getModulesJVMTI());
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    83
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    84
        // Load a new named module
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42630
diff changeset
    85
        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
    86
        ModuleFinder finder = finderOf(descriptor);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    87
        ClassLoader loader = new ClassLoader() {};
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    88
        Configuration parent = Layer.boot().configuration();
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42630
diff changeset
    89
        Configuration cf = parent.resolve(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    90
        Layer my = Layer.boot().defineModules(cf, m -> loader);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    91
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    92
        // 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
    93
        Set<Module> jvmtiModules = getModulesJVMTI();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    94
        for (Module mod : my.modules()) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    95
            if (!jvmtiModules.contains(mod)) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    96
                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
    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
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   102
    /**
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   103
     * 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
   104
     * 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
    static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   107
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   108
        // Create a ModuleReference for each module
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   109
        Map<String, ModuleReference> namesToReference = new HashMap<>();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   110
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   111
        for (ModuleDescriptor descriptor : descriptors) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   112
            String name = descriptor.name();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   113
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   114
            URI uri = URI.create("module:/" + name);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   115
42630
ae91fbc4b59f 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42307
diff changeset
   116
            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
   117
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   118
            namesToReference.put(name, mref);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   119
        }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   120
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   121
        return new ModuleFinder() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   122
            @Override
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   123
            public Optional<ModuleReference> find(String name) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   124
                Objects.requireNonNull(name);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   125
                return Optional.ofNullable(namesToReference.get(name));
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   126
            }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   127
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   128
            @Override
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   129
            public Set<ModuleReference> findAll() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   130
                return new HashSet<>(namesToReference.values());
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
    }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   134
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   135
}