1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package vogar;
18
19import com.google.common.collect.ImmutableSet;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.FileReader;
23import java.io.IOException;
24import java.text.ParseException;
25import java.text.SimpleDateFormat;
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.Date;
29import java.util.HashMap;
30import java.util.Map;
31import java.util.Set;
32import java.util.TimeZone;
33import org.kxml2.io.KXmlParser;
34import org.xmlpull.v1.XmlPullParser;
35import org.xmlpull.v1.XmlPullParserException;
36
37public final class XmlReportReader {
38
39    private final static Set<String> resultTagNames = ImmutableSet.of(
40        XmlReportConstants.FAILURE,
41        XmlReportConstants.ERROR,
42        XmlReportConstants.SUCCESS);
43
44    public Collection<Outcome> readSuiteReport(File xmlReport) {
45        FileInputStream stream = null;
46        try {
47            stream = new FileInputStream(xmlReport);
48
49            KXmlParser parser = new KXmlParser();
50            try {
51                parser.setInput(stream, "UTF-8");
52                parser.setInput(new FileReader(xmlReport));
53                return readTestSuite(parser);
54            } catch (XmlPullParserException e1) {
55                throw new RuntimeException(e1);
56            }
57        } catch (IOException e) {
58            throw new RuntimeException(e);
59        } finally {
60            if (stream != null) {
61                try {
62                    stream.close();
63                } catch(IOException ignored) {
64                }
65            }
66        }
67    }
68
69    private Collection<Outcome> readTestSuite(KXmlParser parser)
70            throws XmlPullParserException, IOException {
71        Collection<Outcome> outcomes = new ArrayList<Outcome>();
72
73        parser.nextTag();
74        parser.require(XmlPullParser.START_TAG, null, XmlReportConstants.TESTSUITE);
75        Map<String, String> testSuiteAttributes = createAttributeMap(parser);
76        String timestamp = testSuiteAttributes.get(XmlReportConstants.TIMESTAMP);
77
78        parser.nextTag();
79        parser.require(XmlPullParser.START_TAG, null, XmlReportConstants.PROPERTIES);
80        parser.nextTag();
81        parser.require(XmlPullParser.END_TAG, null, XmlReportConstants.PROPERTIES);
82        while (parser.nextTag() == XmlPullParser.START_TAG) {
83            parser.require(XmlPullParser.START_TAG, null, XmlReportConstants.TESTCASE);
84
85            Map<String, String> testCaseAttributes = createAttributeMap(parser);
86            String name = testCaseAttributes.get(XmlReportConstants.ATTR_NAME);
87            String classname = testCaseAttributes.get(XmlReportConstants.ATTR_CLASSNAME);
88
89            Result result = Result.SUCCESS;
90            String resultOutput = null;
91            parser.nextTag();
92            String tagName = parser.getName();
93            if (resultTagNames.contains(tagName)) {
94                parser.require(XmlPullParser.START_TAG, null, tagName);
95
96                Map<String, String> resultAttributes = createAttributeMap(parser);
97                String type = resultAttributes.get(XmlReportConstants.ATTR_TYPE);
98                result = Result.valueOf(type);
99
100                resultOutput = parser.nextText();
101
102                parser.require(XmlPullParser.END_TAG, null, tagName);
103                parser.nextTag();
104            }
105
106            // create outcome!
107            SimpleDateFormat dateFormat = new SimpleDateFormat(XmlReportConstants.DATEFORMAT);
108            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
109            dateFormat.setLenient(true);
110            Date date;
111            try {
112                date = dateFormat.parse(timestamp);
113            } catch (ParseException e) {
114                throw new RuntimeException(e);
115            }
116            outcomes.add(new Outcome(classname + "#" + name, result, resultOutput, date));
117
118            parser.require(XmlPullParser.END_TAG, null, XmlReportConstants.TESTCASE);
119        }
120        parser.require(XmlPullParser.END_TAG, null, XmlReportConstants.TESTSUITE);
121
122        return outcomes;
123    }
124
125    private Map<String, String> createAttributeMap(KXmlParser parser) {
126        Map<String, String> attributeMap = new HashMap<String, String>();
127        int attributeCount = parser.getAttributeCount();
128        for (int i = 0; i < attributeCount; i++) {
129            String attributeName = parser.getAttributeName(i);
130            String attributeValue = parser.getAttributeValue(i);
131            attributeMap.put(attributeName, attributeValue);
132        }
133        return attributeMap;
134    }
135}