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.Writer;
24  import java.util.Date;
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.tck.CmisTest;
32  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
33  import org.apache.chemistry.opencmis.tck.CmisTestResult;
34  import org.apache.chemistry.opencmis.tck.CmisTestResultStatus;
35  
36  /**
37   * Text Report.
38   */
39  public class TextReport extends AbstractCmisTestReport {
40      public static final String NL = System.getProperty("line.separator");
41  
42      public TextReport() {
43      }
44  
45      @Override
46      public void createReport(Map<String, String> parameters, List<CmisTestGroup> groups, Writer writer)
47              throws IOException {
48          writer.write("***************************************************************" + NL);
49          writer.write("Test Report: " + (new Date()) + NL);
50  
51          writer.write("***************************************************************" + NL);
52          if (parameters != null) {
53              for (Map.Entry<String, String> p : (new TreeMap<String, String>(parameters)).entrySet()) {
54                  String value = p.getValue();
55                  if (SessionParameter.PASSWORD.endsWith(p.getKey())) {
56                      value = "*****";
57                  }
58  
59                  writer.write(p.getKey() + " = " + value + NL);
60              }
61          }
62          writer.write("***************************************************************" + NL);
63  
64          if (groups != null) {
65              for (CmisTestGroup group : groups) {
66                  printGroupResults(group, writer);
67              }
68          }
69  
70          writer.flush();
71      }
72  
73      private void printGroupResults(CmisTestGroup group, Writer writer) throws IOException {
74          if (!group.isEnabled()) {
75              return;
76          }
77  
78          writer.write("===============================================================" + NL);
79          writer.write(group.getName() + NL);
80          writer.write("===============================================================" + NL);
81  
82          if (group.getTests() != null) {
83              for (CmisTest test : group.getTests()) {
84                  printTestResults(test, writer);
85              }
86          }
87      }
88  
89      private void printTestResults(CmisTest test, Writer writer) throws IOException {
90          if (!test.isEnabled()) {
91              return;
92          }
93  
94          writer.write("---------------------------------------------------------------" + NL);
95          writer.write(test.getName() + " (" + test.getTime() + " ms)" + NL);
96          writer.write("---------------------------------------------------------------" + NL + NL);
97  
98          if (test.getResults() != null) {
99              for (CmisTestResult result : test.getResults()) {
100                 printResult(1, result, writer);
101                 writer.write(NL);
102             }
103         }
104 
105         writer.write(NL);
106     }
107 
108     private void printResult(int level, CmisTestResult result, Writer writer) throws IOException {
109         printIntend(level, writer);
110         writer.write(result.getStatus() + ": " + result.getMessage());
111 
112         if (result.getStackTrace() != null && result.getStackTrace().length > 0) {
113             writer.write(" (" + result.getStackTrace()[0].getFileName() + ":"
114                     + result.getStackTrace()[0].getLineNumber() + ")");
115         }
116 
117         writer.write(NL);
118 
119         if (result.getStatus() == CmisTestResultStatus.UNEXPECTED_EXCEPTION && result.getException() != null) {
120             writer.write(NL + "Stacktrace:" + NL + NL);
121             result.getException().printStackTrace(new PrintWriter(writer));
122 
123             if (result.getException() instanceof CmisBaseException) {
124                 CmisBaseException cbe = (CmisBaseException) result.getException();
125                 if (cbe.getErrorContent() != null) {
126                     writer.write(NL + "Error Content:" + NL + NL);
127                     writer.write(cbe.getErrorContent());
128                 }
129             }
130         }
131 
132         if (result.getException() != null) {
133             printIntend(level, writer);
134             writer.write("Exception: " + result.getException().getMessage() + NL);
135         }
136 
137         if (result.getRequest() != null) {
138             printIntend(level, writer);
139             writer.write("Request: " + result.getRequest() + NL);
140         }
141 
142         if (result.getResponse() != null) {
143             printIntend(level, writer);
144             writer.write("Response: " + result.getResponse() + NL);
145         }
146 
147         for (CmisTestResult child : result.getChildren()) {
148             printResult(level + 1, child, writer);
149         }
150     }
151 
152     private void printIntend(int x, Writer writer) throws IOException {
153         for (int i = 0; i < x; i++) {
154             writer.write("  ");
155         }
156     }
157 }