• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.testing.local;
6 
7 import org.json.JSONArray;
8 import org.json.JSONException;
9 import org.json.JSONObject;
10 import org.junit.runner.Description;
11 
12 import java.io.File;
13 import java.io.FileNotFoundException;
14 import java.io.FileOutputStream;
15 import java.io.PrintStream;
16 
17 /**
18  *  Creates json file with junit test information. Format of the json file mirrors the
19  *  json generated by build/android/pylib/results/json_results.py.
20  */
21 public class JsonLogger {
22 
23     private final JSONObject mBaseJsonObject;
24     private final JSONObject mBaseTestInfoJsonObject;
25     private final File mOutputFile;
26 
JsonLogger(File outputFile)27     public JsonLogger(File outputFile) {
28         mBaseJsonObject = new JSONObject();
29         mBaseTestInfoJsonObject = new JSONObject();
30         mOutputFile = outputFile;
31 
32         try {
33             mBaseJsonObject.put("global_tags", new JSONArray());
34             mBaseJsonObject.put("all_tests", new JSONArray());
35             mBaseJsonObject.put("disabled_tests", new JSONArray());
36             mBaseJsonObject.put("per_iteration_data",
37                                 new JSONArray().put(mBaseTestInfoJsonObject));
38         } catch (JSONException e) {
39             System.err.println("Unable to create json output.");
40         }
41     }
42 
43     /**
44      *  Add the results of a test run to the json output.
45      */
addTestResultInfo(Description test, boolean passed, long elapsedTimeMillis)46     public void addTestResultInfo(Description test, boolean passed, long elapsedTimeMillis) {
47         JSONObject testInfoJsonObject = new JSONObject();
48 
49         try {
50             testInfoJsonObject.put("status", (passed ? "SUCCESS" : "FAILURE"));
51             testInfoJsonObject.put("elapsed_time_ms", elapsedTimeMillis);
52             testInfoJsonObject.put("output_snippet", "");
53             testInfoJsonObject.put("output_snippet_base64", "");
54             testInfoJsonObject.put("losless_snippet", "");
55 
56             if (mBaseTestInfoJsonObject.optJSONArray(testName(test)) == null) {
57                 mBaseTestInfoJsonObject.put(testName(test), new JSONArray());
58                 mBaseJsonObject.getJSONArray("all_tests").put(testName(test));
59             }
60             mBaseTestInfoJsonObject.getJSONArray(testName(test)).put(testInfoJsonObject);
61         } catch (JSONException e) {
62             System.err.println("Unable to log test to json output: " + testName(test));
63         }
64     }
65 
66     /**
67      *  Writes the json output to a file.
68      */
writeJsonToFile()69     public void writeJsonToFile() {
70         try {
71             PrintStream stream = new PrintStream(new FileOutputStream(mOutputFile));
72             try {
73                 stream.print(mBaseJsonObject);
74             } finally {
75                 try {
76                     stream.close();
77                 } catch (RuntimeException e) {
78                     System.err.println("Unable to close output file: " + mOutputFile.getPath());
79                 }
80             }
81         } catch (FileNotFoundException e) {
82             System.err.println("File not found: " + mOutputFile.getPath());
83         }
84     }
85 
testName(Description test)86     private String testName(Description test) {
87         return test.getClassName() + "#" + test.getMethodName();
88     }
89 }