langtools/test/tools/javac/classreader/FileSystemClosedTest.java
changeset 43268 12436ebea906
child 43277 edf2836a33f7
equal deleted inserted replaced
43267:81e70bde1b96 43268:12436ebea906
       
     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 8147414
       
    27  * @summary java.nio.file.ClosedFileSystemException in javadoc
       
    28  * @library /tools/lib
       
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.main
       
    31  * @build toolbox.JarTask toolbox.JavacTask toolbox.ToolBox
       
    32  * @run main FileSystemClosedTest
       
    33  */
       
    34 
       
    35 import java.io.IOException;
       
    36 import java.io.PrintWriter;
       
    37 import java.nio.file.ClosedFileSystemException;
       
    38 import java.nio.file.Files;
       
    39 import java.nio.file.Path;
       
    40 import java.nio.file.Paths;
       
    41 import java.util.Arrays;
       
    42 import java.util.List;
       
    43 
       
    44 import javax.lang.model.element.PackageElement;
       
    45 import javax.lang.model.element.TypeElement;
       
    46 import javax.lang.model.util.Elements;
       
    47 import javax.tools.JavaCompiler;
       
    48 import javax.tools.JavaFileObject;
       
    49 import javax.tools.StandardJavaFileManager;
       
    50 import javax.tools.ToolProvider;
       
    51 
       
    52 import toolbox.ToolBox;
       
    53 import toolbox.JarTask;
       
    54 import toolbox.JavacTask;
       
    55 
       
    56 public class FileSystemClosedTest {
       
    57     public static void main(String... args) throws Exception {
       
    58         new FileSystemClosedTest().run();
       
    59     }
       
    60 
       
    61     void run() throws Exception {
       
    62         ToolBox tb = new ToolBox();
       
    63         Path jar = createJar(tb);
       
    64 
       
    65         Path src = Paths.get("src");
       
    66         tb.writeJavaFiles(src, "class C { p1.C1 c1; }");
       
    67 
       
    68         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
       
    69         PrintWriter out = new PrintWriter(System.err, true);
       
    70         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
       
    71         List<String> options = Arrays.asList("-classpath", jar.toString());
       
    72         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(src.resolve("C.java"));
       
    73         com.sun.source.util.JavacTask task =
       
    74                 (com.sun.source.util.JavacTask) comp.getTask(out, fm, null, options, null, files);
       
    75         task.parse();
       
    76 
       
    77         Elements elems = task.getElements();
       
    78 
       
    79         try {
       
    80             // Use  p1, p1.C1 as a control to verify normal behavior
       
    81             PackageElement p1 = elems.getPackageElement("p1");
       
    82             TypeElement p1C1 = elems.getTypeElement("p1.C1");
       
    83             System.err.println("p1: " + p1 + ";  p1C1: " + p1C1);
       
    84             if (p1C1 == null) {
       
    85                 throw new Exception("p1.C1 not found");
       
    86             }
       
    87 
       
    88             // Now repeat for p2, p2.C2, closing the file manager early
       
    89             PackageElement p2 = elems.getPackageElement("p2");
       
    90             System.err.println("closing file manager");
       
    91             fm.close();
       
    92             TypeElement p2C2 = elems.getTypeElement("p2.C2");
       
    93             System.err.println("p2: " + p2 + ";  p2C2: " + p2C2);
       
    94             if (p2C2 != null) {
       
    95                 throw new Exception("p1.C1 found unexpectedly");
       
    96             }
       
    97         } catch (ClosedFileSystemException e) {
       
    98             throw new Exception("unexpected exception thrown", e);
       
    99         }
       
   100 
       
   101     }
       
   102 
       
   103     private Path createJar(ToolBox tb) throws IOException {
       
   104         Path jarSrc = Paths.get("jarSrc");
       
   105         Path jarClasses = Paths.get("jarClasses");
       
   106         Path jar = Paths.get("jar.jar");
       
   107         Files.createDirectories(jarClasses);
       
   108 
       
   109         tb.writeJavaFiles(jarSrc,
       
   110                 "package p1; public class C1 { }",
       
   111                 "package p2; public class C2 { }");
       
   112 
       
   113         new JavacTask(tb)
       
   114                 .outdir(jarClasses)
       
   115                 .files(tb.findJavaFiles(jarSrc))
       
   116                 .run()
       
   117                 .writeAll();
       
   118         new JarTask(tb)
       
   119                 .run("cf", jar.toString(), "-C", jarClasses.toString(), "p1", "p2");
       
   120 
       
   121         return jar;
       
   122     }
       
   123 }
       
   124