1/* 2 * Copyright (c) 2016, 2017, 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 26 * @bug 8151102 27 * @summary verify that option --dump-on-error functions correctly 28 * @library /tools/lib 29 * @modules 30 * jdk.javadoc/jdk.javadoc.internal.api 31 * jdk.javadoc/jdk.javadoc.internal.tool 32 * jdk.compiler/com.sun.tools.javac.api 33 * jdk.compiler/com.sun.tools.javac.main 34 * @build toolbox.ToolBox toolbox.TestRunner toolbox.JavadocTask toolbox.Task 35 * @run main TestExceptionHandling 36 */ 37 38import java.io.IOException; 39import java.io.PrintStream; 40import java.nio.file.Path; 41import java.nio.file.Paths; 42import java.util.List; 43 44import toolbox.JavadocTask; 45import toolbox.Task; 46import toolbox.TestRunner; 47import toolbox.ToolBox; 48 49/** 50 * This class tests if stack traces printed when 51 * --dump-on-error. The standard doclet is used, 52 * to test the doclet as well as the tool. 53 */ 54public class TestExceptionHandling extends TestRunner { 55 56 final ToolBox tb; 57 final Path testSrcFile; 58 final PrintStream ostream; 59 final JavadocTask cmdTask; 60 final JavadocTask apiTask; 61 62 public static void main(String... args) throws Exception { 63 TestExceptionHandling tester = new TestExceptionHandling(); 64 tester.runTests(); 65 } 66 67 TestExceptionHandling() throws IOException { 68 super(System.err); 69 tb = new ToolBox(); 70 ostream = System.err; 71 testSrcFile = Paths.get("A.java").toAbsolutePath(); 72 tb.writeFile(testSrcFile, "public class A { }"); 73 cmdTask = new JavadocTask(tb, Task.Mode.CMDLINE); 74 apiTask = new JavadocTask(tb, Task.Mode.API); 75 } 76 77 @Test 78 public void testDocletTrace() throws Exception { 79 Path out = Paths.get("out"); 80 // create a file with the same name as the output 81 out.toFile().createNewFile(); 82 cmdTask.outdir(out); 83 cmdTask.options("--dump-on-error"); 84 cmdTask.files(testSrcFile); 85 Task.Result tr = cmdTask.run(Task.Expect.FAIL); 86 87 String errString = "Destination directory is not a directory: " + out.toString(); 88 // check the regular message 89 assertPresent("javadoc: error - " + errString, tr.getOutputLines(Task.OutputKind.DIRECT)); 90 // check that first line of the stack trace is present 91 assertPresent("jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException: " + 92 errString, tr.getOutputLines(Task.OutputKind.STDERR)); 93 94 } 95 96 @Test 97 public void testToolTrace() throws Exception { 98 Path out = Paths.get("out.dir"); 99 cmdTask.options("--dump-on-error", "-doclet", "NonExistentDoclet"); 100 cmdTask.outdir(out); 101 cmdTask.files(testSrcFile); 102 Task.Result tr = cmdTask.run(Task.Expect.FAIL); 103 104 // check the regular message 105 assertPresent("javadoc: error - Cannot find doclet class NonExistentDoclet", 106 tr.getOutputLines(Task.OutputKind.DIRECT)); 107 108 // check that first line of the stack trace is present 109 assertPresent("java.lang.ClassNotFoundException: NonExistentDoclet", 110 tr.getOutputLines(Task.OutputKind.STDERR)); 111 112 } 113 114 @Test 115 public void testApiModeMissingDoclet() throws Exception { 116 apiTask.options("-doclet", "MissingDoclet"); 117 try { 118 Task.Result result = apiTask.run(Task.Expect.FAIL); 119 } catch (IllegalArgumentException iae) { 120 // ok got the right exception 121 return; 122 } 123 throw new Exception("expected exception/error not found"); 124 } 125 126 @Test 127 public void testApiModeMultipleDoclets() throws Exception { 128 apiTask.options("-doclet", "MissingDoclet", 129 "-doclet", "SomeDoclet"); 130 try { 131 Task.Result result = apiTask.run(Task.Expect.FAIL); 132 } catch (IllegalArgumentException iae) { 133 // ok got the right exception 134 return; 135 } 136 throw new Exception("expected exception/error not found"); 137 } 138 139 void assertPresent(String regex, List<String> output) throws Exception { 140 List<String> gresult = tb.grep(regex, output); 141 if (gresult.isEmpty()) { 142 ostream.println("Expected: " + regex); 143 ostream.println("Output: "); 144 output.forEach(s -> { 145 ostream.println(s); 146 }); 147 throw new Exception("Test fails expected output not found: " + regex); 148 } 149 } 150} 151