Test.java revision 3294:9adfb22ff08f
176195Sbrian/*
276195Sbrian * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
376195Sbrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
476195Sbrian *
576195Sbrian * This code is free software; you can redistribute it and/or modify it
676195Sbrian * under the terms of the GNU General Public License version 2 only, as
776195Sbrian * published by the Free Software Foundation.
876195Sbrian *
976195Sbrian * This code is distributed in the hope that it will be useful, but WITHOUT
1076195Sbrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1176195Sbrian * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1276195Sbrian * version 2 for more details (a copy is included in the LICENSE file that
1376195Sbrian * accompanied this code).
1476195Sbrian *
1576195Sbrian * You should have received a copy of the GNU General Public License version
1676195Sbrian * 2 along with this work; if not, write to the Free Software Foundation,
1776195Sbrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1876195Sbrian *
1976195Sbrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2076195Sbrian * or visit www.oracle.com if you need additional information or have any
2176195Sbrian * questions.
2276195Sbrian */
2376195Sbrian
2476195Sbrian/**
2576195Sbrian *  @test
2676195Sbrian *  @bug 8001098 8004961 8004082
2776195Sbrian *  @library /tools/lib
2876195Sbrian *  @modules jdk.compiler/com.sun.tools.javac.api
2976195Sbrian *           jdk.compiler/com.sun.tools.javac.file
30145975Sanholt *           jdk.compiler/com.sun.tools.javac.main
3176195Sbrian *          jdk.jdeps/com.sun.tools.javap
3276195Sbrian *  @build ToolBox
3376195Sbrian *  @run main Test
3476195Sbrian *  @summary Provide a simple light-weight "plug-in" mechanism for javac
3576195Sbrian */
3676195Sbrian
3776195Sbrianimport java.io.File;
3876195Sbrianimport java.util.Arrays;
3976195Sbrianimport java.util.List;
4076195Sbrian
4176195Sbrianimport javax.tools.JavaCompiler;
4276195Sbrianimport javax.tools.JavaFileObject;
4376195Sbrianimport javax.tools.StandardJavaFileManager;
4476195Sbrianimport javax.tools.StandardLocation;
4576195Sbrianimport javax.tools.ToolProvider;
4676195Sbrian
4776195Sbrianimport com.sun.source.util.JavacTask;
4876195Sbrian
4976195Sbrianpublic class Test {
5076195Sbrian    public static void main(String... args) throws Exception {
5176195Sbrian        new Test().run();
5276195Sbrian    }
5376195Sbrian
5476195Sbrian    final File testSrc;
5576195Sbrian    final File pluginSrc;
5676195Sbrian    final File pluginClasses ;
5776195Sbrian    final File pluginJar;
5876195Sbrian    final List<String> ref1;
5976195Sbrian    final List<String> ref2;
6076195Sbrian    final JavaCompiler compiler;
6176195Sbrian    final StandardJavaFileManager fm;
6276195Sbrian    ToolBox tb = new ToolBox();
6376195Sbrian
6476195Sbrian    Test() throws Exception {
6576195Sbrian        testSrc = new File(tb.testSrc);
6676195Sbrian        pluginSrc = new File(testSrc, "ShowTypePlugin.java");
6776195Sbrian        pluginClasses = new File("plugin");
6876195Sbrian        tb.createDirectories(pluginClasses.toPath());
6976195Sbrian        pluginJar = new File("plugin.jar");
7076195Sbrian        ref1 = tb.readAllLines((new File(testSrc,"Identifiers.out")).toPath());
7176195Sbrian        ref2 = tb.readAllLines((new File(testSrc,"Identifiers_PI.out")).toPath());
7276195Sbrian        compiler = ToolProvider.getSystemJavaCompiler();
7376195Sbrian        fm = compiler.getStandardFileManager(null, null, null);
7476195Sbrian    }
7576195Sbrian
7676195Sbrian    void run() throws Exception {
7776195Sbrian        try {
7876195Sbrian            // compile the plugin explicitly, to a non-standard directory
7976195Sbrian            // so that we don't find it on the wrong path by accident
8076195Sbrian            tb.new JavacTask()
8176195Sbrian              .options("-d", pluginClasses.getPath())
8276195Sbrian              .files(pluginSrc.getPath())
8376195Sbrian              .run();
8476195Sbrian
8576195Sbrian            File plugin = new File(pluginClasses.getPath(), "META-INF/services/com.sun.source.util.Plugin");
8676195Sbrian            tb.writeFile(plugin.getPath(), "ShowTypePlugin\n");
8776195Sbrian            tb.new JarTask()
8876195Sbrian              .run("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
8976195Sbrian
9076195Sbrian            testCommandLine("-Xplugin:showtype", ref1);
9176195Sbrian            testCommandLine("-Xplugin:showtype PI", ref2);
9276195Sbrian            testAPI("-Xplugin:showtype", ref1);
9376195Sbrian            testAPI("-Xplugin:showtype PI", ref2);
9476195Sbrian
9576195Sbrian            if (errors > 0)
9676195Sbrian                throw new Exception(errors + " errors occurred");
9776195Sbrian        } finally {
9876195Sbrian            fm.close();
9976195Sbrian        }
10076195Sbrian    }
10176195Sbrian
10276195Sbrian    void testAPI(String opt, List<String> ref) throws Exception {
10376195Sbrian        File identifiers = new File(testSrc, "Identifiers.java");
10476195Sbrian        fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, Arrays.asList(pluginJar));
10576195Sbrian        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
10676195Sbrian        List<String> options = Arrays.asList(opt);
10776195Sbrian        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
10876195Sbrian
10976195Sbrian        System.err.println("test api: " + options + " " + files);
11076195Sbrian        ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.API)
11176195Sbrian                                  .fileManager(fm)
11276195Sbrian                                  .options(opt)
11376195Sbrian                                  .files(identifiers.toPath())
11476195Sbrian                                  .run(ToolBox.Expect.SUCCESS)
11576195Sbrian                                  .writeAll();
11676195Sbrian        String out = result.getOutput(ToolBox.OutputKind.DIRECT);
11776195Sbrian        checkOutput(out, ref);
11876195Sbrian    }
11976195Sbrian
12076195Sbrian    void testCommandLine(String opt, List<String> ref) {
12176195Sbrian        File identifiers = new File(testSrc, "Identifiers.java");
12276195Sbrian        String[] args = {
12376195Sbrian            "-d", ".",
12476195Sbrian            "-processorpath", pluginJar.getPath(),
12576195Sbrian            opt,
12676195Sbrian            identifiers.getPath() };
12776195Sbrian
12876195Sbrian        System.err.println("test command line: " + Arrays.asList(args));
12976195Sbrian        ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.CMDLINE)
13076195Sbrian                                  .options(args)
13176195Sbrian                                  .run(ToolBox.Expect.SUCCESS)
13276195Sbrian                                  .writeAll();
13376195Sbrian        String out = result.getOutput(ToolBox.OutputKind.DIRECT);
13476195Sbrian        checkOutput(out, ref);
13576195Sbrian    }
13676195Sbrian
13776195Sbrian    private void checkOutput(String out, List<String> ref) {
13876195Sbrian        List<String> lines = Arrays.asList(out
13976195Sbrian                .replaceAll(".*?([A-Za-z.]+:[0-9]+: .*)", "$1") // remove file directory
14076195Sbrian                .split("[\r\n]+"));                             // allow for newline formats
14176195Sbrian        if (!lines.equals(ref)) {
14276195Sbrian            error("unexpected output");
14376195Sbrian        }
14476195Sbrian    }
14576195Sbrian
14676195Sbrian    private void error(String msg) {
14776195Sbrian        System.err.println(msg);
14876195Sbrian        errors++;
14976195Sbrian    }
15076195Sbrian
15176195Sbrian    int errors;
15276195Sbrian}
15376195Sbrian