langtools/test/tools/javadoc/8147801/T8147801.java
changeset 35807 2eb1d877da0f
child 43268 12436ebea906
equal deleted inserted replaced
35739:db483b34fa71 35807:2eb1d877da0f
       
     1 /*
       
     2  * Copyright (c) 2016, 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.IOException;
       
    35 import java.nio.file.ClosedFileSystemException;
       
    36 import java.nio.file.Files;
       
    37 import java.nio.file.Path;
       
    38 import java.nio.file.Paths;
       
    39 import java.util.ArrayList;
       
    40 import java.util.List;
       
    41 import java.util.jar.JarEntry;
       
    42 import java.util.jar.JarOutputStream;
       
    43 
       
    44 import com.sun.javadoc.ClassDoc;
       
    45 import com.sun.javadoc.FieldDoc;
       
    46 import com.sun.javadoc.RootDoc;
       
    47 
       
    48 /*
       
    49  * This test verifies the use of the hidden fileManager.deferClose
       
    50  * option, to work around the limitation that javadoc objects
       
    51  * (like RootDoc and related types) should cannot normally be used
       
    52  * after javadoc exits, closing its file manager (if it opened it.)
       
    53  *
       
    54  * The test runs javadoc on a chain of classes, 1 in source form,
       
    55  * and 2 in a jar file. javadoc/javac will "complete" classes found
       
    56  * in source, but will eagerly "classes" in class form.
       
    57  * The chain is p/Test.java -> lib/Lib1.class -> lib/Lib2.class.
       
    58  * After javadoc exits, the classes are examined, to finally force
       
    59  * the classes to be completed, possibly causing javac to try and access
       
    60  * references into a .jar file system which has now been closed.
       
    61  *
       
    62  * The test runs two test cases -- one without the workaround option,
       
    63  * to test the validity of the test case, and one with the workaround
       
    64  * option, to test that it works as expected.
       
    65  */
       
    66 public class T8147801 {
       
    67     public static void main(String... args) throws Exception {
       
    68         new T8147801().run();
       
    69     }
       
    70 
       
    71     void run() throws Exception {
       
    72         initJar();
       
    73         test(false);
       
    74         test(true);
       
    75         if (errors > 0) {
       
    76             throw new Exception(errors + " errors occurred");
       
    77         }
       
    78     }
       
    79 
       
    80     void test(boolean withOption) {
       
    81         System.err.println("Testing " + (withOption ? "with" : "without") + " option");
       
    82         try {
       
    83             RootDoc root = getRootDoc(withOption);
       
    84             for (ClassDoc cd: root.specifiedClasses()) {
       
    85                 dump("", cd);
       
    86             }
       
    87             if (!withOption) {
       
    88                 error("expected option did not occur");
       
    89             }
       
    90         } catch (ClosedFileSystemException e) {
       
    91             if (withOption) {
       
    92                 error("Unexpected exception: " + e);
       
    93             } else {
       
    94                 System.err.println("Exception received as expected: " + e);
       
    95             }
       
    96         }
       
    97         System.err.println();
       
    98     }
       
    99 
       
   100     RootDoc getRootDoc(boolean withOption) {
       
   101         List<String> opts = new ArrayList<>();
       
   102         if (withOption)
       
   103             opts.add("-XDfileManager.deferClose=10");
       
   104         opts.add("-doclet");
       
   105         opts.add(getClass().getName());
       
   106         opts.add("-classpath");
       
   107         opts.add(jarPath.toString());
       
   108         opts.add(Paths.get(System.getProperty("test.src"), "p", "Test.java").toString());
       
   109         System.err.println("javadoc opts: " + opts);
       
   110         int rc = com.sun.tools.javadoc.Main.execute(
       
   111                 "javadoc",
       
   112                 // by specifying our own class loader, we get the same Class instance as this
       
   113                 getClass().getClassLoader(),
       
   114                 opts.toArray(new String[opts.size()]));
       
   115         if (rc != 0) {
       
   116             error("unexpected exit from javadoc or doclet: " + rc);
       
   117         }
       
   118         return cachedRoot;
       
   119     }
       
   120 
       
   121     void dump(String prefix, ClassDoc cd) {
       
   122         System.err.println(prefix + "class: " + cd);
       
   123         for (FieldDoc fd: cd.fields()) {
       
   124             System.err.println(fd);
       
   125             if (fd.type().asClassDoc() != null) {
       
   126                 dump(prefix + "  ", fd.type().asClassDoc());
       
   127             }
       
   128         }
       
   129     }
       
   130 
       
   131     void initJar() throws IOException {
       
   132         Path testClasses = Paths.get(System.getProperty("test.classes"));
       
   133         jarPath = Paths.get("lib.jar");
       
   134         try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarPath))) {
       
   135             String[] classNames = {"Lib1.class", "Lib2.class"};
       
   136             for (String cn : classNames) {
       
   137                 out.putNextEntry(new JarEntry("lib/" + cn));
       
   138                 Path libClass = testClasses.resolve("jarsrc").resolve("lib").resolve(cn);
       
   139                 out.write(Files.readAllBytes(libClass));
       
   140             }
       
   141         }
       
   142     }
       
   143 
       
   144     void error(String msg) {
       
   145         System.err.println("Error: " + msg);
       
   146         errors++;
       
   147     }
       
   148 
       
   149     Path jarPath;
       
   150     int errors;
       
   151 
       
   152     // Bad doclet caches the RootDoc for later use
       
   153 
       
   154     static RootDoc cachedRoot;
       
   155 
       
   156     public static boolean start(RootDoc root) {
       
   157         cachedRoot = root;
       
   158         return true;
       
   159     }
       
   160 }