langtools/test/tools/javac/api/file/SJFM_AsPath.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 asPath method.
       
    29  * @build SJFM_TestBase
       
    30  * @run main SJFM_AsPath
       
    31  */
       
    32 
       
    33 import java.io.IOException;
       
    34 import java.nio.file.Path;
       
    35 import java.util.HashSet;
       
    36 import java.util.List;
       
    37 import java.util.Set;
       
    38 import javax.tools.JavaFileObject;
       
    39 import javax.tools.StandardJavaFileManager;
       
    40 
       
    41 /**
       
    42  * For those paths which are supported by a file manager, such that
       
    43  * a file object can encapsulate the path, verify that the underlying
       
    44  * path can be recovered from the file object.
       
    45  */
       
    46 public class SJFM_AsPath extends SJFM_TestBase {
       
    47     public static void main(String... args) throws Exception {
       
    48         new SJFM_AsPath().run();
       
    49     }
       
    50 
       
    51     @Test
       
    52     void test_asPath(StandardJavaFileManager fm) throws IOException {
       
    53         test_asPath(fm, getTestFilePaths());
       
    54         test_asPath(fm, getTestZipPaths());
       
    55     }
       
    56 
       
    57     /**
       
    58      * Tests the asPath method for a specific file manager and a series
       
    59      * of paths.
       
    60      *
       
    61      * Note: instances of MyStandardJavaFileManager only support
       
    62      * encapsulating paths for files in the default file system,
       
    63      * and throw UnsupportedOperationException for asPath.
       
    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_asPath(StandardJavaFileManager fm, List<Path> paths) throws IOException {
       
    70         if (!isGetFileObjectsSupported(fm, paths))
       
    71             return;
       
    72         boolean expectException = (fm instanceof MyStandardJavaFileManager);
       
    73 
       
    74         Set<Path> ref = new HashSet<>(paths);
       
    75         for (JavaFileObject fo : fm.getJavaFileObjectsFromPaths(paths)) {
       
    76             try {
       
    77                 Path path = fm.asPath(fo);
       
    78                 if (expectException)
       
    79                     error("expected exception not thrown: " + UnsupportedOperationException.class.getName());
       
    80                 boolean found = ref.remove(path);
       
    81                 if (!found) {
       
    82                     error("Unexpected path found: " + path + "; expected one of " + ref);
       
    83                 }
       
    84             } catch (Exception e) {
       
    85                 if (expectException && e instanceof UnsupportedOperationException)
       
    86                     continue;
       
    87                 error("unexpected exception thrown: " + e);
       
    88             }
       
    89         }
       
    90     }
       
    91 }