GatherDiagnosticInfoObserver.java revision 1870:4aa2e64eff30
1/*
2 * Copyright (c) 2015, 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
24package jdk.test.failurehandler.jtreg;
25
26import com.sun.javatest.Harness;
27import com.sun.javatest.Parameters;
28import com.sun.javatest.TestResult;
29import com.sun.javatest.regtest.RegressionParameters;
30import com.sun.javatest.regtest.OS;
31import jdk.test.failurehandler.*;
32
33import java.io.File;
34import java.io.FileWriter;
35import java.io.IOException;
36import java.io.PrintWriter;
37import java.nio.file.Path;
38import java.nio.file.Paths;
39import java.util.HashMap;
40import java.util.Map;
41
42/**
43 * The jtreg test execution observer, which gathers info about
44 * system and dumps it to a file.
45 */
46public class GatherDiagnosticInfoObserver implements Harness.Observer {
47    public static final String LOG_FILENAME = "environment.log";
48    public static final String ENVIRONMENT_OUTPUT = "environment.html";
49
50    private String compileJdk;
51    private String testJdk;
52
53    /*
54     * The harness calls this method after each test.
55     */
56    @Override
57    public void finishedTest(TestResult tr) {
58        if (!tr.getStatus().isError() && !tr.getStatus().isFailed()) {
59            return;
60        }
61
62        String jtrFile = tr.getFile().toString();
63        final Path workDir = Paths.get(
64                jtrFile.substring(0, jtrFile.lastIndexOf('.')));
65        workDir.toFile().mkdir();
66
67        String name = getClass().getName();
68        PrintWriter log;
69        boolean needClose = false;
70        try {
71            log = new PrintWriter(new FileWriter(
72                    workDir.resolve(LOG_FILENAME).toFile(), true));
73            needClose = true;
74        } catch (IOException e) {
75            log = new PrintWriter(System.out);
76            log.printf("ERROR: %s cannot open log file %s", name,
77                    LOG_FILENAME);
78            e.printStackTrace(log);
79        }
80        try {
81            log.printf("%s ---%n", name);
82            GathererFactory gathererFactory = new GathererFactory(
83                    OS.current().family, workDir, log,
84                    Paths.get(testJdk), Paths.get(compileJdk));
85            gatherEnvInfo(workDir, name, log,
86                    gathererFactory.getEnvironmentInfoGatherer());
87        } catch (Throwable e) {
88            log.printf("ERROR: exception in observer %s:", name);
89            e.printStackTrace(log);
90        } finally {
91            log.printf("--- %s%n", name);
92            if (needClose) {
93                log.close();
94            } else {
95                log.flush();
96            }
97        }
98    }
99
100    private void gatherEnvInfo(Path workDir, String name, PrintWriter log,
101                               EnvironmentInfoGatherer gatherer) {
102        File output = workDir.resolve(ENVIRONMENT_OUTPUT).toFile();
103        try (HtmlPage html = new HtmlPage(new PrintWriter(
104                new FileWriter(output, true)))) {
105            try (ElapsedTimePrinter timePrinter
106                         = new ElapsedTimePrinter(new Stopwatch(), name, log)) {
107                gatherer.gatherEnvironmentInfo(html.getRootSection());
108            }
109        } catch (Throwable e) {
110            log.printf("ERROR: exception in observer on getting environment "
111                    + "information %s:", name);
112            e.printStackTrace(log);
113        }
114    }
115
116    /*
117     * The harness calls this method one time per run, not per test.
118     */
119    @Override
120    public void startingTestRun(Parameters params) {
121        // TODO find a better way to get JDKs
122        RegressionParameters rp = (RegressionParameters) params;
123        Map<?,?> map = new HashMap<>();
124        rp.save(map);
125        compileJdk = (String) map.get("regtest.compilejdk");
126        testJdk = (String) map.get("regtest.testjdk");
127    }
128
129    @Override
130    public void startingTest(TestResult tr) {
131        // no-op
132    }
133
134    @Override
135    public void stoppingTestRun() {
136        // no-op
137    }
138
139    @Override
140    public void finishedTesting() {
141        // no-op
142    }
143
144    @Override
145    public void finishedTestRun(boolean allOK) {
146        // no-op
147    }
148
149    @Override
150    public void error(String msg) {
151        // no-op
152    }
153}
154