1 /*
   2  * Copyright (c) 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.io.*;
  24 import jdk.test.lib.*;
  25 import jdk.test.lib.dcmd.*;
  26 import org.testng.annotations.Test;
  27 
  28 /*
  29  * Test to attach JVMTI java agent.
  30  *
  31  * @test
  32  * @bug 8147388
  33  * @library /testlibrary
  34  * @modules java.base/sun.misc
  35  *          java.compiler
  36  *          java.instrument
  37  *          java.management
  38  *          jdk.jvmstat/sun.jvmstat.monitor
  39  * @build ClassFileInstaller jdk.test.lib.* SimpleJvmtiAgent
  40  * @run main ClassFileInstaller SimpleJvmtiAgent
  41  * @run testng LoadJavaAgentDcmdTest
  42  */
  43 public class LoadJavaAgentDcmdTest {
  44     public void run(CommandExecutor executor)  {
  45         try{
  46             PrintWriter pw = new PrintWriter("MANIFEST.MF");
  47             pw.println("Agent-Class: SimpleJvmtiAgent");
  48             pw.close();
  49 
  50             ProcessBuilder pb = new ProcessBuilder();
  51             pb.command(new String[] { JDKToolFinder.getJDKTool("jar"),
  52                                       "cmf",
  53                                       "MANIFEST.MF",
  54                                       "agent.jar",
  55                                       "SimpleJvmtiAgent.class"});
  56             pb.start().waitFor();
  57 
  58             OutputAnalyzer output = executor.execute(
  59                                               "JVMTI.agent_load agent.jar");
  60             output.stderrShouldBeEmpty();
  61         } catch (Exception e) {
  62             throw new RuntimeException(e);
  63         }
  64     }
  65 
  66     @Test
  67     public void jmx() throws Throwable {
  68         run(new JMXExecutor());
  69     }
  70 
  71     @Test
  72     public void cli() throws Throwable {
  73         run(new PidJcmdExecutor());
  74     }
  75 }