langtools/test/tools/javac/api/file/SJFM_TestBase.java
author jjg
Tue, 06 Jan 2015 14:51:10 -0800
changeset 28332 cd3ea1087d2b
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
import java.io.IOException;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    25
import java.io.OutputStream;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    26
import java.io.UncheckedIOException;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    27
import java.lang.annotation.Annotation;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    28
import java.lang.annotation.Retention;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    29
import java.lang.annotation.RetentionPolicy;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    31
import java.lang.reflect.Method;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    32
import java.nio.file.FileSystem;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    33
import java.nio.file.FileSystems;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    34
import java.nio.file.Files;
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.nio.file.Paths;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    37
import java.util.ArrayList;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    38
import java.util.Arrays;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    39
import java.util.List;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    40
import java.util.stream.Collectors;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    41
import java.util.zip.ZipEntry;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    42
import java.util.zip.ZipOutputStream;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    43
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    44
import javax.tools.JavaCompiler;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    45
import javax.tools.StandardJavaFileManager;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    46
import javax.tools.ToolProvider;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    47
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    48
/**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    49
 * Base class for unit tests for StandardJavaFileManager.
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
class SJFM_TestBase {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    52
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    53
    /** Shared compiler instance. */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    54
    JavaCompiler comp;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    55
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    56
    /** A list of items to be closed when the test is complete. */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    57
    List<AutoCloseable> closeables;
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
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    60
     * Runs a test. This is the primary entry point and should generally be
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    61
     * called from each test's main method.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    62
     * It calls all methods annotated with {@code @Test} with the instances
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    63
     * of StandardJavaFileManager to be tested.
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
     * @throws Exception if the test fails.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    66
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    67
    void run() throws Exception {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    68
        comp = ToolProvider.getSystemJavaCompiler();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    69
        closeables = new ArrayList<>();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    70
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    71
        try (StandardJavaFileManager systemFileManager = comp.getStandardFileManager(null, null, null);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    72
                StandardJavaFileManager customFileManager = new MyStandardJavaFileManager(systemFileManager)) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    73
            test(systemFileManager);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    74
            test(customFileManager);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    75
        } finally {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    76
            for (AutoCloseable c: closeables) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    77
                try {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    78
                    c.close();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    79
                } catch (IOException e) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    80
                    error("Exception closing " + c + ": " + e);
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
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    85
        if (errors > 0)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    86
            throw new Exception(errors + " errors occurred");
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    87
    }
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
     * Get the file managers to be tested.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    91
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    92
     * Currently, two are provided:
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    93
     * <ol>
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    94
     * <li>the system-provided file manager
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    95
     * <li>a custom file manager, which relies on the default methods provided in the
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    96
     *     StandardJavaFileManager interface
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    97
     * </li>
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    98
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
    99
     * @return the file managers to be tested
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
    List<StandardJavaFileManager> getTestFileManagers() {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   102
        StandardJavaFileManager systemFileManager = comp.getStandardFileManager(null, null, null);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   103
        StandardJavaFileManager customFileManager = new MyStandardJavaFileManager(systemFileManager);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   104
        return Arrays.asList(systemFileManager, customFileManager);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   105
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   106
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   107
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   108
     * Tests a specific file manager, by calling all methods annotated
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   109
     * with {@code @Test} passing this file manager as an argument.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   110
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   111
     * @param fm the file manager to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   112
     * @throws Exception if the test fails
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
    void test(StandardJavaFileManager fm) throws Exception {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   115
        System.err.println("Testing " + fm);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   116
        for (Method m: getClass().getDeclaredMethods()) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   117
            Annotation a = m.getAnnotation(Test.class);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   118
            if (a != null) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   119
                try {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   120
                    System.err.println("Test " + m.getName());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   121
                    m.invoke(this, new Object[] { fm });
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   122
                } catch (InvocationTargetException e) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   123
                    Throwable cause = e.getCause();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   124
                    throw (cause instanceof Exception) ? ((Exception) cause) : e;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   125
                }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   126
                System.err.println();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   127
            }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   128
        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   129
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   130
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   131
    /** Marker annotation for test cases. */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   132
    @Retention(RetentionPolicy.RUNTIME)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   133
    @interface Test { }
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
     * Returns a series of paths for artifacts in the default file system.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   137
     * The paths are for the .java files in the test.src directory.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   138
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   139
     * @return a list of paths
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   140
     * @throws IOException
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   141
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   142
    List<Path> getTestFilePaths() throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   143
        String testSrc = System.getProperty("test.src");
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   144
        return Files.list(Paths.get(testSrc))
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   145
                .filter(p -> p.getFileName().toString().endsWith(".java"))
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   146
                .collect(Collectors.toList());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   147
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   148
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   149
    private FileSystem zipfs;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   150
    private List<Path> zipPaths;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   151
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   152
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   153
     * Returns a series of paths for artifacts in a non-default file system.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   154
     * A zip file is created containing copies of the .java files in the
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   155
     * test.src directory. The paths that are returned refer to these files.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   156
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   157
     * @return a list of paths
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   158
     * @throws IOException
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   159
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   160
    List<Path> getTestZipPaths() throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   161
        if (zipfs == null) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   162
            Path testZip = createSourceZip();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   163
            zipfs = FileSystems.newFileSystem(testZip, null);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   164
            closeables.add(zipfs);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   165
            zipPaths = Files.list(zipfs.getRootDirectories().iterator().next())
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   166
                .filter(p -> p.getFileName().toString().endsWith(".java"))
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   167
                .collect(Collectors.toList());
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   168
        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   169
        return zipPaths;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   170
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   171
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   172
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   173
     * Create a zip file containing the contents of the test.src directory.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   174
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   175
     * @return a path for the zip file.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   176
     * @throws IOException if there is a problem creating the file
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   177
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   178
    private Path createSourceZip() throws IOException {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   179
        Path testSrc = Paths.get(System.getProperty("test.src"));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   180
        Path testZip = Paths.get("test.zip");
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   181
        try (OutputStream os = Files.newOutputStream(testZip)) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   182
            try (ZipOutputStream zos = new ZipOutputStream(os)) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   183
                Files.list(testSrc)
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   184
                    .filter(p -> p.getFileName().toString().endsWith(".java"))
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   185
                    .forEach(p -> {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   186
                        try {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   187
                            zos.putNextEntry(new ZipEntry(p.getFileName().toString()));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   188
                            zos.write(Files.readAllBytes(p));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   189
                            zos.closeEntry();
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   190
                        } catch (IOException ex) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   191
                            throw new UncheckedIOException(ex);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   192
                        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   193
                    });
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   194
            }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   195
        }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   196
        return testZip;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   197
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   198
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   199
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   200
     * Tests whether it is expected that a file manager will be able
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   201
     * to create a series of file objects from a series of paths.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   202
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   203
     * MyStandardJavaFileManager does not support paths referring to
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   204
     * non-default file systems.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   205
     *
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   206
     * @param fm  the file manager to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   207
     * @param paths  the paths to be tested
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   208
     * @return
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   209
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   210
    boolean isGetFileObjectsSupported(StandardJavaFileManager fm, List<Path> paths) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   211
        return !(fm instanceof MyStandardJavaFileManager
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   212
                && (paths.get(0).getFileSystem() != FileSystems.getDefault()));
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   213
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   214
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   215
    /**
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   216
     * Report an error.
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   217
     */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   218
    void error(String msg) {
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   219
        System.err.println("Error: " + msg);
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   220
        errors++;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   221
    }
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   222
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   223
    /** Count of errors reported. */
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   224
    int errors;
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   225
cd3ea1087d2b 8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
diff changeset
   226
}