File: RerunTest2.java

package info (click to toggle)
jtreg 5.1-b01-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 5,432 kB
  • sloc: java: 29,218; xml: 729; sh: 665; makefile: 245
file content (153 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.*;
import java.util.*;

public class RerunTest2 {
    public static void main(String... args) {
        try {
            new RerunTest2().run(args);
        } catch (Exception e) {
            System.err.println(e);
            System.exit(1);
        }
    }

    void run(String... args) throws Exception {
        File testSuite = new File(args[0]);
        for (String mode: new String[] { "othervm", "agentvm" }) {
            test(testSuite, mode);
        }
        if (errors > 0)
            throw new Exception(errors + " errors found");
    }

    void test(File testSuite, String mode) throws Exception {
        System.out.println("test " + mode);

        File initialWorkDir = new File(mode, "work");
        File initialReportDir = new File(mode, "report");
        runTests(testSuite, initialWorkDir, initialReportDir, mode);

        for (String test: getTests(initialReportDir)) {
            File testWorkDir = new File(mode,
                test.replaceAll("\\.java$", "").replaceAll("[^A-Za-z]+", "_"));
            rerunTest(testSuite, test, initialWorkDir, testWorkDir);
        }

        System.out.println();
    }

    void runTests(File ts, File wd, File rd, String mode) throws Exception {
        String[] args = {
            "-w", wd.getPath(),
            "-r", rd.getPath(),
            "-" + mode,
            ts.getPath()
        };
        int rc = new com.sun.javatest.regtest.Main().run(args);
        if (rc != 0)
            throw new Exception("jtreg: exit code " + rc);
    }


    private static final String markerText = "Lorem ipsum";

    void rerunTest(File testSuite, String name, File wd, File dir) throws Exception {
        System.out.println("rerun " + name);
        File script = getRerunScript(testSuite, name, wd, dir);
        Process p = new ProcessBuilder("sh", script.getAbsolutePath())
                .directory(dir)
                .redirectErrorStream(true)
                .start();
        boolean foundMarker = false;
        String line;
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter out = new BufferedWriter(new FileWriter(new File(dir, "rerun.log")));
        try {
            while ((line = in.readLine()) != null) {
                out.write(line);
                out.newLine();
                if (line.contains(markerText))
                    foundMarker = true;
            }
        } finally {
            in.close();
            out.close();
        }
        //System.out.println("rerun exit code: " + p.waitFor());
        if (!foundMarker)
            error(dir.getParent() + ", " + name + ": marker text not found");
    }

    File getRerunScript(File testSuite, String name, File wd, File dir) throws Exception {
        String[] args = {
            "-w", wd.getPath(),
            "-show:rerun",
            "-dir:" + testSuite.getPath(),
            name
        };
        StringWriter outs = new StringWriter();
        PrintWriter out = new PrintWriter(outs);
        StringWriter errs = new StringWriter();
        PrintWriter err = new PrintWriter(outs);
        int rc;
        try {
            rc = new com.sun.javatest.regtest.Main(out,err).run(args);
        } finally {
            err.close();
            out.close();
            writeFile(new File(dir, "show-rerun-out.txt"), outs.toString());
            writeFile(new File(dir, "show-rerun-err.txt"), errs.toString());
        }
//      if (rc != 0)
//          throw new Exception("jtreg: exit code " + rc);
        return new File(dir, "show-rerun-out.txt");
    }

    List<String> getTests(File rd) throws IOException {
        List<String> tests = new ArrayList<>();
        File summary = new File(rd, "text/summary.txt");
        try (BufferedReader in = new BufferedReader(new FileReader(summary))) {
            String line;
            while ((line = in.readLine()) != null)
                tests.add(line.substring(0, line.indexOf(' ')));
        }
        return tests;
    }

    void writeFile(File f, String s) throws IOException {
        f.getParentFile().mkdirs();
        try (FileWriter w = new FileWriter(f)) {
            w.write(s);
        }
    }

    void error(String msg) {
        System.out.println("Error: " + msg);
        errors++;
    }

    int errors;
}