test/jdk/jdk/jfr/event/runtime/TestModuleEvents.java
changeset 50113 caf115bb98ad
child 50455 2b73cce96dce
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     1 /*
       
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.jfr.event.runtime;
       
    26 
       
    27 import static jdk.test.lib.Asserts.assertEquals;
       
    28 
       
    29 import java.util.HashMap;
       
    30 import java.util.List;
       
    31 import java.util.Map;
       
    32 
       
    33 import jdk.jfr.Recording;
       
    34 import jdk.jfr.consumer.RecordedEvent;
       
    35 import jdk.jfr.consumer.RecordedObject;
       
    36 import jdk.test.lib.jfr.EventNames;
       
    37 import jdk.test.lib.jfr.Events;
       
    38 
       
    39 /*
       
    40  * @test
       
    41  * @summary Tests the JFR events related to modules
       
    42  * @key jfr
       
    43  * @library /test/lib
       
    44  * @run main/othervm --limit-modules java.base,jdk.jfr jdk.jfr.event.runtime.TestModuleEvents
       
    45  */
       
    46 public final class TestModuleEvents {
       
    47 
       
    48     private static final String MODULE_EXPORT_EVENT_NAME = EventNames.ModuleExport;
       
    49     private static final String MODULE_REQUIRE_EVENT_NAME = EventNames.ModuleRequire;
       
    50     private static final String UNNAMED = "<unnamed>";
       
    51 
       
    52     public static void main(String[] args) throws Throwable {
       
    53         verifyRequiredModules();
       
    54         verifyExportedModules();
       
    55     }
       
    56 
       
    57     private static void verifyRequiredModules() throws Throwable {
       
    58         Recording recording = new Recording();
       
    59         recording.enable(MODULE_REQUIRE_EVENT_NAME);
       
    60 
       
    61         recording.start();
       
    62         recording.stop();
       
    63 
       
    64         List<RecordedEvent> events = Events.fromRecording(recording);
       
    65         assertDependency(events, "jdk.jfr", "java.base"); // jdk.jfr requires java.base (by edfault)
       
    66         assertDependency(events, "java.base", "jdk.jfr"); // java.base require jdk.jfr for JDK events, i.e. FileRead
       
    67 
       
    68         recording.close();
       
    69     }
       
    70 
       
    71     private static void assertDependency(List<RecordedEvent> events, String source, String required) throws Exception {
       
    72         for (RecordedEvent e : events) {
       
    73             String sourceModule = e.getValue("source.name");
       
    74             if (source.equals(sourceModule)) {
       
    75                 RecordedObject module = e.getValue("requiredModule");
       
    76                 if (module != null) {
       
    77                     if (required.equals(module.getValue("name"))) {
       
    78                         return;
       
    79                     }
       
    80                 }
       
    81             }
       
    82         }
       
    83         throw new Exception("Could not find module dependency between " + source + " and requires modeule "+ required);
       
    84     }
       
    85 
       
    86     private static void verifyExportedModules() throws Throwable {
       
    87         Recording recording = new Recording();
       
    88         recording.enable(MODULE_EXPORT_EVENT_NAME);
       
    89         recording.start();
       
    90         recording.stop();
       
    91 
       
    92         Map<String, String> edges = new HashMap<>();
       
    93 
       
    94         List<RecordedEvent> events = Events.fromRecording(recording);
       
    95         events.stream().forEach((ev) -> {
       
    96             String exportedPackage = getValue(ev.getValue("exportedPackage"), "name", UNNAMED);
       
    97             String toModule = getValue(ev.getValue("targetModule"), "name", UNNAMED);
       
    98 
       
    99             edges.put(exportedPackage, toModule);
       
   100         });
       
   101 
       
   102         // We expect
       
   103         // 1) jdk.jfr -> <unnamed> (because we use the package)
       
   104         // 2) java.util -> <unnamed> (because we use the package)
       
   105         // 3) jdk.jfr.events -> java.base (from the jfr design)
       
   106         // 4) jdk.internal -> jdk.jfr (from the jfr design)
       
   107         // Where 'a -> b' means "package 'a' exported to module 'b'"
       
   108         assertEquals(edges.get("jdk/jfr"), UNNAMED);
       
   109         assertEquals(edges.get("java/util"), UNNAMED);
       
   110         assertEquals(edges.get("jdk/jfr/events"), "java.base");
       
   111         assertEquals(edges.get("jdk/internal"), "jdk.jfr");
       
   112 
       
   113         recording.close();
       
   114     }
       
   115 
       
   116     // Helper function to get field from a RecordedObject
       
   117     private static String getValue(RecordedObject ro, String field, String defVal) {
       
   118         if (ro != null && ro.getValue(field) != null) {
       
   119             return ro.getValue(field);
       
   120         } else {
       
   121             return defVal;
       
   122         }
       
   123     }
       
   124 }