langtools/test/tools/javadoc/api/basic/GetTask_FileObjectsTest.java
changeset 14545 2e7bab0639b8
child 27319 030080f03e4f
equal deleted inserted replaced
14544:d71d992cb905 14545:2e7bab0639b8
       
     1 /*
       
     2  * Copyright (c) 2012, 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  * @build APITest
       
    29  * @run main GetTask_FileObjectsTest
       
    30  */
       
    31 
       
    32 import java.io.File;
       
    33 import java.util.Arrays;
       
    34 import javax.tools.DocumentationTool;
       
    35 import javax.tools.DocumentationTool.DocumentationTask;
       
    36 import javax.tools.JavaFileObject;
       
    37 import javax.tools.StandardJavaFileManager;
       
    38 import javax.tools.ToolProvider;
       
    39 
       
    40 /**
       
    41  * Tests for DocumentationTool.getTask  fileObjects  parameter.
       
    42  */
       
    43 public class GetTask_FileObjectsTest extends APITest {
       
    44     public static void main(String... args) throws Exception {
       
    45         new GetTask_FileObjectsTest().run();
       
    46     }
       
    47 
       
    48     /**
       
    49      * Verify that expected output files are written via the file manager,
       
    50      * for a source file read from the file system with StandardJavaFileManager.
       
    51      */
       
    52     @Test
       
    53     public void testStandardFileObject() throws Exception {
       
    54         File testSrc = new File(System.getProperty("test.src"));
       
    55         File srcFile = new File(testSrc, "pkg/C.java");
       
    56         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
       
    57         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
       
    58         File outDir = getOutDir();
       
    59         fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
       
    60         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(srcFile);
       
    61         DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
       
    62         if (t.call()) {
       
    63             System.err.println("task succeeded");
       
    64             checkFiles(outDir, standardExpectFiles);
       
    65         } else {
       
    66             throw new Exception("task failed");
       
    67         }
       
    68     }
       
    69 
       
    70     /**
       
    71      * Verify that expected output files are written via the file manager,
       
    72      * for an in-memory file object.
       
    73      */
       
    74     @Test
       
    75     public void testMemoryFileObject() throws Exception {
       
    76         JavaFileObject srcFile = createSimpleJavaFileObject();
       
    77         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
       
    78         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
       
    79         File outDir = getOutDir();
       
    80         fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
       
    81         Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
       
    82         DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
       
    83         if (t.call()) {
       
    84             System.err.println("task succeeded");
       
    85             checkFiles(outDir, standardExpectFiles);
       
    86         } else {
       
    87             throw new Exception("task failed");
       
    88         }
       
    89     }
       
    90 
       
    91     /**
       
    92      * Verify bad file object is handled correctly.
       
    93      */
       
    94     @Test
       
    95     public void testBadFileObject() throws Exception {
       
    96         File testSrc = new File(System.getProperty("test.src"));
       
    97         File srcFile = new File(testSrc, "pkg/C.class");  // unacceptable file kind
       
    98         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
       
    99         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
       
   100         File outDir = getOutDir();
       
   101         fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
       
   102         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(srcFile);
       
   103         try {
       
   104             DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
       
   105             error("getTask succeeded, no exception thrown");
       
   106         } catch (IllegalArgumentException e) {
       
   107             System.err.println("exception caught as expected: " + e);
       
   108         }
       
   109     }
       
   110 
       
   111     /**
       
   112      * Verify null is handled correctly.
       
   113      */
       
   114     @Test
       
   115     public void testNull() throws Exception {
       
   116         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
       
   117         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
       
   118         File outDir = getOutDir();
       
   119         fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
       
   120         Iterable<? extends JavaFileObject> files = Arrays.asList((JavaFileObject) null);
       
   121         try {
       
   122             DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
       
   123             error("getTask succeeded, no exception thrown");
       
   124         } catch (NullPointerException e) {
       
   125             System.err.println("exception caught as expected: " + e);
       
   126         }
       
   127     }
       
   128 
       
   129 }
       
   130