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 7026941
27 * @summary path options ignored when reusing filemanager across tasks
28 * @modules java.compiler
29 *          jdk.compiler
30 */
31
32import java.io.File;
33import java.io.FileWriter;
34import java.io.IOException;
35import java.io.PrintWriter;
36import java.net.URI;
37import java.nio.file.Files;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.Collections;
41import java.util.EnumSet;
42import java.util.List;
43import java.util.Objects;
44import java.util.jar.JarEntry;
45import java.util.jar.JarOutputStream;
46import java.util.regex.Matcher;
47import java.util.regex.Pattern;
48
49import javax.tools.JavaCompiler;
50import javax.tools.JavaCompiler.CompilationTask;
51import javax.tools.JavaFileObject;
52import javax.tools.SimpleJavaFileObject;
53import javax.tools.StandardJavaFileManager;
54import javax.tools.StandardLocation;
55import javax.tools.ToolProvider;
56
57import static javax.tools.StandardLocation.*;
58
59/**
60 * Test for combinations of using javac command-line options and fileManager setLocation
61 * calls to affect the locations available in the fileManager.
62 *
63 * Using a single Java compiler and file manager, for each of the standard locations,
64 * a series of operations is performed, using either compiler options or setLocation
65 * calls. Each operation includes a compilation, and then a check for the value of
66 * the standard location available in the file manager.
67 *
68 * The operations generate and use unique files to minimize the possibility of false
69 * positive results.
70 */
71public class TestSearchPaths {
72
73    public static void main(String... args) throws Exception {
74        TestSearchPaths t = new TestSearchPaths();
75        t.run();
76    }
77
78    void run() throws Exception {
79        compiler = ToolProvider.getSystemJavaCompiler();
80        fileManager = compiler.getStandardFileManager(null, null, null);
81        try {
82            // basic output path
83            testClassOutput();
84
85            // basic search paths
86            testClassPath();
87            testSourcePath();
88            testPlatformClassPath();
89
90            // annotation processing
91            testAnnotationProcessorPath();
92            testSourceOutput();
93
94            // javah equivalent
95            testNativeHeaderOutput();
96
97            // future-proof: guard against new StandardLocations being added
98            if (!tested.equals(EnumSet.allOf(StandardLocation.class))) {
99                // FIXME: need to update for JDK 9 locations
100                // error("not all standard locations have been tested");
101                out.println("not yet tested: " + EnumSet.complementOf(tested));
102            }
103
104            if (errors > 0) {
105                throw new Exception(errors + " errors occurred");
106            }
107        } finally {
108            fileManager.close();
109        }
110    }
111
112    void testClassOutput() throws IOException {
113        String test = "testClassOutput";
114        System.err.println("test: " + test);
115
116        for (int i = 1; i <= 5; i++) {
117            File classes = createDir(test + "/" + i + "/classes");
118            List<String> options;
119            switch (i) {
120                default:
121                    options = getOptions("-d", classes.getPath());
122                    break;
123
124                case 3:
125                    setLocation(CLASS_OUTPUT, classes);
126                    options = null;
127                    break;
128            }
129            List<JavaFileObject> sources = getSources("class C" + i + " { }");
130            callTask(options, sources);
131            checkPath(CLASS_OUTPUT, Mode.EQUALS, classes);
132            checkFile(CLASS_OUTPUT, "C" + i + ".class");
133        }
134
135        tested.add(CLASS_OUTPUT);
136    }
137
138    void testClassPath() throws IOException {
139        String test = "testClassPath";
140        System.err.println("test: " + test);
141
142        for (int i = 1; i <= 5; i++) {
143            File classes = createDir(test + "/" + i + "/classes");
144            File classpath = new File("testClassOutput/" + i + "/classes");
145            List<String> options;
146            switch (i) {
147                default:
148                    options = getOptions("-d", classes.getPath(), "-classpath", classpath.getPath());
149                    break;
150
151                case 3:
152                    setLocation(CLASS_PATH, classpath);
153                    options = getOptions("-d", classes.getPath());
154                    break;
155
156                case 4:
157                    options = getOptions("-d", classes.getPath(), "-cp", classpath.getPath());
158                    break;
159            }
160            List<JavaFileObject> sources = getSources("class D" + i + " { C" + i + " c; }");
161            callTask(options, sources);
162            checkPath(CLASS_PATH, Mode.EQUALS, classpath);
163            checkFile(CLASS_OUTPUT, "D" + i + ".class");
164        }
165
166        tested.add(CLASS_PATH);
167        System.err.println();
168    }
169
170    void testSourcePath() throws IOException {
171        String test = "testSourcePath";
172        System.err.println("test: " + test);
173        setLocation(CLASS_PATH); // empty
174
175        for (int i = 1; i <= 5; i++) {
176            File src = createDir(test + "/" + i + "/src");
177            writeFile(src, "C" + i + ".java", "class C" + i + "{ }");
178            File classes = createDir(test + "/" + i + "/classes");
179            File srcpath = src;
180            List<String> options;
181            switch (i) {
182                default:
183                    options = getOptions("-d", classes.getPath(), "-sourcepath", srcpath.getPath());
184                    break;
185
186                case 3:
187                    setLocation(SOURCE_PATH, srcpath);
188                    options = getOptions("-d", classes.getPath());
189                    break;
190            }
191            List<JavaFileObject> sources = getSources("class D" + i + " { C" + i + " c; }");
192            callTask(options, sources);
193            checkPath(SOURCE_PATH, Mode.EQUALS, srcpath);
194            checkFile(CLASS_OUTPUT, "D" + i + ".class");
195        }
196
197        tested.add(SOURCE_PATH);
198        System.err.println();
199    }
200
201    void testPlatformClassPath() throws IOException {
202        String test = "testPlatformClassPath";
203        System.err.println("test: " + test);
204
205        List<File> defaultPath = getLocation(PLATFORM_CLASS_PATH);
206        StringBuilder sb = new StringBuilder();
207        for (File f: defaultPath) {
208            if (sb.length() > 0)
209                sb.append(File.pathSeparator);
210            sb.append(f);
211        }
212        String defaultPathString = sb.toString();
213
214        setLocation(CLASS_PATH); // empty
215        setLocation(SOURCE_PATH); // empty
216
217        // Use -source 8 -target 8 to enable use of platform class path options
218        // FIXME: temporarily exclude cases referring to default bootclasspath
219        // for (int i = 1; i <= 10; i++) {
220        int[] cases = new int[] { 1, 2, 4, 5, 6, 7 };
221        for (int i : cases) {
222            File classes = createDir(test + "/" + i + "/classes");
223            File testJars = createDir(test + "/" + i + "/testJars");
224            File testClasses = createDir(test + "/" + i + "/testClasses");
225            callTask(getOptions("-d", testClasses.getPath()), getSources("class C" + i + " { }"));
226
227            List<String> options;
228            Mode mode;
229            List<File> match;
230            String reference = "C" + i + " c;";
231
232            File jar;
233
234            switch (i) {
235                case 1:
236                    options = getOptions("-d", classes.getPath(),
237                        "-source", "8", "-target", "8",
238                        "-Xbootclasspath/p:" + testClasses);
239                    mode = Mode.STARTS_WITH;
240                    match = Arrays.asList(testClasses);
241                    break;
242
243                case 2:
244                    // the default values for -extdirs and -endorseddirs come after the bootclasspath;
245                    // so to check -Xbootclasspath/a: we specify empty values for those options.
246                    options = getOptions("-d", classes.getPath(),
247                        "-source", "8", "-target", "8",
248                        "-Xbootclasspath/a:" + testClasses,
249                        "-extdirs", "",
250                        "-endorseddirs", "");
251                    mode = Mode.ENDS_WITH;
252                    match = Arrays.asList(testClasses);
253                    break;
254
255                case 3:
256                    options = getOptions("-d", classes.getPath(), "-Xbootclasspath:" + defaultPathString);
257                    mode = Mode.EQUALS;
258                    match = defaultPath;
259                    reference = "";
260                    break;
261
262                case 4:
263                    fileManager.setLocation(PLATFORM_CLASS_PATH, null);
264                    jar = new File(testJars, "j" + i + ".jar");
265                    writeJar(jar, testClasses, "C" + i + ".class");
266                    options = getOptions("-d", classes.getPath(),
267                        "-source", "8", "-target", "8",
268                        "-endorseddirs", testJars.getPath());
269                    mode = Mode.CONTAINS;
270                    match = Arrays.asList(jar);
271                    break;
272
273                case 5:
274                    fileManager.setLocation(PLATFORM_CLASS_PATH, null);
275                    jar = new File(testJars, "j" + i + ".jar");
276                    writeJar(jar, testClasses, "C" + i + ".class");
277                    options = getOptions("-d", classes.getPath(),
278                        "-source", "8", "-target", "8",
279                        "-Djava.endorsed.dirs=" + testJars.getPath());
280                    mode = Mode.CONTAINS;
281                    match = Arrays.asList(jar);
282                    break;
283
284                case 6:
285                    fileManager.setLocation(PLATFORM_CLASS_PATH, null);
286                    jar = new File(testJars, "j" + i + ".jar");
287                    writeJar(jar, testClasses, "C" + i + ".class");
288                    options = getOptions("-d", classes.getPath(),
289                        "-source", "8", "-target", "8",
290                        "-extdirs", testJars.getPath());
291                    mode = Mode.CONTAINS;
292                    match = Arrays.asList(jar);
293                    break;
294
295                case 7:
296                    fileManager.setLocation(PLATFORM_CLASS_PATH, null);
297                    jar = new File(testJars, "j" + i + ".jar");
298                    writeJar(jar, testClasses, "C" + i + ".class");
299                    options = getOptions("-d", classes.getPath(),
300                        "-source", "8", "-target", "8",
301                        "-Djava.ext.dirs=" + testJars.getPath());
302                    mode = Mode.CONTAINS;
303                    match = Arrays.asList(jar);
304                    break;
305
306                case 8:
307                    setLocation(PLATFORM_CLASS_PATH, defaultPath);
308                    options = getOptions("-d", classes.getPath());
309                    mode = Mode.EQUALS;
310                    match = defaultPath;
311                    reference = "";
312                    break;
313
314                default:
315                    options = getOptions("-d", classes.getPath(),
316                        "-source", "8", "-target", "8",
317                        "-bootclasspath", defaultPathString);
318                    mode = Mode.EQUALS;
319                    match = defaultPath;
320                    reference = "";
321                    break;
322            }
323            List<JavaFileObject> sources = getSources("class D" + i + " { " + reference + " }");
324
325            callTask(options, sources);
326            checkPath(PLATFORM_CLASS_PATH, mode, match);
327            checkFile(CLASS_OUTPUT, "D" + i + ".class");
328        }
329
330        tested.add(PLATFORM_CLASS_PATH);
331        System.err.println();
332    }
333
334    void testAnnotationProcessorPath() throws IOException {
335        String test = "testAnnotationProcessorPath";
336        System.err.println("test: " + test);
337
338        fileManager.setLocation(PLATFORM_CLASS_PATH, null);
339
340        String template =
341                "import java.util.*;\n"
342                + "import javax.annotation.processing.*;\n"
343                + "import javax.lang.model.*;\n"
344                + "import javax.lang.model.element.*;\n"
345                + "@SupportedAnnotationTypes(\"*\")\n"
346                + "public class A%d extends AbstractProcessor {\n"
347                + "    public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {\n"
348                + "        return true;\n"
349                + "    }\n"
350                + "    public SourceVersion getSupportedSourceVersion() {\n"
351                + "        return SourceVersion.latest();\n"
352                + "    }\n"
353                + "}";
354
355        for (int i = 1; i <= 5; i++) {
356            File classes = createDir(test + "/" + i + "/classes");
357            File annodir = createDir(test + "/" + i + "/processors");
358            callTask(getOptions("-d", annodir.getPath()), getSources(String.format(template, i)));
359            File annopath = annodir;
360            List<String> options;
361            switch (i) {
362                default:
363                    options = getOptions("-d", classes.getPath(),
364                            "-processorpath", annopath.getPath(),
365                            "-processor", "A" + i);
366                    break;
367
368                case 3:
369                    setLocation(ANNOTATION_PROCESSOR_PATH, annopath);
370                    options = getOptions("-d", classes.getPath(),
371                            "-processor", "A" + i);
372                    break;
373            }
374            List<JavaFileObject> sources = getSources("class D" + i + " { }");
375            callTask(options, sources);
376            checkPath(ANNOTATION_PROCESSOR_PATH, Mode.EQUALS, annopath);
377            checkFile(CLASS_OUTPUT, "D" + i + ".class");
378        }
379
380        tested.add(ANNOTATION_PROCESSOR_PATH);
381        System.err.println();
382    }
383
384    void testSourceOutput() throws IOException {
385        String test = "testAnnotationProcessorPath";
386        System.err.println("test: " + test);
387
388        String source =
389                "import java.io.*;\n"
390                + "import java.util.*;\n"
391                + "import javax.annotation.processing.*;\n"
392                + "import javax.lang.model.*;\n"
393                + "import javax.lang.model.element.*;\n"
394                + "import javax.tools.*;\n"
395                + "@SupportedOptions(\"name\")\n"
396                + "@SupportedAnnotationTypes(\"*\")\n"
397                + "public class A extends AbstractProcessor {\n"
398                + "    int round = 0;\n"
399                + "    public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {\n"
400                + "        if (round++ == 0) try {\n"
401                + "            String name = processingEnv.getOptions().get(\"name\");\n"
402                + "            JavaFileObject fo = processingEnv.getFiler().createSourceFile(name);\n"
403                + "            try (Writer out = fo.openWriter()) {\n"
404                + "                out.write(\"class \" + name + \" { }\");\n"
405                + "            }\n"
406                + "        } catch (IOException e) { throw new Error(e); }\n"
407                + "        return true;\n"
408                + "    }\n"
409                + "    public SourceVersion getSupportedSourceVersion() {\n"
410                + "        return SourceVersion.latest();\n"
411                + "    }\n"
412                + "}";
413
414        File annodir = createDir(test + "/processors");
415        callTask(getOptions("-d", annodir.getPath()), getSources(source));
416        setLocation(ANNOTATION_PROCESSOR_PATH, annodir);
417
418        for (int i = 1; i <= 5; i++) {
419            File classes = createDir(test + "/" + i + "/classes");
420            File genSrc = createDir(test + "/" + "/genSrc");
421            List<String> options;
422            switch (i) {
423                default:
424                    options = getOptions("-d", classes.getPath(),
425                            "-processor", "A", "-Aname=G" + i,
426                            "-s", genSrc.getPath());
427                    break;
428
429                case 3:
430                    setLocation(SOURCE_OUTPUT, genSrc);
431                    options = getOptions("-d", classes.getPath(),
432                            "-processor", "A", "-Aname=G" + i);
433                    break;
434            }
435            List<JavaFileObject> sources = getSources("class D" + i + " { }");
436            callTask(options, sources);
437            checkPath(SOURCE_OUTPUT, Mode.EQUALS, genSrc);
438            checkFile(CLASS_OUTPUT, "D" + i + ".class");
439            checkFile(CLASS_OUTPUT, "G" + i + ".class");
440        }
441        tested.add(SOURCE_OUTPUT);
442        System.err.println();
443    }
444
445    void testNativeHeaderOutput() throws IOException {
446        String test = "testNativeHeaderOutput";
447        System.err.println("test: " + test);
448
449        for (int i = 1; i <= 5; i++) {
450            File classes = createDir(test + "/" + i + "/classes");
451            File headers = createDir(test + "/" + i + "/hdrs");
452            List<String> options;
453            switch (i) {
454                default:
455                    options = getOptions("-d", classes.getPath(), "-h", headers.getPath());
456                    break;
457
458                case 3:
459                    setLocation(NATIVE_HEADER_OUTPUT, headers);
460                    options = getOptions("-d", classes.getPath());
461                    break;
462            }
463            List<JavaFileObject> sources = getSources("class C" + i + " { native void m(); }");
464            callTask(options, sources);
465            checkPath(NATIVE_HEADER_OUTPUT, Mode.EQUALS, headers);
466            checkFile(NATIVE_HEADER_OUTPUT, "C" + i + ".h");
467        }
468
469        tested.add(StandardLocation.NATIVE_HEADER_OUTPUT);
470        System.err.println();
471    }
472
473    List<String> getOptions(String... args) {
474        return Arrays.asList(args);
475    }
476
477    List<JavaFileObject> getSources(String... sources) {
478        List<JavaFileObject> list = new ArrayList<>();
479        for (String s: sources)
480            list.add(getSource(s));
481        return list;
482    }
483
484    JavaFileObject getSource(final String source) {
485        return new SimpleJavaFileObject(getURIFromSource(source), JavaFileObject.Kind.SOURCE) {
486            @Override
487            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
488                return source;
489            }
490        };
491    }
492
493    void callTask(List<String> options, List<JavaFileObject> files) {
494        out.print("compile: ");
495        if (options != null) {
496            for (String o: options) {
497                if (o.length() > 64) {
498                    o = o.substring(0, 32) + "..." + o.substring(o.length() - 32);
499                }
500                out.print(" " + o);
501            }
502        }
503        for (JavaFileObject f: files)
504            out.print(" " + f.getName());
505        out.println();
506        CompilationTask t = compiler.getTask(out, fileManager, null, options, null, files);
507        boolean ok = t.call();
508        if (!ok)
509            error("compilation failed");
510    }
511
512    enum Mode { EQUALS, CONTAINS, STARTS_WITH, ENDS_WITH };
513
514    void checkFile(StandardLocation l, String path) {
515        if (!l.isOutputLocation()) {
516            error("Not an output location: " + l);
517            return;
518        }
519
520        List<File> files = getLocation(l);
521        if (files == null) {
522            error("location is unset: " + l);
523            return;
524        }
525
526        if (files.size() != 1)
527            error("unexpected number of entries on " + l + ": " + files.size());
528
529        File f = new File(files.get(0), path);
530        if (!f.exists())
531            error("file not found: " + f);
532    }
533
534    void checkPath(StandardLocation l, Mode m, File expect) {
535        checkPath(l, m, Arrays.asList(expect));
536    }
537
538    void checkPath(StandardLocation l, Mode m, List<File> expect) {
539        List<File> files = getLocation(l);
540        if (files == null) {
541            error("location is unset: " + l);
542            return;
543        }
544
545        switch (m) {
546            case EQUALS:
547                if (!Objects.equals(files, expect)) {
548                    error("location does not match the expected files: " + l);
549                    out.println("found:  " + files);
550                    out.println("expect: " + expect);
551                }
552                break;
553
554            case CONTAINS:
555                int containsIndex = Collections.indexOfSubList(files, expect);
556                if (containsIndex == -1) {
557                    error("location does not contain the expected files: " + l);
558                    out.println("found:  " + files);
559                    out.println("expect: " + expect);
560                }
561            break;
562
563            case STARTS_WITH:
564                int startsIndex = Collections.indexOfSubList(files, expect);
565                if (startsIndex != 0) {
566                    error("location does not start with the expected files: " + l);
567                    out.println("found:  " + files);
568                    out.println("expect: " + expect);
569                }
570            break;
571
572            case ENDS_WITH:
573                int endsIndex = Collections.lastIndexOfSubList(files, expect);
574                if (endsIndex != files.size() - expect.size()) {
575                    error("location does not end with the expected files: " + l);
576                    out.println("found:  " + files);
577                    out.println("expect: " + expect);
578                }
579            break;
580
581        }
582    }
583
584    List<File> getLocation(StandardLocation l) {
585        Iterable<? extends File> iter = fileManager.getLocation(l);
586        if (iter == null)
587            return null;
588        List<File> files = new ArrayList<>();
589        for (File f: iter)
590            files.add(f);
591        return files;
592    }
593
594    void setLocation(StandardLocation l, File... files) throws IOException {
595        fileManager.setLocation(l, Arrays.asList(files));
596    }
597
598    void setLocation(StandardLocation l, List<File> files) throws IOException {
599        fileManager.setLocation(l, files);
600    }
601
602    void writeFile(File dir, String path, String body) throws IOException {
603        try (FileWriter w = new FileWriter(new File(dir, path))) {
604            w.write(body);
605        }
606    }
607
608    void writeJar(File jar, File dir, String... entries) throws IOException {
609        try (JarOutputStream j = new JarOutputStream(Files.newOutputStream(jar.toPath()))) {
610            for (String entry: entries) {
611                j.putNextEntry(new JarEntry(entry));
612                j.write(Files.readAllBytes(dir.toPath().resolve(entry)));
613            }
614        }
615    }
616
617    private static final Pattern packagePattern
618            = Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
619    private static final Pattern classPattern
620            = Pattern.compile("(?:public\\s+)?(?:class|enum|interface)\\s+(\\w+)");
621
622
623    private static URI getURIFromSource(String source) {
624        String packageName = null;
625
626        Matcher matcher = packagePattern.matcher(source);
627        if (matcher.find()) {
628            packageName = matcher.group(1).replace(".", "/");
629        }
630
631        matcher = classPattern.matcher(source);
632        if (matcher.find()) {
633            String className = matcher.group(1);
634            String path = ((packageName == null) ? "" : packageName + "/") + className + ".java";
635            return URI.create("myfo:///" + path);
636        } else {
637            throw new Error("Could not extract the java class "
638                    + "name from the provided source");
639        }
640    }
641
642    File createDir(String path) {
643        File dir = new File(path);
644        dir.mkdirs();
645        return dir;
646    }
647
648    JavaCompiler compiler;
649    StandardJavaFileManager fileManager;
650
651    /**
652     * Map for recording which standard locations have been tested.
653     */
654    EnumSet<StandardLocation> tested = EnumSet.noneOf(StandardLocation.class);
655
656    /**
657     * Logging stream. Used directly with test and for getTask calls.
658     */
659    final PrintWriter out = new PrintWriter(System.err, true);
660
661    /**
662     * Count of errors so far.
663     */
664    int errors;
665
666    void error(String message) {
667        errors++;
668        out.println("Error: " + message);
669    }
670}
671