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