Path: blob/master/test/langtools/tools/sjavac/IncludeExcludePatterns.java
41144 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 803708526* @summary Ensures that sjavac can handle various exclusion patterns.27*28* @modules jdk.compiler/com.sun.tools.javac.main29* jdk.compiler/com.sun.tools.sjavac30* jdk.compiler/com.sun.tools.sjavac.server31* @library /tools/lib32* @build Wrapper toolbox.ToolBox toolbox.Assert33* @run main Wrapper IncludeExcludePatterns34*/3536import java.io.File;37import java.io.IOException;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.util.Arrays;42import java.util.Collection;43import java.util.HashSet;44import java.util.Set;45import java.util.stream.Collectors;46import java.util.stream.Stream;4748import com.sun.tools.javac.main.Main.Result;4950import toolbox.Assert;5152public class IncludeExcludePatterns extends SjavacBase {5354final Path SRC = Paths.get("src");55final Path BIN = Paths.get("bin");56final Path STATE_DIR = Paths.get("state-dir");5758// An arbitrarily but sufficiently complicated source tree.59final Path A = Paths.get("pkga/A.java");60final Path X1 = Paths.get("pkga/subpkg/Xx.java");61final Path Y = Paths.get("pkga/subpkg/subsubpkg/Y.java");62final Path B = Paths.get("pkgb/B.java");63final Path C = Paths.get("pkgc/C.java");64final Path X2 = Paths.get("pkgc/Xx.java");6566final Path[] ALL_PATHS = {A, X1, Y, B, C, X2};6768public static void main(String[] ignore) throws Exception {69new IncludeExcludePatterns().runTest();70}7172public void runTest() throws IOException, ReflectiveOperationException {73Files.createDirectories(BIN);74Files.createDirectories(STATE_DIR);75for (Path p : ALL_PATHS) {76writeDummyClass(p);77}7879// Single file80testPattern("pkga/A.java", A);8182// Leading wild cards83testPattern("*/A.java", A);84testPattern("**/Xx.java", X1, X2);85testPattern("**x.java", X1, X2);8687// Wild card in middle of path88testPattern("pkga/*/Xx.java", X1);89testPattern("pkga/**/Y.java", Y);9091// Trailing wild cards92testPattern("pkga/*", A);93testPattern("pkga/**", A, X1, Y);9495// Multiple wildcards96testPattern("pkga/*/*/Y.java", Y);97testPattern("**/*/**", X1, Y);9899}100101// Given "src/pkg/subpkg/A.java" this method returns "A"102String classNameOf(Path javaFile) {103return javaFile.getFileName()104.toString()105.replace(".java", "");106}107108// Puts an empty (dummy) class definition in the given path.109void writeDummyClass(Path javaFile) throws IOException {110String pkg = javaFile.getParent().toString().replace(File.separatorChar, '.');111String cls = javaFile.getFileName().toString().replace(".java", "");112toolbox.writeFile(SRC.resolve(javaFile), "package " + pkg + "; class " + cls + " {}");113}114115void testPattern(String filterArgs, Path... sourcesExpectedToBeVisible)116throws ReflectiveOperationException, IOException {117testFilter("-i " + filterArgs, Arrays.asList(sourcesExpectedToBeVisible));118119Set<Path> complement = new HashSet<>(Arrays.asList(ALL_PATHS));120complement.removeAll(Arrays.asList(sourcesExpectedToBeVisible));121testFilter("-x " + filterArgs, complement);122}123124void testFilter(String filterArgs, Collection<Path> sourcesExpectedToBeVisible)125throws IOException, ReflectiveOperationException {126System.out.println("Testing filter: " + filterArgs);127toolbox.cleanDirectory(BIN);128toolbox.cleanDirectory(STATE_DIR);129String args = filterArgs + " " + SRC130+ " -d " + BIN131+ " --state-dir=" + STATE_DIR;132int rc = compile((Object[]) args.split(" "));133134// Compilation should always pass in these tests135Assert.check(rc == Result.OK.exitCode, "Compilation failed unexpectedly.");136137// The resulting .class files should correspond to the visible source files138Set<Path> result = allFilesInDir(BIN);139Set<Path> expected = correspondingClassFiles(sourcesExpectedToBeVisible);140if (!result.equals(expected)) {141System.out.println("Result:");142printPaths(result);143System.out.println("Expected:");144printPaths(expected);145Assert.error("Test case failed: " + filterArgs);146}147}148149void printPaths(Collection<Path> paths) {150paths.stream()151.sorted()152.forEachOrdered(p -> System.out.println(" " + p));153}154155// Given "pkg/A.java, pkg/B.java" this method returns "bin/pkg/A.class, bin/pkg/B.class"156Set<Path> correspondingClassFiles(Collection<Path> javaFiles) {157return javaFiles.stream()158.map(javaFile -> javaFile.resolveSibling(classNameOf(javaFile) + ".class"))159.map(BIN::resolve)160.collect(Collectors.toSet());161}162163Set<Path> allFilesInDir(Path p) throws IOException {164try (Stream<Path> files = Files.walk(p).filter(Files::isRegularFile)) {165return files.collect(Collectors.toSet());166}167}168}169170171