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