test/jdk/jdk/jfr/api/consumer/streaming/TestRepositoryProperty.java
changeset 58863 c16ac7a2eba4
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     1 /*
       
     2  * Copyright (c) 2019, 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 
       
    26 package jdk.jfr.api.consumer.streaming;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.util.Properties;
       
    32 
       
    33 import com.sun.tools.attach.AttachNotSupportedException;
       
    34 import com.sun.tools.attach.VirtualMachine;
       
    35 
       
    36 import jdk.jfr.Recording;
       
    37 import jdk.test.lib.dcmd.CommandExecutor;
       
    38 import jdk.test.lib.dcmd.PidJcmdExecutor;
       
    39 
       
    40 /**
       
    41  * @test
       
    42  * @summary Verifies that it is possible to access JFR repository from a system
       
    43  *          property
       
    44  * @key jfr
       
    45  * @requires vm.hasJFR
       
    46  * @library /test/lib
       
    47  * @modules jdk.attach
       
    48  *          jdk.jfr
       
    49  * @run main/othervm -Djdk.attach.allowAttachSelf=true jdk.jfr.api.consumer.streaming.TestRepositoryProperty
       
    50  */
       
    51 public class TestRepositoryProperty {
       
    52 
       
    53     private final static String JFR_REPOSITORY_LOCATION_PROPERTY = "jdk.jfr.repository";
       
    54 
       
    55     public static void main(String... args) throws Exception {
       
    56         testBeforeInitialization();
       
    57         testAfterInitialization();
       
    58         testFromAgent();
       
    59         testAfterChange();
       
    60     }
       
    61 
       
    62     private static void testFromAgent() throws AttachNotSupportedException, IOException {
       
    63         String pidText = String.valueOf(ProcessHandle.current().pid());
       
    64         VirtualMachine vm = VirtualMachine.attach(pidText);
       
    65         Properties p = vm.getSystemProperties();
       
    66         String location = (String) p.get(JFR_REPOSITORY_LOCATION_PROPERTY);
       
    67         if (location == null) {
       
    68             throw new AssertionError("Could not find repository path in agent properties");
       
    69         }
       
    70         Path path = Path.of(location);
       
    71         if (!Files.isDirectory(path)) {
       
    72             throw new AssertionError("Repository path doesn't point to directory");
       
    73         }
       
    74     }
       
    75 
       
    76     private static void testAfterChange() {
       
    77         Path newRepository = Path.of(".").toAbsolutePath();
       
    78 
       
    79         String cmd = "JFR.configure repository=" +  newRepository.toString();
       
    80         CommandExecutor executor = new PidJcmdExecutor();
       
    81         executor.execute(cmd);
       
    82         String location = System.getProperty(JFR_REPOSITORY_LOCATION_PROPERTY);
       
    83         if (newRepository.toString().equals(location)) {
       
    84             throw new AssertionError("Repository path not updated after it has been changed");
       
    85         }
       
    86     }
       
    87 
       
    88     private static void testAfterInitialization() {
       
    89         try (Recording r = new Recording()) {
       
    90             r.start();
       
    91             String location = System.getProperty(JFR_REPOSITORY_LOCATION_PROPERTY);
       
    92             if (location == null) {
       
    93                 throw new AssertionError("Repository path should exist after JFR is initialized");
       
    94             }
       
    95             System.out.println("repository=" + location);
       
    96             Path p = Path.of(location);
       
    97             if (!Files.isDirectory(p)) {
       
    98                 throw new AssertionError("Repository path doesn't point to directory");
       
    99             }
       
   100         }
       
   101 
       
   102     }
       
   103 
       
   104     private static void testBeforeInitialization() {
       
   105         String location = System.getProperty(JFR_REPOSITORY_LOCATION_PROPERTY);
       
   106         if (location != null) {
       
   107             throw new AssertionError("Repository path should not exist before JFR is initialized");
       
   108         }
       
   109     }
       
   110 }