hotspot/test/serviceability/jvmti/GetModulesInfo/JvmtiGetAllModulesTest.java
author alanb
Thu, 01 Dec 2016 08:56:41 +0000
changeset 42307 cefc81dc1d52
parent 40631 ed82623d7831
child 42630 ae91fbc4b59f
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: lfoltan, acorn, ctornqvi, mchung Contributed-by: lois.foltan@oracle.com, harold.seigel@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com, serguei.spitsyn@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
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    52
    private static native Module[] getModulesNative();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    53
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    54
    private static Set<Module> getModulesJVMTI() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    55
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    56
        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
    57
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    58
        // 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
    59
        // 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
    60
        // 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
    61
        modules.removeIf(mod -> !mod.isNamed());
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
        return modules;
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
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    66
    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
    67
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    68
        final String MY_MODULE_NAME = "myModule";
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    69
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    70
        // 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
    71
        Asserts.assertEquals(Layer.boot().modules(), getModulesJVMTI());
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    72
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    73
        // Load a new named module
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    74
        ModuleDescriptor descriptor
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    75
                = ModuleDescriptor.module(MY_MODULE_NAME).build();
40003
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    76
        ModuleFinder finder = finderOf(descriptor);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    77
        ClassLoader loader = new ClassLoader() {};
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    78
        Configuration parent = Layer.boot().configuration();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    79
        Configuration cf = parent.resolveRequires(finder, ModuleFinder.of(), Set.of(MY_MODULE_NAME));
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    80
        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
    81
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    82
        // 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
    83
        Set<Module> jvmtiModules = getModulesJVMTI();
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    84
        for (Module mod : my.modules()) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    85
            if (!jvmtiModules.contains(mod)) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    86
                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
    87
            }
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    88
        }
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
    }
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
    /**
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    93
     * 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
    94
     * descriptors.
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
    static ModuleFinder finderOf(ModuleDescriptor... descriptors) {
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
        // Create a ModuleReference for each module
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
    99
        Map<String, ModuleReference> namesToReference = new HashMap<>();
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
        for (ModuleDescriptor descriptor : descriptors) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   102
            String name = descriptor.name();
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
            URI uri = URI.create("module:/" + name);
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
            Supplier<ModuleReader> supplier = () -> {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   107
                throw new UnsupportedOperationException();
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
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   110
            ModuleReference mref = new ModuleReference(descriptor, uri, supplier);
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
            namesToReference.put(name, mref);
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
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   115
        return new ModuleFinder() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   116
            @Override
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   117
            public Optional<ModuleReference> find(String name) {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   118
                Objects.requireNonNull(name);
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   119
                return Optional.ofNullable(namesToReference.get(name));
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
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 Set<ModuleReference> findAll() {
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   124
                return new HashSet<>(namesToReference.values());
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
        };
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
13548c43ae1e 8153978: New test to verify the modules info as returned by the JVMTI
akulyakh
parents:
diff changeset
   129
}