SJFM_AsPath.java revision 2774:70d213c84585
168723Sru/*
268723Sru * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
368723Sru * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
468723Sru *
568723Sru * This code is free software; you can redistribute it and/or modify it
668723Sru * under the terms of the GNU General Public License version 2 only, as
768723Sru * published by the Free Software Foundation.
868723Sru *
968723Sru * This code is distributed in the hope that it will be useful, but WITHOUT
1068723Sru * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1168723Sru * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1268723Sru * version 2 for more details (a copy is included in the LICENSE file that
1368723Sru * accompanied this code).
1468723Sru *
1568723Sru * You should have received a copy of the GNU General Public License version
1668723Sru * 2 along with this work; if not, write to the Free Software Foundation,
1768723Sru * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1868723Sru *
1968723Sru * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2068723Sru * or visit www.oracle.com if you need additional information or have any
2168723Sru * questions.
2268723Sru */
2368723Sru
2468723Sru/*
2568723Sru * @test
2668723Sru * @bug 8059977
2768723Sru * @summary StandardJavaFileManager should support java.nio.file.Path.
2868723Sru *          Test asPath method.
2968723Sru * @build SJFM_TestBase
3068723Sru * @run main SJFM_AsPath
3168723Sru */
3268723Sru
3368723Sruimport java.io.IOException;
3468723Sruimport java.nio.file.Path;
3568723Sruimport java.util.HashSet;
3668723Sruimport java.util.List;
3768723Sruimport java.util.Set;
3868723Sruimport javax.tools.JavaFileObject;
3968723Sruimport javax.tools.StandardJavaFileManager;
4068723Sru
4178719Sdd/**
42206471Sed * For those paths which are supported by a file manager, such that
4368723Sru * a file object can encapsulate the path, verify that the underlying
4468723Sru * path can be recovered from the file object.
45202205Sed */
4668723Srupublic class SJFM_AsPath extends SJFM_TestBase {
4799800Salfred    public static void main(String... args) throws Exception {
48202205Sed        new SJFM_AsPath().run();
4999800Salfred    }
50222767Sed
5168723Sru    @Test
52222767Sed    void test_asPath(StandardJavaFileManager fm) throws IOException {
53222767Sed        test_asPath(fm, getTestFilePaths());
54222767Sed        test_asPath(fm, getTestZipPaths());
55222767Sed    }
56206471Sed
57222767Sed    /**
58206471Sed     * Tests the asPath method for a specific file manager and a series
59206471Sed     * of paths.
60222767Sed     *
61206471Sed     * Note: instances of MyStandardJavaFileManager only support
62206471Sed     * encapsulating paths for files in the default file system,
63206471Sed     * and throw UnsupportedOperationException for asPath.
64222767Sed     *
65222767Sed     * @param fm  the file manager to be tested
66222767Sed     * @param paths  the paths to be tested
67222767Sed     * @throws IOException
68222767Sed     */
69222767Sed    void test_asPath(StandardJavaFileManager fm, List<Path> paths) throws IOException {
70222767Sed        if (!isGetFileObjectsSupported(fm, paths))
71222767Sed            return;
72222767Sed        boolean expectException = (fm instanceof MyStandardJavaFileManager);
73222767Sed
7468723Sru        Set<Path> ref = new HashSet<>(paths);
75201227Sed        for (JavaFileObject fo : fm.getJavaFileObjectsFromPaths(paths)) {
7668723Sru            try {
77206471Sed                Path path = fm.asPath(fo);
78206471Sed                if (expectException)
7968723Sru                    error("expected exception not thrown: " + UnsupportedOperationException.class.getName());
80222767Sed                boolean found = ref.remove(path);
81222767Sed                if (!found) {
82222767Sed                    error("Unexpected path found: " + path + "; expected one of " + ref);
83222767Sed                }
84222767Sed            } catch (Exception e) {
85222767Sed                if (expectException && e instanceof UnsupportedOperationException)
86222767Sed                    continue;
87222767Sed                error("unexpected exception thrown: " + e);
88222767Sed            }
89222767Sed        }
90222767Sed    }
91222767Sed}
92222767Sed