Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
6914 dpurdie 1
/**
2
 * 
3
 */
4
package com.erggroup.buildtool.ripple;
5
 
6
import java.io.File;
7
import java.io.FilenameFilter;
8
 
7033 dpurdie 9
import org.slf4j.Logger;
6914 dpurdie 10
 
11
import java.io.IOException;
12
import java.util.ArrayList;
13
 
14
import javax.xml.parsers.ParserConfigurationException;
15
import javax.xml.parsers.SAXParser;
16
import javax.xml.parsers.SAXParserFactory;
17
 
18
import org.xml.sax.Attributes;
19
import org.xml.sax.SAXException;
20
import org.xml.sax.helpers.DefaultHandler;
21
 
22
import com.erggroup.buildtool.utilities.utilities;
23
 
24
 
25
/**
26
 * Process unit test results
27
 *
28
 */
29
public class BuildTestResults
30
{
31
    /**
32
     * Results have been found
33
     */
34
    public boolean mResultsFound = false;
35
 
36
    /**
37
     *  A list of items that are collected from all documents 
38
     */
39
    public ArrayList<testResultData> mTestResults = new ArrayList<testResultData>();
40
 
41
    /**
42
     * Logger
43
     * Expect to replace the default logger with one provided by the user
44
     */
45
    Logger mLogger;
46
 
47
    /**
48
     * Class to contain the test result data
49
     * 
50
     *
51
     */
52
    public class testResultData
53
    {
54
        public String testName = null;
55
        public String outcome = null;
56
        public Long   duration = null;
57
        public String platform = null;
58
        public String type = null;
59
        public String message = null;
60
 
61
        public void insertData(String name, String value)
62
        {
63
            if (name.equals("DURATION"))
64
            {
65
                try {
66
                    duration = Long.parseLong(value);
67
                }
68
                catch(NumberFormatException e)
69
                {
70
                    duration = new Long (0);
71
                }
72
            }
73
            else if (name.equals("NAME"))
74
                testName = value;
75
            else if (name.equals("OUTCOME"))
76
                outcome = value;
77
            else if (name.equals("TARGET"))
78
                platform = value;
79
            else if (name.equals("TYPE"))
80
                type = value;
81
        }
82
 
83
        public void insertMessage(StringBuffer mMessage)
84
        {
85
            if (mMessage != null && mMessage.length() > 0 )
86
                message = mMessage.toString().trim();
87
        }
88
 
89
        public String toString()
90
        {
91
            String rv = "Platform:" + platform + " Type:" + type + " Name:" + testName + " Outcome:" + outcome;
92
            if (message != null)
93
                rv += " Msg: " + message;
94
            return (rv);
95
        }
96
 
97
    }
98
 
99
    /**
100
     * 
101
     * Class to parse the XML
102
     *
103
     */
104
    public class parseXml extends DefaultHandler
105
    {
106
 
107
        //  Attributes collected from the <TestResults> section
108
        String mTarget;
109
        String mType;
110
        String mSection;
111
        boolean mGatherMessage = false;
112
        StringBuffer mMessage = new StringBuffer(1024);
113
        testResultData item = null;
114
 
115
 
116
        public void parseDocument(String fname)
117
        {
118
            //System.out.println("Reading XML from: '" + fname + "'");
119
 
120
            // get a factory
121
            SAXParserFactory spf = SAXParserFactory.newInstance();
122
 
123
            try
124
            {
125
 
126
                // get a new instance of parser
127
                SAXParser sp = spf.newSAXParser();
128
 
129
                // parse the file and also register this class for call backs
130
                sp.parse(fname, this);
131
 
132
            }
133
            catch (SAXException se)
134
            {
135
                se.printStackTrace();
136
            }
137
            catch (ParserConfigurationException pce)
138
            {
139
                pce.printStackTrace();
140
            }
141
            catch (IOException ie)
142
            {
143
                ie.printStackTrace();
144
            }
145
        }
146
 
147
        // Event Handlers
148
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
149
        {
150
            //System.out.println("Start Element:" + qName + " ,Local Name: " + localName);
151
 
152
            mSection = qName;
153
            if (qName.equals("TestResults"))
154
            {
155
                int length = attributes.getLength();
156
 
157
                // process each attribute
158
                for (int i = 0; i < length; i++)
159
                {
160
                    // get qualified (prefixed) name by index
161
                    String name = attributes.getQName(i);
162
                    String value = attributes.getValue(i);
163
 
164
                    if (name.equals("TARGET"))
165
                        mTarget = value;
166
                    if (name.equals("TYPE"))
167
                        mType = value;
168
                }
169
            }
170
 
171
            if (qName.equals("TestResult"))
172
            {
173
                int length = attributes.getLength();
174
                item = new testResultData();
175
                mGatherMessage = true;
176
 
177
                item.insertData("TARGET", mTarget);
178
                item.insertData("TYPE", mType);
179
 
180
                // process each attribute
181
                for (int i = 0; i < length; i++)
182
                {
183
                    // get qualified (prefixed) name by index
184
                    String name = attributes.getQName(i);
185
                    String value = attributes.getValue(i);
186
 
187
                    //System.out.println("    Name:" + name + ", Value:" + value );
188
                    item.insertData(name, value);
189
                }
190
            }
191
 
192
        }
193
 
194
        public void endElement(String uri, String localName, String qName) throws SAXException
195
        {
196
            //System.out.println("End Element:" + qName);
197
 
198
            //  Reset the Target and Type
199
            if (qName.equals("TestResults"))
200
            {
201
                mTarget = null;
202
                mType = null;
203
            }
204
 
205
            if (mGatherMessage)
206
            {
207
                if (mMessage.length() > 0)
208
                {
209
                    //System.out.println("MESSAGE:" + mMessage);
210
                    item.insertMessage(mMessage);
211
                }
212
 
213
                mTestResults.add(item);
214
            }
215
 
216
            item = null;
217
            mSection = null;
218
            mGatherMessage = false;
219
            mMessage.setLength(0);
220
        }
221
 
222
        public void characters (char ch[], int start, int length)
223
        {
224
            if (mGatherMessage)
225
            {
226
                mMessage.append(ch, start, length);
227
            }
228
        }
229
 
230
    }
231
 
232
    /**
233
     * Locate and parse unit test results
234
     * The unit test results WILL BE in a subdirectory of the package called 'utfResults'
235
     * All .xml files in that directory will be a part of the result set
236
     *
237
     * @param dpkg_archive  - Root of dpkg_archive
238
     * @param mReporting    - Lots of useful information on the package being built
239
     * @param mlogger       - Logger to use
240
     */
241
    public BuildTestResults(String dpkg_archive, ReportingData mReporting, Logger mlogger)
242
    {
243
        this.mLogger = mlogger;
244
 
245
        //
246
        //  Locate the package
247
        //
248
        String destination = utilities.catDir(dpkg_archive, mReporting.packageName, mReporting.packageVersion);
249
 
250
        mLogger.debug("BuildTestResults: Archive:" + destination);
251
        File pkgDir = new File( destination );
252
        if (!pkgDir.exists() )
253
        {
7176 dpurdie 254
            mLogger.debug("BuildTestResults: Package not found: {} ", destination);
6914 dpurdie 255
            return;
256
        }
257
 
258
        File utDir = new File( destination, "utfResults" );
259
        if (!utDir.exists() )
260
        {
7176 dpurdie 261
            mLogger.warn("BuildTestResults: Results not found: {}", utDir.getAbsolutePath());
6914 dpurdie 262
            return;
263
        }
264
 
265
        //
266
        //  Locate all the .xml files in the test directory
267
        //
268
        File[] listOfFiles = utDir.listFiles(
269
                new FilenameFilter() {
270
 
271
                    @Override
272
                    public boolean accept(File folder, String name) {
273
                        return name.toLowerCase().endsWith(".xml");
274
                    }
275
                });
276
 
277
        if ( listOfFiles.length <= 0)
278
        {
7176 dpurdie 279
            mLogger.warn("BuildTestResults: Results not found: {}", utDir.getAbsolutePath());
6914 dpurdie 280
            return;
281
        }
282
 
283
        //  Process all the selected files
284
        parseXml data = new parseXml();
285
        for (int i = 0; i < listOfFiles.length; i++)
286
        {
287
            if (listOfFiles[i].isFile())
288
            {
7176 dpurdie 289
                mLogger.debug("BuildTestResults File: {}", listOfFiles[i].getName());
6914 dpurdie 290
                data.parseDocument( listOfFiles[i].getAbsolutePath());
291
            }
292
        }
293
 
294
        //
295
        //  Did we find any results
296
        //
297
        mResultsFound = (mTestResults.size() > 0);
298
    }
299
 
300
    /**
301
     * Constructor used only for Unit Testing
302
     */
303
    public BuildTestResults()
304
    {
305
 
306
    }
307
 
308
}