Path: blob/master/test/langtools/jdk/javadoc/tool/exceptionHandling/TestExceptionHandling.java
40971 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 815110226* @summary verify that option --dump-on-error functions correctly27* @library /tools/lib28* @modules29* jdk.javadoc/jdk.javadoc.internal.api30* jdk.javadoc/jdk.javadoc.internal.tool31* jdk.compiler/com.sun.tools.javac.api32* jdk.compiler/com.sun.tools.javac.main33* @build toolbox.ToolBox toolbox.TestRunner toolbox.JavadocTask toolbox.Task34* @run main TestExceptionHandling35*/3637import java.io.IOException;38import java.io.PrintStream;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.util.List;4243import toolbox.JavadocTask;44import toolbox.Task;45import toolbox.TestRunner;46import toolbox.ToolBox;4748/**49* This class tests if stack traces printed when50* --dump-on-error. The standard doclet is used,51* to test the doclet as well as the tool.52*/53public class TestExceptionHandling extends TestRunner {5455final ToolBox tb;56final Path testSrcFile;57final PrintStream ostream;58final JavadocTask cmdTask;59final JavadocTask apiTask;6061public static void main(String... args) throws Exception {62TestExceptionHandling tester = new TestExceptionHandling();63tester.runTests();64}6566TestExceptionHandling() throws IOException {67super(System.err);68tb = new ToolBox();69ostream = System.err;70testSrcFile = Paths.get("A.java").toAbsolutePath();71tb.writeFile(testSrcFile, "public class A { }");72cmdTask = new JavadocTask(tb, Task.Mode.CMDLINE);73apiTask = new JavadocTask(tb, Task.Mode.API);74}7576@Test77public void testDocletTrace() throws Exception {78Path out = Paths.get("out");79// create a file with the same name as the output80out.toFile().createNewFile();81cmdTask.outdir(out);82cmdTask.options("--dump-on-error");83cmdTask.files(testSrcFile);84Task.Result tr = cmdTask.run(Task.Expect.FAIL);8586String errString = "Destination directory is not a directory: " + out.toString();87// check the regular message88assertPresent("error: " + errString, tr.getOutputLines(Task.OutputKind.DIRECT));89// check that first line of the stack trace is present90assertPresent("jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException: " +91errString, tr.getOutputLines(Task.OutputKind.STDERR));9293}9495@Test96public void testToolTrace() throws Exception {97Path out = Paths.get("out.dir");98cmdTask.options("--dump-on-error", "-doclet", "NonExistentDoclet");99cmdTask.outdir(out);100cmdTask.files(testSrcFile);101Task.Result tr = cmdTask.run(Task.Expect.FAIL);102103// check the regular message104assertPresent("error: Cannot find doclet class NonExistentDoclet",105tr.getOutputLines(Task.OutputKind.DIRECT));106107// check that first line of the stack trace is present108assertPresent("java.lang.ClassNotFoundException: NonExistentDoclet",109tr.getOutputLines(Task.OutputKind.STDERR));110111}112113@Test114public void testApiModeMissingDoclet() throws Exception {115apiTask.options("-doclet", "MissingDoclet");116try {117Task.Result result = apiTask.run(Task.Expect.FAIL);118} catch (IllegalArgumentException iae) {119// ok got the right exception120return;121}122throw new Exception("expected exception/error not found");123}124125@Test126public void testApiModeMultipleDoclets() throws Exception {127apiTask.options("-doclet", "MissingDoclet",128"-doclet", "SomeDoclet");129try {130Task.Result result = apiTask.run(Task.Expect.FAIL);131} catch (IllegalArgumentException iae) {132// ok got the right exception133return;134}135throw new Exception("expected exception/error not found");136}137138void assertPresent(String regex, List<String> output) throws Exception {139List<String> gresult = tb.grep(regex, output);140if (gresult.isEmpty()) {141ostream.println("Expected: " + regex);142ostream.println("Output: ");143output.forEach(s -> {144ostream.println(s);145});146throw new Exception("Test fails expected output not found: " + regex);147}148}149}150151152