test/langtools/tools/javadoc/8147801/T8147801.java
branchniosocketimpl-branch
changeset 57208 7a45c67e73d0
parent 57207 30695f27d7ea
parent 53902 7a6fd71449e7
child 57210 a67ea4f53e56
equal deleted inserted replaced
57207:30695f27d7ea 57208:7a45c67e73d0
     1 /*
       
     2  * Copyright (c) 2016, 2017, 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 8147801
       
    27  * @summary java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
       
    28  * @modules jdk.javadoc/com.sun.tools.javadoc
       
    29  * @library jarsrc
       
    30  * @build lib.* p.*
       
    31  * @run main T8147801
       
    32  */
       
    33 
       
    34 import java.io.File;
       
    35 import java.io.IOException;
       
    36 import java.io.PrintWriter;
       
    37 import java.io.StringWriter;
       
    38 import java.nio.file.ClosedFileSystemException;
       
    39 import java.nio.file.Files;
       
    40 import java.nio.file.Path;
       
    41 import java.nio.file.Paths;
       
    42 import java.util.ArrayList;
       
    43 import java.util.List;
       
    44 import java.util.jar.JarEntry;
       
    45 import java.util.jar.JarOutputStream;
       
    46 import java.util.stream.Stream;
       
    47 
       
    48 import com.sun.javadoc.ClassDoc;
       
    49 import com.sun.javadoc.FieldDoc;
       
    50 import com.sun.javadoc.RootDoc;
       
    51 
       
    52 /*
       
    53  * This test verifies the use of the hidden fileManager.deferClose
       
    54  * option, to work around the limitation that javadoc objects
       
    55  * (like RootDoc and related types) should cannot normally be used
       
    56  * after javadoc exits, closing its file manager (if it opened it.)
       
    57  *
       
    58  * The test runs javadoc on a chain of classes, 1 in source form,
       
    59  * and 2 in a jar file. javadoc/javac will "complete" classes found
       
    60  * in source, but will eagerly "classes" in class form.
       
    61  * The chain is p/Test.java -> lib/Lib1.class -> lib/Lib2.class.
       
    62  * After javadoc exits, the classes are examined, to finally force
       
    63  * the classes to be completed, possibly causing javac to try and access
       
    64  * references into a .jar file system which has now been closed.
       
    65  *
       
    66  * The test runs two test cases -- one without the workaround option,
       
    67  * to test the validity of the test case, and one with the workaround
       
    68  * option, to test that it works as expected.
       
    69  */
       
    70 public class T8147801 {
       
    71     public static void main(String... args) throws Exception {
       
    72         new T8147801().run();
       
    73     }
       
    74 
       
    75     void run() throws Exception {
       
    76         initJar();
       
    77         test(false);
       
    78         test(true);
       
    79         if (errors > 0) {
       
    80             throw new Exception(errors + " errors occurred");
       
    81         }
       
    82     }
       
    83 
       
    84     void test(boolean withOption) {
       
    85         System.err.println("Testing " + (withOption ? "with" : "without") + " option");
       
    86         try {
       
    87             String dump = "";
       
    88             RootDoc root = getRootDoc(withOption);
       
    89             for (ClassDoc cd: root.specifiedClasses()) {
       
    90                 dump += dump(cd);
       
    91             }
       
    92             if (dump.contains("lib.Lib2.i")) {
       
    93                 if (!withOption) {
       
    94                     error("control case failed: Lib2 class file was read, unexpectedly, without using option");
       
    95                 }
       
    96             } else {
       
    97                 if (withOption) {
       
    98                     error("test case failed: could not read Lib2 class file, using option");
       
    99                 }
       
   100             }
       
   101         } catch (ClosedFileSystemException e) {
       
   102             error("Unexpected exception: " + e);
       
   103         }
       
   104         System.err.println();
       
   105     }
       
   106 
       
   107     RootDoc getRootDoc(boolean withOption) {
       
   108         List<String> opts = new ArrayList<>();
       
   109         if (withOption)
       
   110             opts.add("-XDfileManager.deferClose=10");
       
   111         opts.add("-doclet");
       
   112         opts.add(getClass().getName());
       
   113         opts.add("-classpath");
       
   114         opts.add(jarPath.toString());
       
   115         opts.add(Paths.get(System.getProperty("test.src"), "p", "Test.java").toString());
       
   116         System.err.println("javadoc opts: " + opts);
       
   117         int rc = com.sun.tools.javadoc.Main.execute(
       
   118                 "javadoc",
       
   119                 // by specifying our own class loader, we get the same Class instance as this
       
   120                 getClass().getClassLoader(),
       
   121                 opts.toArray(new String[opts.size()]));
       
   122         if (rc != 0) {
       
   123             error("unexpected exit from javadoc or doclet: " + rc);
       
   124         }
       
   125         return cachedRoot;
       
   126     }
       
   127 
       
   128     String dump(ClassDoc cd) {
       
   129         StringWriter sw = new StringWriter();
       
   130         PrintWriter pw = new PrintWriter(sw);
       
   131         dump(pw, "", cd);
       
   132         String out = sw.toString();
       
   133         System.err.println(out);
       
   134         return out;
       
   135     }
       
   136 
       
   137     void dump(PrintWriter out, String prefix, ClassDoc cd) {
       
   138         out.println(prefix + "class: " + cd);
       
   139         for (FieldDoc fd: cd.fields()) {
       
   140             out.println(prefix + "  " + fd);
       
   141             if (fd.type().asClassDoc() != null) {
       
   142                 dump(out, prefix + "    ", fd.type().asClassDoc());
       
   143             }
       
   144         }
       
   145     }
       
   146 
       
   147     void initJar() throws IOException {
       
   148         String testClassPath = System.getProperty("test.class.path", "");
       
   149         Path jarsrc = Stream.of(testClassPath.split(File.pathSeparator))
       
   150                 .map(Paths::get)
       
   151                 .filter(e -> e.endsWith("jarsrc"))
       
   152                 .findAny()
       
   153                 .orElseThrow(() -> new InternalError("jarsrc not found"));
       
   154         jarPath = Paths.get("lib.jar");
       
   155         try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarPath))) {
       
   156             String[] classNames = {"Lib1.class", "Lib2.class"};
       
   157             for (String cn : classNames) {
       
   158                 out.putNextEntry(new JarEntry("lib/" + cn));
       
   159                 Path libClass = jarsrc.resolve("lib").resolve(cn);
       
   160                 out.write(Files.readAllBytes(libClass));
       
   161             }
       
   162         }
       
   163     }
       
   164 
       
   165     void error(String msg) {
       
   166         System.err.println("Error: " + msg);
       
   167         errors++;
       
   168     }
       
   169 
       
   170     Path jarPath;
       
   171     int errors;
       
   172 
       
   173     // Bad doclet caches the RootDoc for later use
       
   174 
       
   175     static RootDoc cachedRoot;
       
   176 
       
   177     public static boolean start(RootDoc root) {
       
   178         cachedRoot = root;
       
   179         return true;
       
   180     }
       
   181 }