IncludeExcludePatterns.java revision 3573:c4a18ee691c4
125184Sjkh/*
225184Sjkh * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
350357Ssheldonh * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
425184Sjkh *
525184Sjkh * This code is free software; you can redistribute it and/or modify it
625184Sjkh * under the terms of the GNU General Public License version 2 only, as
725184Sjkh * published by the Free Software Foundation.
825184Sjkh *
925184Sjkh * This code is distributed in the hope that it will be useful, but WITHOUT
1025184Sjkh * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1125184Sjkh * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1225184Sjkh * version 2 for more details (a copy is included in the LICENSE file that
1325184Sjkh * accompanied this code).
1425184Sjkh *
1525184Sjkh * You should have received a copy of the GNU General Public License version
1625184Sjkh * 2 along with this work; if not, write to the Free Software Foundation,
1750357Ssheldonh * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1825184Sjkh *
1925184Sjkh * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2025184Sjkh * or visit www.oracle.com if you need additional information or have any
2125184Sjkh * questions.
2250357Ssheldonh */
2350357Ssheldonh
2425184Sjkh/*
2525184Sjkh * @test
2625184Sjkh * @bug 8037085
2725184Sjkh * @summary Ensures that sjavac can handle various exclusion patterns.
2840006Sphk *
2950357Ssheldonh * @modules jdk.compiler/com.sun.tools.javac.main
3040006Sphk *          jdk.compiler/com.sun.tools.sjavac
3140006Sphk *          jdk.compiler/com.sun.tools.sjavac.server
3240006Sphk * @library /tools/lib
3340006Sphk * @build Wrapper toolbox.ToolBox toolbox.Assert
3442621Shm * @run main Wrapper IncludeExcludePatterns
3550357Ssheldonh */
3642621Shm
3742621Shmimport java.io.File;
3842621Shmimport java.io.IOException;
3942627Sjoergimport java.nio.file.Files;
4042627Sjoergimport java.nio.file.Path;
4142627Sjoergimport java.nio.file.Paths;
4242627Sjoergimport java.util.Arrays;
4342627Sjoergimport java.util.Collection;
4442627Sjoergimport java.util.HashSet;
4542627Sjoergimport java.util.Set;
4642627Sjoergimport java.util.stream.Collectors;
4742627Sjoergimport java.util.stream.Stream;
4842627Sjoerg
4942627Sjoergimport com.sun.tools.javac.main.Main.Result;
5042627Sjoerg
5142627Sjoergimport toolbox.Assert;
5242627Sjoerg
5325184Sjkhpublic class IncludeExcludePatterns extends SjavacBase {
5450357Ssheldonh
5548687Speter    final Path SRC = Paths.get("src");
5648687Speter    final Path BIN = Paths.get("bin");
5748687Speter    final Path STATE_DIR = Paths.get("state-dir");
5848662Speter
5925184Sjkh    // An arbitrarily but sufficiently complicated source tree.
6033682Sbrian    final Path A = Paths.get("pkga/A.java");
6148662Speter    final Path X1 = Paths.get("pkga/subpkg/Xx.java");
6225184Sjkh    final Path Y = Paths.get("pkga/subpkg/subsubpkg/Y.java");
6325184Sjkh    final Path B = Paths.get("pkgb/B.java");
6425184Sjkh    final Path C = Paths.get("pkgc/C.java");
6525184Sjkh    final Path X2 = Paths.get("pkgc/Xx.java");
6648842Sjkh
6750357Ssheldonh    final Path[] ALL_PATHS = {A, X1, Y, B, C, X2};
6848842Sjkh
6948842Sjkh    public static void main(String[] ignore) throws Exception {
7048842Sjkh        new IncludeExcludePatterns().runTest();
7148842Sjkh    }
7248662Speter
7325184Sjkh    public void runTest() throws IOException, ReflectiveOperationException {
7425184Sjkh        Files.createDirectories(BIN);
7525184Sjkh        Files.createDirectories(STATE_DIR);
7625184Sjkh        for (Path p : ALL_PATHS) {
7725184Sjkh            writeDummyClass(p);
7825184Sjkh        }
7925184Sjkh
8025184Sjkh        // Single file
8148662Speter        testPattern("pkga/A.java", A);
8225184Sjkh
8325184Sjkh        // Leading wild cards
8425184Sjkh        testPattern("*/A.java", A);
8525184Sjkh        testPattern("**/Xx.java", X1, X2);
8625184Sjkh        testPattern("**x.java", X1, X2);
8725184Sjkh
8825184Sjkh        // Wild card in middle of path
8925184Sjkh        testPattern("pkga/*/Xx.java", X1);
9025184Sjkh        testPattern("pkga/**/Y.java", Y);
9148662Speter
9225184Sjkh        // Trailing wild cards
9348662Speter        testPattern("pkga/*", A);
9448662Speter        testPattern("pkga/**", A, X1, Y);
9548662Speter
9648662Speter        // Multiple wildcards
9725184Sjkh        testPattern("pkga/*/*/Y.java", Y);
9829300Sdanny        testPattern("**/*/**", X1, Y);
9949122Sbrian
10050357Ssheldonh    }
10149122Sbrian
10250357Ssheldonh    // Given "src/pkg/subpkg/A.java" this method returns "A"
10350357Ssheldonh    String classNameOf(Path javaFile) {
10449122Sbrian        return javaFile.getFileName()
10549122Sbrian                       .toString()
10649122Sbrian                       .replace(".java", "");
10749122Sbrian    }
10849122Sbrian
10950357Ssheldonh    // Puts an empty (dummy) class definition in the given path.
11050193Sbrian    void writeDummyClass(Path javaFile) throws IOException {
11149122Sbrian        String pkg = javaFile.getParent().toString().replace(File.separatorChar, '.');
11249122Sbrian        String cls = javaFile.getFileName().toString().replace(".java", "");
11350063Sbrian        toolbox.writeFile(SRC.resolve(javaFile), "package " + pkg + "; class " + cls + " {}");
11449122Sbrian    }
11549122Sbrian
11629300Sdanny    void testPattern(String filterArgs, Path... sourcesExpectedToBeVisible)
11729300Sdanny            throws ReflectiveOperationException, IOException {
11829300Sdanny        testFilter("-i " + filterArgs, Arrays.asList(sourcesExpectedToBeVisible));
11932382Salex
12032382Salex        Set<Path> complement = new HashSet<>(Arrays.asList(ALL_PATHS));
12132382Salex        complement.removeAll(Arrays.asList(sourcesExpectedToBeVisible));
12229300Sdanny        testFilter("-x " + filterArgs, complement);
12329300Sdanny    }
12429300Sdanny
12550357Ssheldonh    void testFilter(String filterArgs, Collection<Path> sourcesExpectedToBeVisible)
12641077Speter            throws IOException, ReflectiveOperationException {
12729300Sdanny        System.out.println("Testing filter: " + filterArgs);
12829300Sdanny        toolbox.cleanDirectory(BIN);
12929300Sdanny        toolbox.cleanDirectory(STATE_DIR);
13029300Sdanny        String args = filterArgs + " " + SRC
13129300Sdanny                + " -d " + BIN
13229300Sdanny                + " --state-dir=" + STATE_DIR;
13329300Sdanny        int rc = compile((Object[]) args.split(" "));
13429300Sdanny
13550357Ssheldonh        // Compilation should always pass in these tests
13645542Sdes        Assert.check(rc == Result.OK.exitCode, "Compilation failed unexpectedly.");
13745542Sdes
13845542Sdes        // The resulting .class files should correspond to the visible source files
13950357Ssheldonh        Set<Path> result = allFilesInDir(BIN);
14045542Sdes        Set<Path> expected = correspondingClassFiles(sourcesExpectedToBeVisible);
14145622Sbrian        if (!result.equals(expected)) {
14244992Sbrian            System.out.println("Result:");
14344992Sbrian            printPaths(result);
14450357Ssheldonh            System.out.println("Expected:");
14544992Sbrian            printPaths(expected);
14644992Sbrian            Assert.error("Test case failed: " + filterArgs);
14744992Sbrian        }
14844992Sbrian    }
14944992Sbrian
15044992Sbrian    void printPaths(Collection<Path> paths) {
15144992Sbrian        paths.stream()
15244992Sbrian             .sorted()
15344992Sbrian             .forEachOrdered(p -> System.out.println("    " + p));
15429300Sdanny    }
15533337Salex
15650357Ssheldonh    // Given "pkg/A.java, pkg/B.java" this method returns "bin/pkg/A.class, bin/pkg/B.class"
15733149Salex    Set<Path> correspondingClassFiles(Collection<Path> javaFiles) {
15833149Salex        return javaFiles.stream()
15933149Salex                        .map(javaFile -> javaFile.resolveSibling(classNameOf(javaFile) + ".class"))
16033149Salex                        .map(BIN::resolve)
16129300Sdanny                        .collect(Collectors.toSet());
16225184Sjkh    }
16325184Sjkh
16440006Sphk    Set<Path> allFilesInDir(Path p) throws IOException {
16540006Sphk        try (Stream<Path> files = Files.walk(p).filter(Files::isRegularFile)) {
16640006Sphk            return files.collect(Collectors.toSet());
16740006Sphk        }
16840006Sphk    }
16929300Sdanny}
17029300Sdanny