langtools/test/tools/javac/api/file/SJFM_GetFileObjects.java
author jjg
Tue, 06 Jan 2015 14:51:10 -0800
changeset 28332 cd3ea1087d2b
child 30730 d3ce7619db2c
permissions -rw-r--r--
8059977: StandardJavaFileManager should support java.nio.file.Path Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28332
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     1
/*
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     4
 *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     8
 *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    13
 * accompanied this code).
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    14
 *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    18
 *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    21
 * questions.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    22
 */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    23
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    24
/*
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    25
 * @test
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    26
 * @bug 8059977
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    27
 * @summary StandardJavaFileManager should support java.nio.file.Path.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    28
 *          Test getFileObject methods.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    29
 * @build SJFM_TestBase
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    30
 * @run main SJFM_GetFileObjects
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    31
 */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    32
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    33
import java.io.File;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    34
import java.io.IOException;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    35
import java.nio.file.Path;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    36
import java.util.Collections;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    37
import java.util.List;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    39
import javax.tools.StandardJavaFileManager;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    40
import javax.tools.StandardLocation;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    41
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    42
/**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    43
 * For those paths supported by a file manager, verify that the paths
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    44
 * can be encapsulated by file objects, such that the file objects can
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    45
 * be used by a tool such as javac.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    46
 */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    47
public class SJFM_GetFileObjects extends SJFM_TestBase {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    48
    public static void main(String... args) throws Exception {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    49
        new SJFM_GetFileObjects().run();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    50
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    51
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    52
    @Test
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    53
    void test_getJavaFileObjects(StandardJavaFileManager fm) throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    54
        test_getJavaFileObjects(fm, getTestFilePaths());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    55
        test_getJavaFileObjects(fm, getTestZipPaths());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    56
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    57
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    58
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    59
     * Tests the getJavaFileObjects method for a specific file manager
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    60
     * and a series of paths.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    61
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    62
     * Note: instances of MyStandardJavaFileManager only support
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    63
     * encapsulating paths for files in the default file system.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    64
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    65
     * @param fm  the file manager to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    66
     * @param paths  the paths to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    67
     * @throws IOException
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    68
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    69
    void test_getJavaFileObjects(StandardJavaFileManager fm, List<Path> paths) throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    70
        boolean expectException = !isGetFileObjectsSupported(fm, paths);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    71
        try {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    72
            compile(fm.getJavaFileObjects(paths.toArray(new Path[paths.size()])));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    73
            if (expectException)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    74
                error("expected exception not thrown");
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    75
        } catch (RuntimeException e) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    76
            if (expectException && e instanceof IllegalArgumentException)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    77
                return;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    78
            error("unexpected exception thrown: " + e);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    79
        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    80
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    81
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    82
    //----------------------------------------------------------------------------------------------
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    83
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    84
    @Test
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    85
    void test_getJavaFileObjectsFromPaths(StandardJavaFileManager fm) throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    86
        test_getJavaFileObjectsFromPaths(fm, getTestFilePaths());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    87
        test_getJavaFileObjectsFromPaths(fm, getTestZipPaths());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    88
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    89
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    90
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    91
     * Tests the getJavaFileObjectsFromPaths method for a specific file manager
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    92
     * and a series of paths.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    93
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    94
     * Note: instances of MyStandardJavaFileManager only support
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    95
     * encapsulating paths for files in the default file system.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    96
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    97
     * @param fm  the file manager to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    98
     * @param paths  the paths to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    99
     * @throws IOException
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   100
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   101
    void test_getJavaFileObjectsFromPaths(StandardJavaFileManager fm, List<Path> paths)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   102
            throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   103
        boolean expectException = !isGetFileObjectsSupported(fm, paths);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   104
        try {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   105
            compile(fm.getJavaFileObjectsFromPaths(paths));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   106
            if (expectException)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   107
                error("expected exception not thrown: " + IllegalArgumentException.class.getName());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   108
        } catch (RuntimeException e) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   109
            if (expectException && e instanceof IllegalArgumentException)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   110
                return;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   111
            error("unexpected exception thrown: " + e);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   112
        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   113
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   114
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   115
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   116
    //----------------------------------------------------------------------------------------------
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   117
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   118
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   119
     * Compiles a set of files.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   120
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   121
     * @param files the files to be compiled.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   122
     * @throws IOException
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   123
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   124
    void compile(Iterable<? extends JavaFileObject> files) throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   125
        String name = "compile" + (compileCount++);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   126
        try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   127
            File f = new File(name);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   128
            f.mkdirs();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   129
            // use setLocation(Iterable<File>) to avoid relying on setLocationFromPaths
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   130
            fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(f));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   131
            boolean ok = comp.getTask(null, fm, null, null, null, files).call();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   132
            if (!ok)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   133
                error(name + ": compilation failed");
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   134
        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   135
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   136
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   137
    int compileCount;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   138
}