TestWithXstdout.java revision 1465:b52a38d4536c
1179055Sjfv/*
2171384Sjfv * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3190873Sjfv * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4171384Sjfv *
5171384Sjfv * This code is free software; you can redistribute it and/or modify it
6171384Sjfv * under the terms of the GNU General Public License version 2 only, as
7171384Sjfv * published by the Free Software Foundation.
8171384Sjfv *
9171384Sjfv * This code is distributed in the hope that it will be useful, but WITHOUT
10171384Sjfv * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11171384Sjfv * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12171384Sjfv * version 2 for more details (a copy is included in the LICENSE file that
13171384Sjfv * accompanied this code).
14171384Sjfv *
15171384Sjfv * You should have received a copy of the GNU General Public License version
16171384Sjfv * 2 along with this work; if not, write to the Free Software Foundation,
17171384Sjfv * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18171384Sjfv *
19171384Sjfv * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20171384Sjfv * or visit www.oracle.com if you need additional information or have any
21171384Sjfv * questions.
22171384Sjfv */
23171384Sjfv
24171384Sjfv/*
25171384Sjfv * @test
26171384Sjfv * @bug 6987384
27171384Sjfv * @summary -XprintProcessorRoundsInfo message printed with different timing than previous
28171384Sjfv * @library /tools/javac/lib
29171384Sjfv * @build JavacTestingAbstractProcessor Test TestWithXstdout
30171384Sjfv * @run main TestWithXstdout
31171384Sjfv */
32179055Sjfv
33179055Sjfvimport java.io.*;
34171384Sjfvimport java.nio.charset.*;
35171384Sjfvimport java.nio.file.*;
36171384Sjfvimport java.util.*;
37171384Sjfv
38171384Sjfvpublic class TestWithXstdout {
39190873Sjfv    public static void main(String... args) throws Exception {
40190873Sjfv        File testSrc = new File(System.getProperty("test.src"));
41190873Sjfv        String testClasses = System.getProperty("test.classes", ".");
42190873Sjfv        String testClassPath = System.getProperty("test.class.path", testClasses);
43190873Sjfv        File stdout = new File("stdout.out");
44171384Sjfv        run_javac("-XDrawDiagnostics",
45200239Sjfv                "-XprintProcessorInfo",
46200239Sjfv                "-Werror",
47179055Sjfv                "-proc:only",
48171384Sjfv                "-processor",  "Test",
49171384Sjfv                "-Xstdout", stdout.getPath(),
50171384Sjfv                "-classpath", testClassPath,
51179055Sjfv                new File(testSrc, "Test.java").getPath());
52171384Sjfv        boolean ok = compare(stdout, new File(testSrc, "Test.out"));
53171384Sjfv        if (!ok)
54185352Sjfv            throw new Exception("differences found");
55171384Sjfv    }
56171384Sjfv
57171384Sjfv    static void run_javac(String... args) throws IOException, InterruptedException {
58171384Sjfv        File javaHome = new File(System.getProperty("java.home"));
59171384Sjfv        if (javaHome.getName().equals("jre"))
60171384Sjfv            javaHome = javaHome.getParentFile();
61171384Sjfv        File javac = new File(new File(javaHome, "bin"), "javac");
62200239Sjfv
63171384Sjfv        List<String> opts = new ArrayList<>();
64179055Sjfv        opts.add(javac.getPath());
65200239Sjfv
66171384Sjfv        String toolOpts = System.getProperty("test.tool.vm.opts");
67179055Sjfv        if (toolOpts != null && !"".equals(toolOpts.trim())) {
68171384Sjfv            opts.addAll(Arrays.asList(toolOpts.trim().split("[\\s]+")));
69200239Sjfv        }
70171384Sjfv        opts.addAll(Arrays.asList(args));
71179055Sjfv        System.out.println("exec: " + opts);
72179055Sjfv        ProcessBuilder pb = new ProcessBuilder(opts);
73181003Sjfv        pb.redirectErrorStream();
74171384Sjfv        Process p = pb.start();
75171384Sjfv        try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
76179055Sjfv            String line;
77179055Sjfv            while ((line = r.readLine()) != null)
78179055Sjfv                System.out.println();
79179055Sjfv        }
80171384Sjfv        int rc = p.waitFor();
81171384Sjfv        if (rc != 0)
82190873Sjfv            System.out.println("javac exited, rc=" + rc);
83171384Sjfv    }
84190873Sjfv
85190873Sjfv    static boolean compare(File a, File b) throws IOException {
86185352Sjfv        List<String> aLines = Files.readAllLines(a.toPath(), Charset.defaultCharset());
87185352Sjfv        List<String> bLines = Files.readAllLines(b.toPath(), Charset.defaultCharset());
88171384Sjfv        System.out.println(a + ": " + aLines.size() + " lines");
89171384Sjfv        System.out.println(b + ": " + bLines.size() + " lines");
90171384Sjfv        return aLines.equals(bLines);
91171384Sjfv    }
92171384Sjfv}
93190873Sjfv