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