1 /*
2  * Copyright (c) 2014, 2016, 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  */
23 
24 /*
25  * @test
26  * @bug 8037085
27  * @summary Ensures that sjavac can handle various exclusion patterns.
28  *
29  * @modules jdk.compiler/com.sun.tools.javac.main
30  *          jdk.compiler/com.sun.tools.sjavac
31  *          jdk.compiler/com.sun.tools.sjavac.server
32  * @library /tools/lib
33  * @build Wrapper toolbox.ToolBox toolbox.Assert
34  * @run main Wrapper IncludeExcludePatterns
35  */
36 
37 import java.io.File;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.HashSet;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
48 
49 import com.sun.tools.javac.main.Main.Result;
50 
51 import toolbox.Assert;
52 
53 public class IncludeExcludePatterns extends SjavacBase {
54 
55     final Path SRC = Paths.get("src");
56     final Path BIN = Paths.get("bin");
57     final Path STATE_DIR = Paths.get("state-dir");
58 
59     // An arbitrarily but sufficiently complicated source tree.
60     final Path A = Paths.get("pkga/A.java");
61     final Path X1 = Paths.get("pkga/subpkg/Xx.java");
62     final Path Y = Paths.get("pkga/subpkg/subsubpkg/Y.java");
63     final Path B = Paths.get("pkgb/B.java");
64     final Path C = Paths.get("pkgc/C.java");
65     final Path X2 = Paths.get("pkgc/Xx.java");
66 
67     final Path[] ALL_PATHS = {A, X1, Y, B, C, X2};
68 
main(String[] ignore)69     public static void main(String[] ignore) throws Exception {
70         new IncludeExcludePatterns().runTest();
71     }
72 
runTest()73     public void runTest() throws IOException, ReflectiveOperationException {
74         Files.createDirectories(BIN);
75         Files.createDirectories(STATE_DIR);
76         for (Path p : ALL_PATHS) {
77             writeDummyClass(p);
78         }
79 
80         // Single file
81         testPattern("pkga/A.java", A);
82 
83         // Leading wild cards
84         testPattern("*/A.java", A);
85         testPattern("**/Xx.java", X1, X2);
86         testPattern("**x.java", X1, X2);
87 
88         // Wild card in middle of path
89         testPattern("pkga/*/Xx.java", X1);
90         testPattern("pkga/**/Y.java", Y);
91 
92         // Trailing wild cards
93         testPattern("pkga/*", A);
94         testPattern("pkga/**", A, X1, Y);
95 
96         // Multiple wildcards
97         testPattern("pkga/*/*/Y.java", Y);
98         testPattern("**/*/**", X1, Y);
99 
100     }
101 
102     // Given "src/pkg/subpkg/A.java" this method returns "A"
classNameOf(Path javaFile)103     String classNameOf(Path javaFile) {
104         return javaFile.getFileName()
105                        .toString()
106                        .replace(".java", "");
107     }
108 
109     // Puts an empty (dummy) class definition in the given path.
writeDummyClass(Path javaFile)110     void writeDummyClass(Path javaFile) throws IOException {
111         String pkg = javaFile.getParent().toString().replace(File.separatorChar, '.');
112         String cls = javaFile.getFileName().toString().replace(".java", "");
113         toolbox.writeFile(SRC.resolve(javaFile), "package " + pkg + "; class " + cls + " {}");
114     }
115 
testPattern(String filterArgs, Path... sourcesExpectedToBeVisible)116     void testPattern(String filterArgs, Path... sourcesExpectedToBeVisible)
117             throws ReflectiveOperationException, IOException {
118         testFilter("-i " + filterArgs, Arrays.asList(sourcesExpectedToBeVisible));
119 
120         Set<Path> complement = new HashSet<>(Arrays.asList(ALL_PATHS));
121         complement.removeAll(Arrays.asList(sourcesExpectedToBeVisible));
122         testFilter("-x " + filterArgs, complement);
123     }
124 
testFilter(String filterArgs, Collection<Path> sourcesExpectedToBeVisible)125     void testFilter(String filterArgs, Collection<Path> sourcesExpectedToBeVisible)
126             throws IOException, ReflectiveOperationException {
127         System.out.println("Testing filter: " + filterArgs);
128         toolbox.cleanDirectory(BIN);
129         toolbox.cleanDirectory(STATE_DIR);
130         String args = filterArgs + " " + SRC
131                 + " -d " + BIN
132                 + " --state-dir=" + STATE_DIR;
133         int rc = compile((Object[]) args.split(" "));
134 
135         // Compilation should always pass in these tests
136         Assert.check(rc == Result.OK.exitCode, "Compilation failed unexpectedly.");
137 
138         // The resulting .class files should correspond to the visible source files
139         Set<Path> result = allFilesInDir(BIN);
140         Set<Path> expected = correspondingClassFiles(sourcesExpectedToBeVisible);
141         if (!result.equals(expected)) {
142             System.out.println("Result:");
143             printPaths(result);
144             System.out.println("Expected:");
145             printPaths(expected);
146             Assert.error("Test case failed: " + filterArgs);
147         }
148     }
149 
printPaths(Collection<Path> paths)150     void printPaths(Collection<Path> paths) {
151         paths.stream()
152              .sorted()
153              .forEachOrdered(p -> System.out.println("    " + p));
154     }
155 
156     // Given "pkg/A.java, pkg/B.java" this method returns "bin/pkg/A.class, bin/pkg/B.class"
correspondingClassFiles(Collection<Path> javaFiles)157     Set<Path> correspondingClassFiles(Collection<Path> javaFiles) {
158         return javaFiles.stream()
159                         .map(javaFile -> javaFile.resolveSibling(classNameOf(javaFile) + ".class"))
160                         .map(BIN::resolve)
161                         .collect(Collectors.toSet());
162     }
163 
allFilesInDir(Path p)164     Set<Path> allFilesInDir(Path p) throws IOException {
165         try (Stream<Path> files = Files.walk(p).filter(Files::isRegularFile)) {
166             return files.collect(Collectors.toSet());
167         }
168     }
169 }
170