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