T6397104.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2006, 2016, 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     6397104
27 * @summary JSR 199: JavaFileManager.getFileForOutput should have sibling argument
28 * @author  Peter von der Ah\u00e9
29 * @modules java.compiler
30 *          jdk.compiler
31 */
32
33import java.io.File;
34import java.net.URI;
35import java.util.Arrays;
36import javax.tools.*;
37import javax.tools.JavaFileManager.Location;
38
39import static javax.tools.StandardLocation.CLASS_OUTPUT;
40
41public class T6397104 {
42
43    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
44
45    void test(StandardJavaFileManager fm,
46              Location location,
47              File siblingFile,
48              String relName,
49              String expectedPath)
50        throws Exception
51    {
52        JavaFileObject sibling = siblingFile == null
53            ? null
54            : fm.getJavaFileObjectsFromFiles(Arrays.asList(siblingFile)).iterator().next();
55        FileObject fileObject =
56            fm.getFileForOutput(location, "java.lang", relName, sibling);
57
58        File expectedFile = new File(expectedPath).getCanonicalFile();
59        File fileObjectFile = new File(fileObject.toUri()).getCanonicalFile();
60
61        if (!fileObjectFile.equals(expectedFile))
62            throw new AssertionError("Expected " + expectedFile +
63                                     ", got " + fileObjectFile);
64        System.err.format("OK: (%s, %s) => %s%n", siblingFile, relName, fileObjectFile);
65    }
66
67    void test(boolean hasLocation, File siblingFile, String relName, String expectedPath)
68        throws Exception
69    {
70        System.err.format("test: hasLocation:%s, siblingFile:%s, relName:%s, expectedPath:%s%n",
71                hasLocation, siblingFile, relName, expectedPath);
72        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
73            if (hasLocation) {
74                for (Location location : StandardLocation.values()) {
75                    System.err.format("  location:%s, moduleLocn:%b%n",
76                        location, location.isModuleLocation());
77                    if (location.isModuleLocation()) {
78                        continue;
79                    }
80                    fm.setLocation(location, Arrays.asList(new File(".")));
81                    test(fm, location, siblingFile, relName, expectedPath);
82                }
83            } else {
84                test(fm, CLASS_OUTPUT, siblingFile, relName, expectedPath);
85            }
86        }
87    }
88
89    public static void main(String... args) throws Exception {
90        T6397104 tester = new T6397104();
91        tester.test(false,
92                    new File(new File("foo", "bar"), "baz.java"),
93                    "qux/baz.xml",
94                    "foo/bar/baz.xml");
95        tester.test(false,
96                    null,
97                    "qux/baz.xml",
98                    "baz.xml"); // sb "java/lang/qux/baz.xml"
99        tester.test(true,
100                    new File(new File("foo", "bar"), "baz.java"),
101                    "qux/baz.xml",
102                    "./java/lang/qux/baz.xml");
103        tester.test(true,
104                    null,
105                    "qux/baz.xml",
106                    "./java/lang/qux/baz.xml");
107    }
108
109}
110