langtools/test/tools/javac/api/file/SJFM_GetFileObjects.java
changeset 28332 cd3ea1087d2b
child 30730 d3ce7619db2c
equal deleted inserted replaced
28331:43ed6b2e0e3b 28332:cd3ea1087d2b
       
     1 /*
       
     2  * Copyright (c) 2014, 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 8059977
       
    27  * @summary StandardJavaFileManager should support java.nio.file.Path.
       
    28  *          Test getFileObject methods.
       
    29  * @build SJFM_TestBase
       
    30  * @run main SJFM_GetFileObjects
       
    31  */
       
    32 
       
    33 import java.io.File;
       
    34 import java.io.IOException;
       
    35 import java.nio.file.Path;
       
    36 import java.util.Collections;
       
    37 import java.util.List;
       
    38 import javax.tools.JavaFileObject;
       
    39 import javax.tools.StandardJavaFileManager;
       
    40 import javax.tools.StandardLocation;
       
    41 
       
    42 /**
       
    43  * For those paths supported by a file manager, verify that the paths
       
    44  * can be encapsulated by file objects, such that the file objects can
       
    45  * be used by a tool such as javac.
       
    46  */
       
    47 public class SJFM_GetFileObjects extends SJFM_TestBase {
       
    48     public static void main(String... args) throws Exception {
       
    49         new SJFM_GetFileObjects().run();
       
    50     }
       
    51 
       
    52     @Test
       
    53     void test_getJavaFileObjects(StandardJavaFileManager fm) throws IOException {
       
    54         test_getJavaFileObjects(fm, getTestFilePaths());
       
    55         test_getJavaFileObjects(fm, getTestZipPaths());
       
    56     }
       
    57 
       
    58     /**
       
    59      * Tests the getJavaFileObjects method for a specific file manager
       
    60      * and a series of paths.
       
    61      *
       
    62      * Note: instances of MyStandardJavaFileManager only support
       
    63      * encapsulating paths for files in the default file system.
       
    64      *
       
    65      * @param fm  the file manager to be tested
       
    66      * @param paths  the paths to be tested
       
    67      * @throws IOException
       
    68      */
       
    69     void test_getJavaFileObjects(StandardJavaFileManager fm, List<Path> paths) throws IOException {
       
    70         boolean expectException = !isGetFileObjectsSupported(fm, paths);
       
    71         try {
       
    72             compile(fm.getJavaFileObjects(paths.toArray(new Path[paths.size()])));
       
    73             if (expectException)
       
    74                 error("expected exception not thrown");
       
    75         } catch (RuntimeException e) {
       
    76             if (expectException && e instanceof IllegalArgumentException)
       
    77                 return;
       
    78             error("unexpected exception thrown: " + e);
       
    79         }
       
    80     }
       
    81 
       
    82     //----------------------------------------------------------------------------------------------
       
    83 
       
    84     @Test
       
    85     void test_getJavaFileObjectsFromPaths(StandardJavaFileManager fm) throws IOException {
       
    86         test_getJavaFileObjectsFromPaths(fm, getTestFilePaths());
       
    87         test_getJavaFileObjectsFromPaths(fm, getTestZipPaths());
       
    88     }
       
    89 
       
    90     /**
       
    91      * Tests the getJavaFileObjectsFromPaths method for a specific file manager
       
    92      * and a series of paths.
       
    93      *
       
    94      * Note: instances of MyStandardJavaFileManager only support
       
    95      * encapsulating paths for files in the default file system.
       
    96      *
       
    97      * @param fm  the file manager to be tested
       
    98      * @param paths  the paths to be tested
       
    99      * @throws IOException
       
   100      */
       
   101     void test_getJavaFileObjectsFromPaths(StandardJavaFileManager fm, List<Path> paths)
       
   102             throws IOException {
       
   103         boolean expectException = !isGetFileObjectsSupported(fm, paths);
       
   104         try {
       
   105             compile(fm.getJavaFileObjectsFromPaths(paths));
       
   106             if (expectException)
       
   107                 error("expected exception not thrown: " + IllegalArgumentException.class.getName());
       
   108         } catch (RuntimeException e) {
       
   109             if (expectException && e instanceof IllegalArgumentException)
       
   110                 return;
       
   111             error("unexpected exception thrown: " + e);
       
   112         }
       
   113     }
       
   114 
       
   115 
       
   116     //----------------------------------------------------------------------------------------------
       
   117 
       
   118     /**
       
   119      * Compiles a set of files.
       
   120      *
       
   121      * @param files the files to be compiled.
       
   122      * @throws IOException
       
   123      */
       
   124     void compile(Iterable<? extends JavaFileObject> files) throws IOException {
       
   125         String name = "compile" + (compileCount++);
       
   126         try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
       
   127             File f = new File(name);
       
   128             f.mkdirs();
       
   129             // use setLocation(Iterable<File>) to avoid relying on setLocationFromPaths
       
   130             fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(f));
       
   131             boolean ok = comp.getTask(null, fm, null, null, null, files).call();
       
   132             if (!ok)
       
   133                 error(name + ": compilation failed");
       
   134         }
       
   135     }
       
   136 
       
   137     int compileCount;
       
   138 }