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