langtools/test/tools/javadoc/doclint/DocLintTest.java
changeset 31752 a4ea4c9bce2f
parent 30730 d3ce7619db2c
equal deleted inserted replaced
31751:ec251536a004 31752:a4ea4c9bce2f
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8004834 8007610
    26  * @bug 8004834 8007610 8129909
    27  * @summary Add doclint support into javadoc
    27  * @summary Add doclint support into javadoc
    28  * @modules jdk.compiler/com.sun.tools.javac.main
    28  * @modules jdk.compiler/com.sun.tools.javac.main
    29  */
    29  */
    30 
    30 
    31 import java.io.File;
    31 import java.io.File;
    57         new DocLintTest().run();
    57         new DocLintTest().run();
    58     }
    58     }
    59 
    59 
    60     DocumentationTool javadoc;
    60     DocumentationTool javadoc;
    61     StandardJavaFileManager fm;
    61     StandardJavaFileManager fm;
    62     JavaFileObject file;
    62     Iterable<? extends JavaFileObject> files;
    63 
    63 
    64     final String code =
    64     final String code =
    65         /* 01 */    "/** Class comment. */\n" +
    65         /* 01 */    "/** Class comment. */\n" +
    66         /* 02 */    "public class Test {\n" +
    66         /* 02 */    "public class Test {\n" +
    67         /* 03 */    "    /** Method comment. */\n" +
    67         /* 03 */    "    /** Method comment. */\n" +
    75         /* 11 */    "\n" +
    75         /* 11 */    "\n" +
    76         /* 12 */    "    /** @return */\n" +
    76         /* 12 */    "    /** @return */\n" +
    77         /* 13 */    "    public int emptyReturn() { return 0; }\n" +
    77         /* 13 */    "    public int emptyReturn() { return 0; }\n" +
    78         /* 14 */    "}\n";
    78         /* 14 */    "}\n";
    79 
    79 
       
    80     final String p1Code =
       
    81         /* 01 */    "package p1;\n" +
       
    82         /* 02 */    "public class P1Test {\n" +
       
    83         /* 03 */    "    /** Syntax < error. */\n" +
       
    84         /* 04 */    "    public void method() { }\n" +
       
    85         /* 05 */    "}\n";
       
    86 
       
    87     final String p2Code =
       
    88         /* 01 */    "package p2;\n" +
       
    89         /* 02 */    "public class P2Test {\n" +
       
    90         /* 03 */    "    /** Syntax < error. */\n" +
       
    91         /* 04 */    "    public void method() { }\n" +
       
    92         /* 05 */    "}\n";
       
    93 
    80     private final String rawDiags = "-XDrawDiagnostics";
    94     private final String rawDiags = "-XDrawDiagnostics";
    81 
    95 
    82     private enum Message {
    96     private enum Message {
    83         // doclint messages
    97         // doclint messages
    84         DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),
    98         DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),
    85         DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),
    99         DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),
    86         DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),
   100         DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),
    87 
   101 
       
   102         DL_ERR_P1TEST(ERROR, "P1Test.java:3:16: compiler.err.proc.messager: malformed HTML"),
       
   103         DL_ERR_P2TEST(ERROR, "P2Test.java:3:16: compiler.err.proc.messager: malformed HTML"),
       
   104 
    88         // doclint messages when -XDrawDiagnostics is not in effect
   105         // doclint messages when -XDrawDiagnostics is not in effect
    89         DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),
   106         DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),
    90         DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),
   107         DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),
    91 
   108 
    92         // javadoc messages about bad content: these should only appear when doclint is disabled
   109         // javadoc messages about bad content: these should only appear when doclint is disabled
    93         JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"),
   110         JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"),
    94         JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."),
   111         JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."),
    95 
   112 
    96         // javadoc messages for bad options
   113         // javadoc messages for bad options
    97         OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"),
   114         OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"),
    98         OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments");
   115         OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments"),
       
   116         OPT_BADPACKAGEARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint/package option");
    99 
   117 
   100         final Diagnostic.Kind kind;
   118         final Diagnostic.Kind kind;
   101         final String text;
   119         final String text;
   102 
   120 
   103         static Message get(String text) {
   121         static Message get(String text) {
   122     void run() throws Exception {
   140     void run() throws Exception {
   123         javadoc = ToolProvider.getSystemDocumentationTool();
   141         javadoc = ToolProvider.getSystemDocumentationTool();
   124         fm = javadoc.getStandardFileManager(null, null, null);
   142         fm = javadoc.getStandardFileManager(null, null, null);
   125         try {
   143         try {
   126             fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
   144             fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
   127             file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) {
   145             files = Arrays.asList(new TestJFO("Test.java", code));
   128                 @Override
       
   129                 public CharSequence getCharContent(boolean ignoreEncoding) {
       
   130                     return code;
       
   131                 }
       
   132             };
       
   133 
   146 
   134             test(Collections.<String>emptyList(),
   147             test(Collections.<String>emptyList(),
   135                     Main.Result.ERROR,
   148                     Main.Result.ERROR,
   136                     EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));
   149                     EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));
   137 
   150 
   172                     EnumSet.of(Message.DL_ERR9));
   185                     EnumSet.of(Message.DL_ERR9));
   173 
   186 
   174             test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),
   187             test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),
   175                     Main.Result.ERROR,
   188                     Main.Result.ERROR,
   176                     EnumSet.of(Message.OPT_BADARG));
   189                     EnumSet.of(Message.OPT_BADARG));
       
   190 
       
   191             files = Arrays.asList(new TestJFO("p1/P1Test.java", p1Code),
       
   192                                   new TestJFO("p2/P2Test.java", p2Code));
       
   193 
       
   194             test(Arrays.asList(rawDiags),
       
   195                     Main.Result.ERROR,
       
   196                     EnumSet.of(Message.DL_ERR_P1TEST, Message.DL_ERR_P2TEST));
       
   197 
       
   198             test(Arrays.asList(rawDiags, "-Xdoclint/package:p1"),
       
   199                     Main.Result.ERROR,
       
   200                     EnumSet.of(Message.DL_ERR_P1TEST));
       
   201 
       
   202             test(Arrays.asList(rawDiags, "-Xdoclint/package:*p"),
       
   203                     Main.Result.ERROR,
       
   204                     EnumSet.of(Message.OPT_BADPACKAGEARG));
   177 
   205 
   178             if (errors > 0)
   206             if (errors > 0)
   179                 throw new Exception(errors + " errors occurred");
   207                 throw new Exception(errors + " errors occurred");
   180         } finally {
   208         } finally {
   181             fm.close();
   209             fm.close();
   184 
   212 
   185     void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
   213     void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
   186         System.err.println("test: " + opts);
   214         System.err.println("test: " + opts);
   187         StringWriter sw = new StringWriter();
   215         StringWriter sw = new StringWriter();
   188         PrintWriter pw = new PrintWriter(sw);
   216         PrintWriter pw = new PrintWriter(sw);
   189         List<JavaFileObject> files = Arrays.asList(file);
       
   190         try {
   217         try {
   191             DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
   218             DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
   192             boolean ok = t.call();
   219             boolean ok = t.call();
   193             pw.close();
   220             pw.close();
   194             String out = sw.toString().replaceAll("[\r\n]+", "\n");
   221             String out = sw.toString().replaceAll("[\r\n]+", "\n");
   255         System.err.println("Error: " + msg);
   282         System.err.println("Error: " + msg);
   256         errors++;
   283         errors++;
   257     }
   284     }
   258 
   285 
   259     int errors;
   286     int errors;
       
   287 
       
   288     class TestJFO extends SimpleJavaFileObject {
       
   289 
       
   290         private final String content;
       
   291 
       
   292         public TestJFO(String fileName, String content) {
       
   293             super(URI.create(fileName), JavaFileObject.Kind.SOURCE);
       
   294             this.content = content;
       
   295         }
       
   296 
       
   297         @Override
       
   298         public CharSequence getCharContent(boolean ignoreEncoding) {
       
   299             return content;
       
   300         }
       
   301     };
   260 }
   302 }