This project has retired. For details please refer to its Attic page.
TextReport xref

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