test/tools/javac/api/T6397104.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial