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 8007490
  27  * @summary javadoc should have a javax.tools.Tool service provider
  28  * @modules java.compiler
  29  *          jdk.compiler
  30  * @build APITest
  31  * @run main RunTest
  32  */
  33 
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.File;
  36 import java.io.PrintStream;
  37 import javax.tools.DocumentationTool;
  38 import javax.tools.ToolProvider;
  39 
  40 /**
  41  * Tests for DocumentationTool.run method.
  42  */
  43 public class RunTest extends APITest {
  44     public static void main(String... args) throws Exception {
  45         new RunTest().run();
  46     }
  47 
  48     /**
  49      * Verify that run method can be invoked.
  50      */
  51     @Test
  52     public void testRunOK() throws Exception {
  53         File testSrc = new File(System.getProperty("test.src"));
  54         File srcFile = new File(testSrc, "pkg/C.java");
  55         File outDir = getOutDir();
  56         String[] args = { "-d", outDir.getPath(), srcFile.getPath() };
  57 
  58         ByteArrayOutputStream stdout = new ByteArrayOutputStream();
  59         ByteArrayOutputStream stderr = new ByteArrayOutputStream();
  60         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  61         int rc = tool.run(null, stdout, stderr, args);
  62         System.err.println("stdout >>" + stdout.toString() + "<<");
  63         System.err.println("stderr >>" + stderr.toString() + "<<");
  64 
  65         if (rc == 0) {
  66             System.err.println("call succeeded");
  67             checkFiles(outDir, standardExpectFiles);
  68             String out = stdout.toString();
  69             for (String f: standardExpectFiles) {
  70                 String f1 = f.replace('/', File.separatorChar);
  71                 if (f1.endsWith(".html") && !out.contains(f1))
  72                     error("expected string not found: " + f1);
  73             }
  74         } else {
  75             error("call failed");
  76         }
  77     }
  78 
  79     /**
  80      * Verify that run method can be invoked.
  81      */
  82     @Test
  83     public void testRunFail() throws Exception {
  84         File outDir = getOutDir();
  85         String badfile = "badfile.java";
  86         String[] args = { "-d", outDir.getPath(), badfile };
  87 
  88         ByteArrayOutputStream stdout = new ByteArrayOutputStream();
  89         ByteArrayOutputStream stderr = new ByteArrayOutputStream();
  90         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  91         int rc = tool.run(null, stdout, stderr, args);
  92         System.err.println("stdout >>" + stdout.toString() + "<<");
  93         System.err.println("stderr >>" + stderr.toString() + "<<");
  94 
  95         if (rc == 0) {
  96             error("call succeeded unexpectedly");
  97         } else {
  98             String err = stderr.toString();
  99             if (err.contains(badfile))
 100                 System.err.println("call failed as expected");
 101             else
 102                 error("expected diagnostic not found");
 103         }
 104     }
 105 
 106     /**
 107      * Verify that null args are accepted.
 108      */
 109     @Test
 110     public void testNullArgs() throws Exception {
 111         File testSrc = new File(System.getProperty("test.src"));
 112         File srcFile = new File(testSrc, "pkg/C.java");
 113         File outDir = getOutDir();
 114         String[] args = { "-d", outDir.getPath(), srcFile.getPath() };
 115 
 116         ByteArrayOutputStream stdout = new ByteArrayOutputStream();
 117         PrintStream prevStdout = System.out;
 118         System.setOut(new PrintStream(stdout));
 119 
 120         ByteArrayOutputStream stderr = new ByteArrayOutputStream();
 121         PrintStream prevStderr = System.err;
 122         System.setErr(new PrintStream(stderr));
 123 
 124         int rc ;
 125         try {
 126             DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
 127             rc = tool.run(null, null, null, args);
 128         } finally {
 129             System.setOut(prevStdout);
 130             System.setErr(prevStderr);
 131         }
 132 
 133         System.err.println("stdout >>" + stdout.toString() + "<<");
 134         System.err.println("stderr >>" + stderr.toString() + "<<");
 135 
 136         if (rc == 0) {
 137             System.err.println("call succeeded");
 138             checkFiles(outDir, standardExpectFiles);
 139             String out = stdout.toString();
 140             for (String f: standardExpectFiles) {
 141                 String f1 = f.replace('/', File.separatorChar);
 142                 if (f1.endsWith(".html") && !out.contains(f1))
 143                     error("expected string not found: " + f1);
 144             }
 145         } else {
 146             error("call failed");
 147         }
 148     }
 149 }
 150