TestWithXstdout.java revision 2721:f7ce2cfa4cdb
1/*
2 * Copyright (c) 2011, 2012, 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 6987384
27 * @summary -XprintProcessorRoundsInfo message printed with different timing than previous
28 * @library /tools/javac/lib
29 * @build JavacTestingAbstractProcessor Test TestWithXstdout
30 * @run main TestWithXstdout
31 */
32
33import java.io.*;
34import java.nio.charset.*;
35import java.nio.file.*;
36import java.util.*;
37
38public class TestWithXstdout {
39    public static void main(String... args) throws Exception {
40        File testSrc = new File(System.getProperty("test.src"));
41        String testClasses = System.getProperty("test.classes", ".");
42        String testClassPath = System.getProperty("test.class.path", testClasses);
43        File stdout = new File("stdout.out");
44        run_javac("-XDrawDiagnostics",
45                "-XprintProcessorInfo",
46                "-Werror",
47                "-proc:only",
48                "-processor",  "Test",
49                "-Xstdout", stdout.getPath(),
50                "-classpath", testClassPath,
51                new File(testSrc, "Test.java").getPath());
52        boolean ok = compare(stdout, new File(testSrc, "Test.out"));
53        if (!ok)
54            throw new Exception("differences found");
55    }
56
57    static void run_javac(String... args) throws IOException, InterruptedException {
58        File javaHome = new File(System.getProperty("java.home"));
59        File javac = new File(new File(javaHome, "bin"), "javac");
60
61        List<String> opts = new ArrayList<>();
62        opts.add(javac.getPath());
63
64        String toolOpts = System.getProperty("test.tool.vm.opts");
65        if (toolOpts != null && !"".equals(toolOpts.trim())) {
66            opts.addAll(Arrays.asList(toolOpts.trim().split("[\\s]+")));
67        }
68        opts.addAll(Arrays.asList(args));
69        System.out.println("exec: " + opts);
70        ProcessBuilder pb = new ProcessBuilder(opts);
71        pb.redirectErrorStream();
72        Process p = pb.start();
73        try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
74            String line;
75            while ((line = r.readLine()) != null)
76                System.out.println();
77        }
78        int rc = p.waitFor();
79        if (rc != 0)
80            System.out.println("javac exited, rc=" + rc);
81    }
82
83    static boolean compare(File a, File b) throws IOException {
84        List<String> aLines = Files.readAllLines(a.toPath(), Charset.defaultCharset());
85        List<String> bLines = Files.readAllLines(b.toPath(), Charset.defaultCharset());
86        System.out.println(a + ": " + aLines.size() + " lines");
87        System.out.println(b + ": " + bLines.size() + " lines");
88        return aLines.equals(bLines);
89    }
90}
91