View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.tck.report;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  import java.io.Writer;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import org.apache.chemistry.opencmis.commons.SessionParameter;
30  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
31  import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
32  import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
33  import org.apache.chemistry.opencmis.tck.CmisTest;
34  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
35  import org.apache.chemistry.opencmis.tck.CmisTestResult;
36  import org.apache.chemistry.opencmis.tck.CmisTestResultStatus;
37  
38  /**
39   * JSON Report.
40   */
41  public class JsonReport extends AbstractCmisTestReport {
42  
43      public JsonReport() {
44      }
45  
46      @Override
47      public void createReport(Map<String, String> parameters, List<CmisTestGroup> groups, Writer writer)
48              throws IOException {
49  
50          JSONObject jsonReport = new JSONObject();
51  
52          JSONObject jsonParameters = new JSONObject();
53          jsonReport.put("parameters", jsonParameters);
54  
55          if (parameters != null) {
56              for (Map.Entry<String, String> p : (new TreeMap<String, String>(parameters)).entrySet()) {
57                  String value = p.getValue();
58                  if (SessionParameter.PASSWORD.endsWith(p.getKey())) {
59                      value = "*****";
60                  }
61  
62                  jsonParameters.put(p.getKey(), value);
63              }
64          }
65  
66          if (groups != null) {
67              JSONArray jsonGroups = new JSONArray();
68              jsonReport.put("groups", jsonGroups);
69  
70              for (CmisTestGroup group : groups) {
71                  printGroupResults(group, jsonGroups);
72              }
73          }
74  
75          jsonReport.writeJSONString(writer);
76          writer.flush();
77      }
78  
79      private void printGroupResults(CmisTestGroup group, JSONArray jsonGroups) throws IOException {
80          if (!group.isEnabled()) {
81              return;
82          }
83  
84          JSONObject jsonGroup = new JSONObject();
85          jsonGroups.add(jsonGroup);
86  
87          jsonGroup.put("name", group.getName());
88  
89          if (group.getTests() != null && !group.getTests().isEmpty()) {
90              JSONArray jsonTests = new JSONArray();
91              jsonGroup.put("tests", jsonTests);
92  
93              for (CmisTest test : group.getTests()) {
94                  printTestResults(test, jsonTests);
95              }
96          }
97      }
98  
99      private void printTestResults(CmisTest test, JSONArray jsonTests) throws IOException {
100         if (!test.isEnabled()) {
101             return;
102         }
103 
104         JSONObject jsonTest = new JSONObject();
105         jsonTests.add(jsonTest);
106 
107         jsonTest.put("name", test.getName());
108         jsonTest.put("time", test.getTime());
109 
110         if (test.getResults() != null && !test.getResults().isEmpty()) {
111             JSONArray jsonResults = new JSONArray();
112             jsonTest.put("results", jsonResults);
113 
114             for (CmisTestResult result : test.getResults()) {
115                 printResult(result, jsonResults);
116             }
117         }
118     }
119 
120     private void printResult(CmisTestResult result, JSONArray results) throws IOException {
121         JSONObject jsonResult = new JSONObject();
122         results.add(jsonResult);
123 
124         jsonResult.put("status", result.getStatus().toString());
125         jsonResult.put("message", result.getMessage());
126 
127         if (result.getStackTrace() != null && result.getStackTrace().length > 0) {
128             jsonResult.put("file",
129                     result.getStackTrace()[0].getFileName() + ":" + result.getStackTrace()[0].getLineNumber());
130         }
131 
132         if (result.getStatus() == CmisTestResultStatus.UNEXPECTED_EXCEPTION && result.getException() != null) {
133             StringWriter sw = new StringWriter();
134             PrintWriter pw = new PrintWriter(sw);
135             result.getException().printStackTrace(pw);
136 
137             jsonResult.put("stacktrace", sw.toString());
138 
139             if (result.getException() instanceof CmisBaseException) {
140                 CmisBaseException cbe = (CmisBaseException) result.getException();
141                 if (cbe.getErrorContent() != null) {
142                     jsonResult.put("errorcontent", cbe.getErrorContent());
143                 }
144             }
145         }
146 
147         if (result.getException() != null) {
148             jsonResult.put("exception", result.getException().getMessage());
149         }
150 
151         if (result.getRequest() != null) {
152             jsonResult.put("request", result.getRequest());
153         }
154 
155         if (result.getRequest() != null) {
156             jsonResult.put("response", result.getResponse());
157         }
158 
159         if (!result.getChildren().isEmpty()) {
160             JSONArray nextLevel = new JSONArray();
161             jsonResult.put("results", nextLevel);
162 
163             for (CmisTestResult child : result.getChildren()) {
164                 printResult(child, nextLevel);
165             }
166         }
167     }
168 }