Subversion Repositories DevTools

Rev

Rev 7033 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package com.erggroup.buildtool.ripple;

import java.io.File;
import java.io.FilenameFilter;

import org.slf4j.Logger;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.erggroup.buildtool.utilities.utilities;


/**
 * Process unit test results
 *
 */
public class BuildTestResults
{
    /**
     * Results have been found
     */
    public boolean mResultsFound = false;
    
    /**
     *  A list of items that are collected from all documents 
     */
    public ArrayList<testResultData> mTestResults = new ArrayList<testResultData>();
    
    /**
     * Logger
     * Expect to replace the default logger with one provided by the user
     */
    Logger mLogger;
    
    /**
     * Class to contain the test result data
     * 
     *
     */
    public class testResultData
    {
        public String testName = null;
        public String outcome = null;
        public Long   duration = null;
        public String platform = null;
        public String type = null;
        public String message = null;
            
        public void insertData(String name, String value)
        {
            if (name.equals("DURATION"))
            {
                try {
                    duration = Long.parseLong(value);
                }
                catch(NumberFormatException e)
                {
                    duration = new Long (0);
                }
            }
            else if (name.equals("NAME"))
                testName = value;
            else if (name.equals("OUTCOME"))
                outcome = value;
            else if (name.equals("TARGET"))
                platform = value;
            else if (name.equals("TYPE"))
                type = value;
        }
        
        public void insertMessage(StringBuffer mMessage)
        {
            if (mMessage != null && mMessage.length() > 0 )
                message = mMessage.toString().trim();
        }
        
        public String toString()
        {
            String rv = "Platform:" + platform + " Type:" + type + " Name:" + testName + " Outcome:" + outcome;
            if (message != null)
                rv += " Msg: " + message;
            return (rv);
        }
        
    }
    
    /**
     * 
     * Class to parse the XML
     *
     */
    public class parseXml extends DefaultHandler
    {
 
        //  Attributes collected from the <TestResults> section
        String mTarget;
        String mType;
        String mSection;
        boolean mGatherMessage = false;
        StringBuffer mMessage = new StringBuffer(1024);
        testResultData item = null;
        
        
        public void parseDocument(String fname)
        {
            //System.out.println("Reading XML from: '" + fname + "'");

            // get a factory
            SAXParserFactory spf = SAXParserFactory.newInstance();

            try
            {

                // get a new instance of parser
                SAXParser sp = spf.newSAXParser();

                // parse the file and also register this class for call backs
                sp.parse(fname, this);

            }
            catch (SAXException se)
            {
                se.printStackTrace();
            }
            catch (ParserConfigurationException pce)
            {
                pce.printStackTrace();
            }
            catch (IOException ie)
            {
                ie.printStackTrace();
            }
        }

        // Event Handlers
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
        {
            //System.out.println("Start Element:" + qName + " ,Local Name: " + localName);

            mSection = qName;
            if (qName.equals("TestResults"))
            {
                int length = attributes.getLength();

                // process each attribute
                for (int i = 0; i < length; i++)
                {
                    // get qualified (prefixed) name by index
                    String name = attributes.getQName(i);
                    String value = attributes.getValue(i);
                    
                    if (name.equals("TARGET"))
                        mTarget = value;
                    if (name.equals("TYPE"))
                        mType = value;
                }
            }
            
            if (qName.equals("TestResult"))
            {
                int length = attributes.getLength();
                item = new testResultData();
                mGatherMessage = true;
                
                item.insertData("TARGET", mTarget);
                item.insertData("TYPE", mType);

                // process each attribute
                for (int i = 0; i < length; i++)
                {
                    // get qualified (prefixed) name by index
                    String name = attributes.getQName(i);
                    String value = attributes.getValue(i);
                    
                    //System.out.println("    Name:" + name + ", Value:" + value );
                    item.insertData(name, value);
                }
            }

        }

        public void endElement(String uri, String localName, String qName) throws SAXException
        {
            //System.out.println("End Element:" + qName);
            
            //  Reset the Target and Type
            if (qName.equals("TestResults"))
            {
                mTarget = null;
                mType = null;
            }
            
            if (mGatherMessage)
            {
                if (mMessage.length() > 0)
                {
                    //System.out.println("MESSAGE:" + mMessage);
                    item.insertMessage(mMessage);
                }
                
                mTestResults.add(item);
            }
            
            item = null;
            mSection = null;
            mGatherMessage = false;
            mMessage.setLength(0);
        }
        
        public void characters (char ch[], int start, int length)
        {
            if (mGatherMessage)
            {
                mMessage.append(ch, start, length);
            }
        }

    }
    
    /**
     * Locate and parse unit test results
     * The unit test results WILL BE in a subdirectory of the package called 'utfResults'
     * All .xml files in that directory will be a part of the result set
     *
     * @param dpkg_archive  - Root of dpkg_archive
     * @param mReporting    - Lots of useful information on the package being built
     * @param mlogger       - Logger to use
     */
    public BuildTestResults(String dpkg_archive, ReportingData mReporting, Logger mlogger)
    {
        this.mLogger = mlogger;
        
        //
        //  Locate the package
        //
        String destination = utilities.catDir(dpkg_archive, mReporting.packageName, mReporting.packageVersion);
        
        mLogger.debug("BuildTestResults: Archive:" + destination);
        File pkgDir = new File( destination );
        if (!pkgDir.exists() )
        {
            mLogger.debug("BuildTestResults: Package not found: " + destination);
            return;
        }
        
        File utDir = new File( destination, "utfResults" );
        if (!utDir.exists() )
        {
            mLogger.error("BuildTestResults: Results not found: " + utDir.getAbsolutePath());
            return;
        }
        
        //
        //  Locate all the .xml files in the test directory
        //
        File[] listOfFiles = utDir.listFiles(
                new FilenameFilter() {

                    @Override
                    public boolean accept(File folder, String name) {
                        return name.toLowerCase().endsWith(".xml");
                    }
                });
        
        if ( listOfFiles.length <= 0)
        {
            mLogger.error("BuildTestResults: Results not found: " + utDir.getAbsolutePath());
            return;
        }
        
        //  Process all the selected files
        parseXml data = new parseXml();
        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                mLogger.info("BuildTestResults File: " + listOfFiles[i].getName());
                data.parseDocument( listOfFiles[i].getAbsolutePath());
            }
        }
        
        //
        //  Did we find any results
        //
        mResultsFound = (mTestResults.size() > 0);
    }

    /**
     * Constructor used only for Unit Testing
     */
    public BuildTestResults()
    {

    }

}