OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
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.base.test; | |
6 | |
7 import android.support.test.internal.runner.listener.InstrumentationRunListener; | |
8 | |
9 import org.json.JSONArray; | |
10 import org.json.JSONObject; | |
11 import org.junit.runner.Description; | |
12 import org.junit.runner.Result; | |
13 | |
14 import org.chromium.base.Log; | |
15 | |
16 import java.io.File; | |
17 import java.io.FileOutputStream; | |
18 import java.io.IOException; | |
19 import java.io.OutputStreamWriter; | |
20 import java.io.Writer; | |
21 import java.lang.annotation.Annotation; | |
22 import java.lang.reflect.Method; | |
23 import java.util.Arrays; | |
24 import java.util.Collection; | |
25 import java.util.HashMap; | |
26 import java.util.HashSet; | |
27 import java.util.Map; | |
28 import java.util.Set; | |
29 | |
30 /** | |
31 * A RunListener that list out all the test information into a json file. | |
32 */ | |
33 public class TestListInstrumentationRunListener extends InstrumentationRunListen er { | |
34 private static final String TAG = "TestListRunListener"; | |
35 private static final Set<String> SKIP_METHODS = new HashSet<>( | |
36 Arrays.asList(new String[] {"toString", "hashCode", "annotationType" , "equals"})); | |
37 | |
38 private final Map<Class<?>, JSONObject> mTestClassJsonMap = new HashMap<>(); | |
39 private final String mOutputPath; | |
40 | |
41 public TestListInstrumentationRunListener(String outputPath) { | |
42 super(); | |
43 mOutputPath = outputPath; | |
44 } | |
45 | |
46 /** | |
47 * Store the test method description to a Map at the beginning of a test run . | |
48 */ | |
49 @Override | |
50 public void testStarted(Description desc) throws Exception { | |
51 if (!mTestClassJsonMap.containsKey(desc.getTestClass())) { | |
52 Class<?> testClass = desc.getTestClass(); | |
53 mTestClassJsonMap.put(desc.getTestClass(), new JSONObject() | |
54 .put("class", testClass.getName()) | |
55 .put("superclass", testClass.getSuperclass().getName()) | |
56 .put("annotations", | |
57 getAnnotationJSON(Arrays.asList(testClass.getAnnotat ions()))) | |
58 .put("methods", new JSONArray().put(getTestMethodJSON(desc)) )); | |
59 } else { | |
60 ((JSONArray) mTestClassJsonMap.get(desc.getTestClass()).get("methods ")) | |
61 .put(getTestMethodJSON(desc)); | |
62 } | |
63 } | |
64 | |
65 /** | |
66 * Create a JSONArray with all the test class JSONObjects and save it to lis ted output path. | |
67 */ | |
68 @Override | |
69 public void testRunFinished(Result result) throws Exception { | |
70 Writer writer = null; | |
71 File file = new File(mOutputPath); | |
72 try { | |
73 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8") ; | |
74 JSONArray allTestClassesJSON = new JSONArray(mTestClassJsonMap.value s()); | |
75 writer.write(allTestClassesJSON.toString()); | |
76 } catch (Exception e) { | |
77 Log.e(TAG, "failed to write json to file", e); | |
78 throw e; | |
jbudorick
2017/07/21 19:35:08
nit: Do we need to catch this if we're just going
| |
79 } finally { | |
80 if (writer != null) { | |
81 try { | |
82 writer.close(); | |
83 } catch (IOException e) { | |
84 } | |
85 } | |
86 } | |
87 } | |
88 | |
89 /** | |
90 * Return a JSONOject that represent a Description of a method". | |
91 */ | |
92 static JSONObject getTestMethodJSON(Description desc) throws Exception { | |
93 return new JSONObject() | |
94 .put("method", desc.getMethodName()) | |
95 .put("annotations", getAnnotationJSON(desc.getAnnotations())); | |
96 } | |
97 | |
98 /** | |
99 * Create a JSONObject that represent a collection of anntations. | |
100 * | |
101 * For example, for the following group of annotations for ExampleClass | |
102 * <code> | |
103 * @A | |
104 * @B(message = "hello", level = 3) | |
105 * public class ExampleClass() {} | |
106 * </code> | |
107 * | |
108 * This method would return a JSONObject as such: | |
109 * <code> | |
110 * { | |
111 * "A": {}, | |
112 * "B": { | |
113 * "message": "hello", | |
114 * "level": "3" | |
115 * } | |
116 * } | |
117 * </code> | |
118 * | |
119 * The method accomplish this by though through each annotation and reflecti vely call the | |
120 * annotation's method to get the element value, with exceptions to methods like "equals()" | |
121 * or "hashCode". | |
122 */ | |
123 static JSONObject getAnnotationJSON(Collection<Annotation> annotations) | |
124 throws Exception { | |
125 JSONObject annotationsJsons = new JSONObject(); | |
126 for (Annotation a : annotations) { | |
127 JSONObject elementJsonObject = new JSONObject(); | |
128 for (Method method : a.annotationType().getMethods()) { | |
129 if (SKIP_METHODS.contains(method.getName())) { | |
130 continue; | |
131 } | |
132 try { | |
133 Object value = method.invoke(a); | |
134 if (value == null) { | |
135 elementJsonObject.put(method.getName(), null); | |
136 } else { | |
137 elementJsonObject.put(method.getName(), | |
138 value.getClass().isArray() | |
139 ? new JSONArray(Arrays.asList((Object[]) value)) | |
140 : value.toString()); | |
141 } | |
142 } catch (IllegalArgumentException e) { | |
143 } | |
144 } | |
145 annotationsJsons.put(a.annotationType().getSimpleName(), elementJson Object); | |
146 } | |
147 return annotationsJsons; | |
148 } | |
149 } | |
OLD | NEW |