1 /*
   2  * Copyright (c) 2012, 2015, 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 6493690
  27  * @summary javadoc should have a javax.tools.Tool service provider
  28  * @modules java.compiler
  29  *          jdk.compiler
  30  * @build APITest
  31  * @run main GetTask_DiagListenerTest
  32  */
  33 
  34 import java.io.File;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.List;
  38 import javax.tools.Diagnostic;
  39 import javax.tools.DiagnosticCollector;
  40 import javax.tools.DocumentationTool;
  41 import javax.tools.DocumentationTool.DocumentationTask;
  42 import javax.tools.JavaFileObject;
  43 import javax.tools.StandardJavaFileManager;
  44 import javax.tools.ToolProvider;
  45 
  46 /**
  47  * Tests for DocumentationTool.getTask  diagnosticListener  parameter.
  48  */
  49 public class GetTask_DiagListenerTest extends APITest {
  50     public static void main(String... args) throws Exception {
  51         new GetTask_DiagListenerTest().run();
  52     }
  53 
  54     /**
  55      * Verify that a diagnostic listener can be specified.
  56      * Note that messages from the tool and doclet are imperfectly modeled
  57      * because the DocErrorReporter API works in terms of localized strings
  58      * and file:line positions. Therefore, messages reported via DocErrorReporter
  59      * and simply wrapped and passed through.
  60      */
  61     @Test
  62     public void testDiagListener() throws Exception {
  63         JavaFileObject srcFile = createSimpleJavaFileObject("pkg/C", "package pkg; public error { }");
  64         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  65         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
  66             File outDir = getOutDir();
  67             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
  68             Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
  69             DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
  70             DocumentationTask t = tool.getTask(null, fm, dc, null, null, files);
  71             if (t.call()) {
  72                 throw new Exception("task succeeded unexpectedly");
  73             } else {
  74                 List<String> diagCodes = new ArrayList<String>();
  75                 for (Diagnostic d: dc.getDiagnostics()) {
  76                     System.err.println(d);
  77                     diagCodes.add(d.getCode());
  78                 }
  79                 List<String> expect = Arrays.asList(
  80                         "javadoc.note.msg",         // Loading source file
  81                         "compiler.err.expected3",   // class, interface, enum, or __datum expected
  82                         "javadoc.note.msg");        // 1 error
  83                 if (!diagCodes.equals(expect))
  84                     throw new Exception("unexpected diagnostics occurred");
  85                 System.err.println("diagnostics received as expected");
  86             }
  87         }
  88     }
  89 
  90 }
  91