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 jdk.compiler/com.sun.tools.javac.file
  29  *          jdk.compiler/com.sun.tools.javac.util
  30  *          jdk.javadoc/com.sun.tools.javadoc.api
  31  * @build APITest
  32  * @run main JavadocTaskImplTest
  33  */
  34 
  35 import java.io.File;
  36 import java.util.Arrays;
  37 import java.util.concurrent.Callable;
  38 
  39 import javax.tools.DocumentationTool;
  40 import javax.tools.DocumentationTool.DocumentationTask;
  41 import javax.tools.JavaFileObject;
  42 import javax.tools.StandardJavaFileManager;
  43 import javax.tools.ToolProvider;
  44 
  45 import com.sun.tools.javac.file.JavacFileManager;
  46 import com.sun.tools.javac.util.Context;
  47 import jdk.javadoc.internal.api.JavadocTaskImpl;
  48 import jdk.javadoc.internal.tool.Messager;
  49 
  50 /**
  51  *  Misc tests for JavacTaskImpl.
  52  */
  53 public class JavadocTaskImplTest extends APITest {
  54     public static void main(String... args) throws Exception {
  55         new JavadocTaskImplTest().run();
  56     }
  57 
  58     @Test
  59     public void testRawCall() throws Exception {
  60         JavaFileObject srcFile = createSimpleJavaFileObject();
  61         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  62         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
  63             File outDir = getOutDir();
  64             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
  65             Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
  66 
  67             @SuppressWarnings("rawtypes")
  68             Callable t = tool.getTask(null, fm, null, null, null, files);
  69 
  70             if (t.call() == Boolean.TRUE) {
  71                 System.err.println("task succeeded");
  72             } else {
  73                 throw new Exception("task failed");
  74             }
  75         }
  76     }
  77 
  78     @Test
  79     public void testDirectAccess1() throws Exception {
  80         JavaFileObject srcFile = createSimpleJavaFileObject();
  81         Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
  82         Context c = new Context();
  83         Messager.preRegister(c, "javadoc");
  84         try (StandardJavaFileManager fm = new JavacFileManager(c, true, null)) {
  85             File outDir = getOutDir();
  86             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
  87             DocumentationTask t = new JavadocTaskImpl(c, null, null, files);
  88             if (t.call()) {
  89                 System.err.println("task succeeded");
  90             } else {
  91                 throw new Exception("task failed");
  92             }
  93         }
  94     }
  95 
  96     @Test
  97     public void testDirectAccess2() throws Exception {
  98         JavaFileObject srcFile = null; // error, provokes NPE
  99         Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
 100         Context c = new Context();
 101         Messager.preRegister(c, "javadoc");
 102         try (StandardJavaFileManager fm = new JavacFileManager(c, true, null)) {
 103             File outDir = getOutDir();
 104             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
 105             try {
 106                 DocumentationTask t = new JavadocTaskImpl(c, null, null, files);;
 107                 error("getTask succeeded, no exception thrown");
 108             } catch (NullPointerException e) {
 109                 System.err.println("exception caught as expected: " + e);
 110             }
 111         }
 112     }
 113 }
 114