SJFM_IsSameFile.java revision 2774:70d213c84585
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 isSameFile method.
29 * @build SJFM_TestBase
30 * @run main SJFM_IsSameFile
31 */
32
33import java.nio.file.Path;
34import java.util.List;
35import java.util.concurrent.Callable;
36import javax.tools.JavaFileObject;
37import javax.tools.StandardJavaFileManager;
38
39/**
40 * For those paths which are supported by a file manager, such that
41 * a file object can encapsulate the path, verify that the underlying
42 * paths can be compared.
43 */
44public class SJFM_IsSameFile extends SJFM_TestBase {
45    public static void main(String... args) throws Exception {
46        new SJFM_IsSameFile().run();
47    }
48
49    @Test
50    void test_isSameFile(StandardJavaFileManager fm) throws Exception {
51        test_isSameFile(fm, () -> getTestFilePaths());
52        test_isSameFile(fm, () -> getTestZipPaths());
53    }
54
55    /**
56     * Tests the isSameFile method for a specific file manager
57     * and a series of paths.
58     *
59     * Note: instances of MyStandardJavaFileManager only support
60     * encapsulating paths for files in the default file system.
61     *
62     * @param fm  the file manager to be tested
63     * @param paths  a generator for the paths to be tested
64     * @throws IOException
65     */
66    void test_isSameFile(StandardJavaFileManager fm, Callable<List<Path>> paths) throws Exception {
67        if (!isGetFileObjectsSupported(fm, paths.call()))
68            return;
69
70        // use distinct paths and file objects in the following two sets
71        Iterable<? extends JavaFileObject> setA = fm.getJavaFileObjectsFromPaths(paths.call());
72        Iterable<? extends JavaFileObject> setB = fm.getJavaFileObjectsFromPaths(paths.call());
73        for (JavaFileObject a : setA) {
74            for (JavaFileObject b : setB) {
75                System.err.println("compare: a: " + a);
76                System.err.println("         b: " + b);
77                // Use the fileObject getName method to determine the expected result.
78                // For the files being tested, getName is the absolute path.
79                boolean expect = a.getName().equals(b.getName());
80                boolean actual = fm.isSameFile(a, b);
81                if (actual != expect) {
82                    error("mismatch: actual:" + (actual ? "same" : "not same")
83                            + ", expect:" + (expect ? "same" : "not same"));
84                }
85            }
86        }
87    }
88}
89