This project has retired. For details please refer to its Attic page.
XmlReport 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.Writer;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.TreeMap;
25  
26  import javax.xml.stream.XMLOutputFactory;
27  import javax.xml.stream.XMLStreamWriter;
28  
29  import org.apache.chemistry.opencmis.tck.CmisTest;
30  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
31  import org.apache.chemistry.opencmis.tck.CmisTestResult;
32  
33  /**
34   * XML Report.
35   */
36  public class XmlReport extends AbstractCmisTestReport {
37      private static final String TAG_REPORT = "report";
38      private static final String TAG_PARAMETERS = "parameters";
39      private static final String TAG_PARAMETER = "parameter";
40      private static final String TAG_GROUP = "group";
41      private static final String TAG_TEST = "test";
42      private static final String TAG_RESULT = "result";
43  
44      private static final String ATTR_KEY = "key";
45      private static final String ATTR_VALUE = "value";
46      private static final String ATTR_NAME = "name";
47      private static final String ATTR_TIME = "time";
48      private static final String ATTR_STATUS = "status";
49      private static final String ATTR_MESSAGE = "message";
50  
51      public XmlReport() {
52      }
53  
54      @Override
55      public void createReport(Map<String, String> parameters, List<CmisTestGroup> groups, Writer writer)
56              throws Exception {
57          XMLOutputFactory factory = XMLOutputFactory.newInstance();
58          XMLStreamWriter xml = factory.createXMLStreamWriter(writer);
59  
60          // start doc
61          xml.writeStartDocument();
62  
63          xml.writeStartElement(TAG_REPORT);
64  
65          if (parameters != null) {
66              xml.writeStartElement(TAG_PARAMETERS);
67  
68              for (Map.Entry<String, String> p : (new TreeMap<String, String>(parameters)).entrySet()) {
69                  xml.writeStartElement(TAG_PARAMETER);
70                  xml.writeAttribute(ATTR_KEY, p.getKey());
71                  if (p.getValue() != null) {
72                      xml.writeAttribute(ATTR_VALUE, p.getValue());
73                  }
74                  xml.writeEndElement();
75              }
76  
77              xml.writeEndElement();
78          }
79  
80          if (groups != null) {
81              for (CmisTestGroup group : groups) {
82                  printGroupResults(group, xml);
83              }
84          }
85  
86          xml.writeEndElement();
87  
88          // end document
89          xml.writeEndDocument();
90          xml.flush();
91      }
92  
93      private void printGroupResults(CmisTestGroup group, XMLStreamWriter xml) throws Exception {
94          if (!group.isEnabled()) {
95              return;
96          }
97  
98          xml.writeStartElement(TAG_GROUP);
99          xml.writeAttribute(ATTR_NAME, group.getName());
100 
101         if (group.getTests() != null) {
102             for (CmisTest test : group.getTests()) {
103                 printTestResults(test, xml);
104             }
105         }
106 
107         xml.writeEndElement();
108     }
109 
110     private void printTestResults(CmisTest test, XMLStreamWriter xml) throws Exception {
111         if (!test.isEnabled()) {
112             return;
113         }
114 
115         xml.writeStartElement(TAG_TEST);
116         xml.writeAttribute(ATTR_NAME, test.getName());
117         xml.writeAttribute(ATTR_TIME, "" + test.getTime());
118 
119         if (test.getResults() != null) {
120             for (CmisTestResult result : test.getResults()) {
121                 printResult(result, xml);
122             }
123         }
124 
125         xml.writeEndElement();
126     }
127 
128     private void printResult(CmisTestResult result, XMLStreamWriter xml) throws Exception {
129         xml.writeStartElement(TAG_RESULT);
130         xml.writeAttribute(ATTR_STATUS, result.getStatus().toString());
131         xml.writeAttribute(ATTR_MESSAGE, result.getMessage());
132 
133         for (CmisTestResult child : result.getChildren()) {
134             printResult(child, xml);
135         }
136 
137         xml.writeEndElement();
138     }
139 }