File: XMLReportParser.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (215 lines) | stat: -rw-r--r-- 8,051 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) 2008 Fabien DUMINY (fduminy@jnode.org)
// Modified by Levente S\u00e1ntha (lsantha@jnode.org)

// This file is part of Mauve.

// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.

// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.  */

package gnu.testlet.runner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Enumeration;

import net.sourceforge.nanoxml.XMLElement;
import net.sourceforge.nanoxml.XMLParseException;

/**
 * XML parser for mauve reports.
 * 
 * @author fabien
 *
 */
public class XMLReportParser implements XMLReportConstants {
    /**
     * Parse the given file and return the corresponding mauve result. 
     * 
     * @param input
     * @return
     * @throws XMLParseException
     * @throws IOException
     */
    public RunResult parse(File input) throws XMLParseException, IOException {
        return parse(new FileReader(input));
    }
    
    /**
     * Parse the given reader and return the corresponding mauve result. 
     * 
     * @param r
     * @return
     * @throws XMLParseException
     * @throws IOException
     */
    public RunResult parse(Reader r) throws XMLParseException, IOException {
        XMLElement xmlRun = new XMLElement();
        BufferedReader reader = null;
        
        try {
            reader = new BufferedReader(r);
            xmlRun.parseFromReader(reader);
    
            checkTag(xmlRun, RUN_RESULT);
            
            String attr;
            RunResult run = new RunResult(getValue(xmlRun, RUN_NAME, ""));
            
            for (Enumeration enumPkg = xmlRun.enumerateChildren(); enumPkg.hasMoreElements(); ) {
                XMLElement xmlPkg = (XMLElement) enumPkg.nextElement();
                int indexTag = checkTag(xmlPkg, new String[]{PACKAGE_RESULT, PROPERTY});
                
                if (indexTag == 1) {
                    String name  = getValue(xmlPkg, PROPERTY_NAME, "");
                    String value  = getValue(xmlPkg, PROPERTY_VALUE, "");
                    run.setSystemProperty(name, value);
                    continue;
                }
                
                attr = getValue(xmlPkg, PACKAGE_NAME, "");
                PackageResult pkg = new PackageResult(attr);             
                run.add(pkg);
                
                for (Enumeration enumCls = xmlPkg.enumerateChildren(); enumCls.hasMoreElements(); ) {
                    XMLElement xmlCls = (XMLElement) enumCls.nextElement();
                    checkTag(xmlCls, CLASS_RESULT);
                    
                    attr = getValue(xmlCls, CLASS_NAME, "");
                    ClassResult cls = new ClassResult(attr);
                    pkg.add(cls);
                    
                    for (Enumeration enumTest = xmlCls.enumerateChildren(); enumTest.hasMoreElements(); ) {
                        XMLElement xmlTest = (XMLElement) enumTest.nextElement();
                        checkTag(xmlTest, TEST_RESULT);
                        
                        attr = getValue(xmlTest, TEST_NAME, "");
                        TestResult test = new TestResult(attr);
                        cls.add(test);
                        
                        for (Enumeration enumCheck = xmlTest.enumerateChildren(); enumCheck.hasMoreElements(); ) {
                            XMLElement xmlCheck = (XMLElement) enumCheck.nextElement();
                            if (TEST_ERROR.equals(xmlCheck.getName())) {
                                test.setFailedMessage(xmlCheck.getContent());
                            } else {
                                checkTag(xmlCheck, CHECK_RESULT);
                                
                                test.add(createCheck(xmlCheck));
                            }
                        }
                    }
                }
            }
            
            return run;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
    
    private CheckResult createCheck(XMLElement xmlCheck) {
        String attr = getValue(xmlCheck, CHECK_NUMBER, "-1");
        int number = Integer.valueOf(attr).intValue();
        
        attr = getValue(xmlCheck, CHECK_PASSED, "false");
        boolean passed = Boolean.valueOf(attr).booleanValue();
        
        CheckResult check = new CheckResult(number, passed);
        
        attr = getValue(xmlCheck, CHECK_POINT, "");
        check.setCheckPoint(attr);
        
        attr = getValue(xmlCheck, CHECK_EXPECTED, "");
        check.setExpected(attr);
        
        attr = getValue(xmlCheck, CHECK_ACTUAL, "");
        check.setActual(attr);
        
        // get the log if any
        if (xmlCheck.countChildren() > 0) {
            XMLElement firstChild = (XMLElement) xmlCheck.enumerateChildren().nextElement();
            checkTag(firstChild, CHECK_LOG);
            
            String content = firstChild.getContent(); 
            if (content != null) {
                check.appendToLog(attr);
            }
        }
    
        return check;
    }

    /**
     * Get the value of an xml element's attribute and return the given default
     * value if the attribute is not defined. 
     * @param xml The xml element for which we want an attribute.
     * @param attributeName The name of the attribute.
     * @param defaultValue The default value to return if the attribute is not defined.
     * @return The value of the xml element's attribute or, if not defined, the given
     * <code>defaultValue</code> parameter. 
     */
    private String getValue(XMLElement xml, String attributeName, String defaultValue) {
        Object attr = xml.getAttribute(attributeName);
        return (attr == null) ? defaultValue : String.valueOf(attr);
    }
    
    /**
     * Checks that the xml element represents the given tag.
     * @param xml The xml element to check.
     * @param tag The tag which is expected.
     * @throws XMLParseException if the xml element doesn't represent the given tag.
     */
    private void checkTag(XMLElement xml, String tag) {
        final String actualTag = xml.getName();
        if (!tag.equals(actualTag)) {
            throw new XMLParseException("", "tag is not '" + tag + "' (actual: '" + actualTag + "')");
        }        
    }
    
    /**
     * Checks that the xml element represents one of the given tags.
     * @param xml The xml element to check.
     * @param tag The tags which are expected.
     * @throws XMLParseException if the xml element doesn't represent one of the given tags.
     */
    private int checkTag(XMLElement xml, String[] tags) {
        final String actualTag = xml.getName();
        int indexTag = -1;
        for (int i = 0; i < tags.length; i++) {
            if (tags[i].equals(actualTag)) {
                indexTag = i;
                break;
            }
        }
        
        if (indexTag < 0) {
            StringBuffer sb = new StringBuffer('(');
            for (int i = 0; i < tags.length; i++) {
                if (i > 0) {
                    sb.append(',');
                }
                sb.append(tags[i]);
            }
            sb.append(')');
            throw new XMLParseException("", "tag is not one of " + sb.toString() + " (actual: '" + actualTag + "')");
        }
        
        return indexTag;
    }
}