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
26 * @bug 8284161 8287008
27 * @summary Basic test for jcmd Thread.dump_to_file
28 * @modules jdk.jcmd
29 * @library /test/lib
30 * @run junit/othervm ThreadDumpToFileTest
31 */
32
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.util.stream.Stream;
37 import jdk.test.lib.dcmd.PidJcmdExecutor;
38 import jdk.test.lib.process.OutputAnalyzer;
39 import jdk.test.lib.threaddump.ThreadDump;
40
41 import org.junit.jupiter.api.Test;
42 import static org.junit.jupiter.api.Assertions.*;
43
44 class ThreadDumpToFileTest {
45
46 /**
47 * Test thread dump, should be in plain text format.
48 */
49 @Test
50 void testThreadDump() throws IOException {
51 Path file = genThreadDumpPath(".txt");
52 testPlainThreadDump(file);
53 }
54
55 /**
56 * Test thread dump in plain text format.
57 */
58 @Test
59 void testPlainThreadDump() throws IOException {
60 Path file = genThreadDumpPath(".txt");
61 testPlainThreadDump(file, "-format=plain");
62 }
63
64 /**
138 }
139
140 /**
141 * Generate a file path with the given suffix to use for the thread dump.
142 */
143 private Path genThreadDumpPath(String suffix) throws IOException {
144 Path dir = Path.of(".").toAbsolutePath();
145 Path file = Files.createTempFile(dir, "threads-", suffix);
146 Files.delete(file);
147 return file;
148 }
149
150 /**
151 * Launches jcmd Thread.dump_to_file to obtain a thread dump of this VM.
152 */
153 private OutputAnalyzer jcmdThreadDumpToFile(Path file, String... options) {
154 String cmd = "Thread.dump_to_file";
155 for (String option : options) {
156 cmd += " " + option;
157 }
158 return new PidJcmdExecutor().execute(cmd + " " + file);
159 }
160
161 /**
162 * Returns true if the given file contains a line with the string.
163 */
164 private boolean find(Path file, String text) throws IOException {
165 try (Stream<String> stream = Files.lines(file)) {
166 return stream.anyMatch(line -> line.indexOf(text) >= 0);
167 }
168 }
169 }
|
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
26 * @bug 8284161 8287008
27 * @summary Basic test for jcmd Thread.dump_to_file
28 * @modules jdk.jcmd
29 * @library /test/lib
30 * @run junit/othervm -Dminify=true ThreadDumpToFileTest
31 * @run junit/othervm -Dminify=false ThreadDumpToFileTest
32 */
33
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.util.stream.Stream;
38 import jdk.test.lib.dcmd.PidJcmdExecutor;
39 import jdk.test.lib.process.OutputAnalyzer;
40 import jdk.test.lib.threaddump.ThreadDump;
41
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.BeforeAll;
44 import static org.junit.jupiter.api.Assertions.*;
45
46 class ThreadDumpToFileTest {
47 private static boolean minify;
48
49 @BeforeAll
50 static void setup() throws Exception {
51 minify = Boolean.getBoolean("minify");
52 }
53
54 /**
55 * Test thread dump, should be in plain text format.
56 */
57 @Test
58 void testThreadDump() throws IOException {
59 Path file = genThreadDumpPath(".txt");
60 testPlainThreadDump(file);
61 }
62
63 /**
64 * Test thread dump in plain text format.
65 */
66 @Test
67 void testPlainThreadDump() throws IOException {
68 Path file = genThreadDumpPath(".txt");
69 testPlainThreadDump(file, "-format=plain");
70 }
71
72 /**
146 }
147
148 /**
149 * Generate a file path with the given suffix to use for the thread dump.
150 */
151 private Path genThreadDumpPath(String suffix) throws IOException {
152 Path dir = Path.of(".").toAbsolutePath();
153 Path file = Files.createTempFile(dir, "threads-", suffix);
154 Files.delete(file);
155 return file;
156 }
157
158 /**
159 * Launches jcmd Thread.dump_to_file to obtain a thread dump of this VM.
160 */
161 private OutputAnalyzer jcmdThreadDumpToFile(Path file, String... options) {
162 String cmd = "Thread.dump_to_file";
163 for (String option : options) {
164 cmd += " " + option;
165 }
166 if (minify) {
167 cmd += " -minify";
168 }
169 return new PidJcmdExecutor().execute(cmd + " " + file);
170 }
171
172 /**
173 * Returns true if the given file contains a line with the string.
174 */
175 private boolean find(Path file, String text) throws IOException {
176 try (Stream<String> stream = Files.lines(file)) {
177 return stream.anyMatch(line -> line.indexOf(text) >= 0);
178 }
179 }
180 }
|