test/hotspot/jtreg/serviceability/dcmd/jvmti/LoadAgentDcmdTest.java
changeset 48698 aca813e53416
parent 47902 aed3a9c1abfe
child 48835 62004f705d27
equal deleted inserted replaced
48697:0474300affbd 48698:aca813e53416
     1 /*
     1 /*
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 import java.io.*;
    23 import java.io.*;
    24 import java.nio.file.*;
    24 import java.nio.file.*;
       
    25 import java.util.Arrays;
    25 import java.util.jar.Attributes;
    26 import java.util.jar.Attributes;
    26 import java.util.jar.JarEntry;
    27 import java.util.jar.JarEntry;
    27 import java.util.jar.JarOutputStream;
    28 import java.util.jar.JarOutputStream;
    28 import java.util.jar.Manifest;
    29 import java.util.jar.Manifest;
       
    30 import java.util.List;
       
    31 import java.util.regex.Matcher;
       
    32 import java.util.regex.Pattern;
    29 import jdk.test.lib.Platform;
    33 import jdk.test.lib.Platform;
    30 import jdk.test.lib.process.OutputAnalyzer;
    34 import jdk.test.lib.process.OutputAnalyzer;
    31 import jdk.test.lib.dcmd.*;
    35 import jdk.test.lib.dcmd.*;
    32 import org.testng.annotations.Test;
    36 import org.testng.annotations.Test;
    33 
    37 
    95         } finally {
    99         } finally {
    96             target.close();
   100             target.close();
    97         }
   101         }
    98     }
   102     }
    99 
   103 
       
   104     static void checkWarningsOnly(OutputAnalyzer out) {
       
   105         // stderr should be empty except for VM warnings.
       
   106         if (!out.getStderr().isEmpty()) {
       
   107             List<String> lines = Arrays.asList(out.getStderr().split("(\\r\\n|\\n|\\r)"));
       
   108             Pattern p = Pattern.compile(".*VM warning.*");
       
   109             for (String line : lines) {
       
   110                 Matcher m = p.matcher(line);
       
   111                 if (!m.matches()) {
       
   112                     throw new RuntimeException("Stderr has output other than VM warnings");
       
   113                 }
       
   114             }
       
   115         }
       
   116     }
       
   117 
   100     public void run(CommandExecutor executor)  {
   118     public void run(CommandExecutor executor)  {
   101         try{
   119         try{
   102 
   120 
   103             createJarFileForAgent();
   121             createJarFileForAgent();
   104 
   122 
   106             OutputAnalyzer output = null;
   124             OutputAnalyzer output = null;
   107 
   125 
   108             // Test 1: Native agent, no arguments
   126             // Test 1: Native agent, no arguments
   109             output = executor.execute("JVMTI.agent_load " +
   127             output = executor.execute("JVMTI.agent_load " +
   110                                           libpath + " agent.jar");
   128                                           libpath + " agent.jar");
   111             output.stderrShouldBeEmpty();
   129             checkWarningsOnly(output);
   112 
   130 
   113             // Test 2: Native agent, with arguments
   131             // Test 2: Native agent, with arguments
   114             output = executor.execute("JVMTI.agent_load " +
   132             output = executor.execute("JVMTI.agent_load " +
   115                                           libpath + " \"agent.jar=foo=bar\"");
   133                                           libpath + " \"agent.jar=foo=bar\"");
   116             output.stderrShouldBeEmpty();
   134             checkWarningsOnly(output);
   117 
   135 
   118             // Test 3: Java agent, no arguments
   136             // Test 3: Java agent, no arguments
   119             output = executor.execute("JVMTI.agent_load " +
   137             output = executor.execute("JVMTI.agent_load " +
   120                                           "agent.jar");
   138                                           "agent.jar");
   121             output.stderrShouldBeEmpty();
   139             checkWarningsOnly(output);
   122 
   140 
   123             // Test 4: Java agent, with arguments
   141             // Test 4: Java agent, with arguments
   124             output = executor.execute("JVMTI.agent_load " +
   142             output = executor.execute("JVMTI.agent_load " +
   125                                           "\"agent.jar=foo=bar\"");
   143                                           "\"agent.jar=foo=bar\"");
   126             output.stderrShouldBeEmpty();
   144             checkWarningsOnly(output);
   127 
   145 
   128         } catch (Exception e) {
   146         } catch (Exception e) {
   129             throw new RuntimeException(e);
   147             throw new RuntimeException(e);
   130         }
   148         }
   131     }
   149     }