1 /*
   2  * Copyright (c) 2015, 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 
  24 /*
  25  * @test CompilerDirectivesDCMDTest
  26  * @bug 8046155
  27  * @library /testlibrary
  28  * @modules java.base/sun.misc
  29  *          java.compiler
  30  *          java.management
  31  * @build jdk.test.lib.*
  32  * @build jdk.test.lib.dcmd.*
  33  * @run testng/othervm CompilerDirectivesDCMDTest
  34  * @summary Test of diagnostic command
  35  */
  36 
  37  // testng/othervm -XX:CompileCommand=break,*.* CompilerDirectivesDCMDTest
  38  // testng/othervm -Xint CompilerDirectivesDCMDTest
  39  // testng/othervm -Xcomp CompilerDirectivesDCMDTest
  40 
  41 
  42 import jdk.test.lib.OutputAnalyzer;
  43 import jdk.test.lib.dcmd.CommandExecutor;
  44 import jdk.test.lib.dcmd.JMXExecutor;
  45 import org.testng.annotations.Test;
  46 import org.testng.Assert;
  47 
  48 import java.io.BufferedReader;
  49 import java.io.File;
  50 import java.io.StringReader;
  51 
  52 public class CompilerDirectivesDCMDTest {
  53 
  54         public static String filename;
  55 
  56         public void run(CommandExecutor executor) {
  57 
  58                 filename = System.getProperty("test.src", ".") + File.separator + "control1.txt";
  59         testPrintCommand(executor);
  60         testAddAndRemoveCommand(executor);
  61     }
  62 
  63         public static void testPrintCommand(CommandExecutor executor) {
  64 
  65                 // Get output from dcmd (diagnostic command)
  66                 OutputAnalyzer output = executor.execute("Compiler.directives_print");
  67                 int count = find(output, "Directive:");
  68         if (count < 1) {
  69                 Assert.fail("Expected at least one directive - found " + count);
  70         }
  71 
  72                 /*
  73          * CMH add some simple sanity test. Mostly make sure we can print without crashing
  74         } */
  75 
  76         }
  77 
  78     public static void testAddAndRemoveCommand(CommandExecutor executor) {
  79         OutputAnalyzer output;
  80         int count = 0;
  81 
  82         // Start with clearing stack - expect only default directive left
  83         output = executor.execute("Compiler.directives_clear");
  84         output = executor.execute("Compiler.directives_print");
  85         count = find(output, "Directive:");
  86         if (count != 1) {
  87                 Assert.fail("Expected one directives - found " + count);
  88         }
  89 
  90         // Test that we can not remove the default directive
  91         output = executor.execute("Compiler.directives_remove");
  92         output = executor.execute("Compiler.directives_print");
  93         count = find(output, "Directive:");
  94         if (count != 1) {
  95                 Assert.fail("Expected one directives - found " + count);
  96         }
  97 
  98         // Test adding some directives from file
  99         output = executor.execute("Compiler.directives_add " + filename);
 100         output = executor.execute("Compiler.directives_print");
 101         count = find(output, "Directive:");
 102         if (count != 3) {
 103                 Assert.fail("Expected three directives - found " + count);
 104         }
 105 
 106         // Test remove one directive
 107         output = executor.execute("Compiler.directives_remove");
 108         output = executor.execute("Compiler.directives_print");
 109         count = find(output, "Directive:");
 110         if (count != 2) {
 111                 Assert.fail("Expected two directives - found " + count);
 112         }
 113 
 114         // Test adding directives again
 115         output = executor.execute("Compiler.directives_add " + filename);
 116         output = executor.execute("Compiler.directives_print");
 117         count = find(output, "Directive:");
 118         if (count != 4) {
 119                 Assert.fail("Expected four directives - found " + count);
 120         }
 121 
 122         // Test clearing
 123         output = executor.execute("Compiler.directives_clear");
 124         output = executor.execute("Compiler.directives_print");
 125         count = find(output, "Directive:");
 126         if (count != 1) {
 127                 Assert.fail("Expected one directives - found " + count);
 128         }
 129 
 130         // Test clear when already cleared
 131         output = executor.execute("Compiler.directives_clear");
 132         output = executor.execute("Compiler.directives_print");
 133         count = find(output, "Directive:");
 134         if (count != 1) {
 135                 Assert.fail("Expected one directives - found " + count);
 136         }
 137         }
 138 
 139     public static int find(OutputAnalyzer output, String find) {
 140         int count = 0;
 141 
 142         for (String line : output.asLines()) {
 143                 if (line.startsWith(find)) {
 144                 count++;
 145             }
 146         }
 147         return count;
 148     }
 149 
 150     @Test
 151     public void jmx() {
 152         run(new JMXExecutor());
 153     }
 154 }