tsrc/qtestutils/src/testresultxmlparser.cpp
branchRCL_3
changeset 33 bc78a40cd63c
parent 32 73a1feb507fb
child 35 6c57ef9392d2
equal deleted inserted replaced
32:73a1feb507fb 33:bc78a40cd63c
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "testresultxmlparser.h" 
       
    19 #include <stdio.h>
       
    20 
       
    21 const char testFunctionElement[] = "TestFunction";
       
    22 const char incidentElement[] = "Incident";
       
    23 const char descriptionElement[] = "Description";
       
    24 const char nameAttr[] = "name";
       
    25 const char typeAttr[] = "type";
       
    26 const char fileAttr[] = "file";
       
    27 const char lineAttr[] = "line";
       
    28 const char attrValueFail[] = "fail";
       
    29 
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // TestResultXmlParser::TestResultXmlParser
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 TestResultXmlParser::TestResultXmlParser() 
       
    36 : mTestCount(0), 
       
    37   mParsingIncidentElement(false),
       
    38   mParsingDescriptionElement(false),
       
    39   mCurrentTestFailed(false),
       
    40   mCurrentTestFailureLine(0)
       
    41 {
       
    42     mErrors = new QStringList;
       
    43 }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // TestResultXmlParser::TestResultXmlParser
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 TestResultXmlParser::~TestResultXmlParser()
       
    50 {
       
    51     delete mErrors;   
       
    52 }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // TestResultXmlParser::startElement
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 bool TestResultXmlParser::startElement(
       
    59     const QString& /*namespaceURI*/, 
       
    60     const QString& /*localName*/, 
       
    61     const QString& qName, 
       
    62     const QXmlAttributes& atts)
       
    63 {
       
    64     if (qName == testFunctionElement) {
       
    65         mTestCount++;
       
    66         mCurrentTestName = atts.value(nameAttr);
       
    67         return true;
       
    68     }
       
    69     if (qName == incidentElement) {
       
    70         mParsingIncidentElement = true;
       
    71         if (atts.value(typeAttr) == attrValueFail) {
       
    72             mCurrentTestFailed = true;
       
    73             mCurrentTestFile = atts.value(fileAttr);
       
    74             mCurrentTestFailureLine = atts.value(lineAttr).toInt();
       
    75         }
       
    76         return true;
       
    77     }
       
    78     mParsingDescriptionElement = (qName == descriptionElement);
       
    79     return true;
       
    80 }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // TestResultXmlParser::endElement
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 bool TestResultXmlParser::endElement(
       
    87     const QString& /*namespaceURI*/,
       
    88     const QString& /*localName*/,
       
    89     const QString& qName)
       
    90 {
       
    91     if (qName == incidentElement) {
       
    92         mParsingIncidentElement = false;
       
    93         mCurrentTestFailed = false;
       
    94         return true;
       
    95     }
       
    96     if (qName == descriptionElement) {
       
    97         mParsingDescriptionElement = false;
       
    98     }    
       
    99     return true;
       
   100 }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // TestResultXmlParser::characters
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 bool TestResultXmlParser::characters(const QString& ch)
       
   107 {
       
   108     if (mParsingIncidentElement && 
       
   109         mParsingDescriptionElement &&
       
   110         mCurrentTestFailed) {
       
   111         QString testResult = mCurrentTestName + " failed:\n";
       
   112         testResult += "File: ";
       
   113         testResult += mCurrentTestFile;
       
   114         testResult += "\n";
       
   115         testResult += "Line: ";
       
   116         testResult += QString::number(mCurrentTestFailureLine);
       
   117         testResult += "\n";
       
   118         testResult += "Reason: ";
       
   119         testResult += ch;
       
   120         testResult += "\n";
       
   121         mErrors->append(testResult);
       
   122     }
       
   123     return true;
       
   124 }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // TestResultXmlParser::parse
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 int TestResultXmlParser::parse(const QString& fileName)
       
   131 {
       
   132     QFile file(fileName);
       
   133     QXmlInputSource inputSource(&file);
       
   134     QXmlSimpleReader reader;
       
   135     reader.setContentHandler(this);
       
   136     return reader.parse(inputSource);
       
   137 }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // TestResultXmlParser::parseAndPrintResults
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 int TestResultXmlParser::parseAndPrintResults(
       
   144     const QString& fileName,
       
   145     bool printDetails)
       
   146 {
       
   147     printf("Parsing: %s\n", fileName.toUtf8().data());
       
   148     int error = parse(fileName);
       
   149     printf("%d tests executed. Failed total: %d\n", mTestCount, mErrors->count());
       
   150     if (printDetails) {
       
   151         printf("\n");
       
   152         foreach(QString error, *mErrors) {
       
   153             printf(error.toUtf8().data());
       
   154             printf("\n");
       
   155         }
       
   156     }
       
   157     return error;
       
   158 }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // TestResultXmlParser::testCount
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 int TestResultXmlParser::testCount()
       
   165 {
       
   166     return mTestCount;
       
   167 }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // TestResultXmlParser::errors
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 QStringList TestResultXmlParser::errors()
       
   174 {
       
   175     return *mErrors;
       
   176 }
       
   177 
       
   178 // End of File.