File: Parameters.cpp

package info (click to toggle)
xalan 1.12-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,368 kB
  • sloc: cpp: 145,645; xml: 1,523; ansic: 434; sh: 27; makefile: 17
file content (526 lines) | stat: -rw-r--r-- 17,473 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the  "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// Base header file.  Must be first.
#include <xalanc/Include/PlatformDefinitions.hpp>



#include <iomanip>



#include <xalanc/Include/XalanMemoryManagement.hpp>



#include <xalanc/XalanTransformer/XalanParsedSource.hpp>



#include <xalanc/PlatformSupport/DOMStringHelper.hpp>



#include <xalanc/XalanDOM/XalanDocument.hpp>
#include <xalanc/XalanDOM/XalanNode.hpp>
#include <xalanc/XalanDOM/XalanNamedNodeMap.hpp>
#include <xalanc/XalanDOM/XalanDOMString.hpp>



#include <xalanc/Harness/XalanFileUtility.hpp>



#include "Parameters.hpp"
#include "Utils.hpp"



using xalanc::XalanTransformer;
using xalanc::XalanParsedSource;
using xalanc::XalanDocument;
using xalanc::XalanNode;
using xalanc::XalanNamedNodeMap;
using xalanc::XalanDOMString;
using xalanc::WideStringToLong;
using xalanc::compareIgnoreCaseASCII;
using xalanc::XalanMemMgrs;
using xalanc::MemoryManager;
using xalanc::XalanFileUtility;
using xalanc::XSLTInputSource;





using std::endl;



/**
 * Processes parameters for the test harness
 */
Parameters::Parameters(
        const XalanDOMString&   runFileName,
        const XalanDOMString&   testDirectory,
        const XalanDOMString&   resultDirectory,
        const XalanDOMString&   baselineDirectory,
        const XalanDOMString&   reportDirectory,
        XalanFileUtility&       fileUtility,
        Logger&                 logger) :
    m_name("default"),
    m_description(""),
    m_resultDirectory(resultDirectory),
    m_resultFile("results"),
    m_threshold("5"),
    m_baselineDirectory(baselineDirectory),
    m_baselineFile(""),
    m_defaultTestCase(),
    m_testDirectory(testDirectory),
    m_goldDirectory(""),
    m_reportDirectory(reportDirectory),
    m_transformer(),
    m_uniqId(),
    m_initialized(false)
{
    fileUtility.generateUniqRunid(m_uniqId);

    if (parseConfigurationFile(runFileName, logger))
    {
        if (m_testDirectory.empty())
        {
            logger.error() << "No test directory specified on command line or in configuration file" << endl;
        }
        else
        {
            // configure directories
            // result directory
            if (m_resultDirectory.empty())
            {
                m_resultDirectory = getWorkingDirectory();
            }
            m_resultDirectory += getPathSep();

            // gold directory
            if (m_goldDirectory.empty())
            {
                m_goldDirectory = m_testDirectory;
                m_goldDirectory += XalanDOMString("-gold");
            }
            
            // test directory
            m_testDirectory += getPathSep();

            if (readTestCases(fileUtility, logger))
            {
                m_initialized = true;
            }

            // baseline directory
            if (m_baselineDirectory.empty())
            {
                m_baselineDirectory = m_resultDirectory;
            }
            else
            {
                m_baselineDirectory += getPathSep();
            }

            if (m_baselineFile.empty())
            {
                m_baselineFile = m_resultFile;
                m_baselineFile += XalanDOMString("_latest.xml");
            }

            // report directory
            if (m_reportDirectory.empty())
            {
                m_reportDirectory = getWorkingDirectory();
            }
            m_reportDirectory += getPathSep();
        }
    }
}



const TestCasesType& 
Parameters::getTestCases()
{
    return m_testCases;
}



bool
Parameters::parseConfigurationFile(
        const XalanDOMString&   runFileName,
        Logger&                 logger)
{
    // parse the configuration file and get default settings

    const XalanParsedSource* theParsedSource;

    XSLTInputSource confFile(runFileName, m_transformer.getMemoryManager());

    if (m_transformer.parseSource(confFile, theParsedSource) < 0)
    {
        logger.error() << "Failed to parse: " 
                << runFileName.c_str() 
                << ", error: " 
                << m_transformer.getLastError() 
                << endl;

        return false;
    }

    XalanDocument* document = theParsedSource->getDocument();

    XalanNode* runNode = document->getFirstChild();

    while (runNode->getNodeType() != XalanNode::ELEMENT_NODE)
    {
        runNode = runNode->getNextSibling();
        if (0 == runNode)
        {
            logger.error() << "Failed to parse: " 
                << runFileName.c_str() 
                << ", error: no <testconfig> found"
                << endl;

            return false;
        }
    }

    if(!(runNode->getNodeName() == XalanDOMString("testconfig")))
    {
        logger.error() << "Invalid configuration file: "
                << runFileName.c_str()
                << ", error: Missing <testconfig> tag" 
                << endl;

        return false;
    }

    const XalanNamedNodeMap* attributes = runNode->getAttributes();

    if (attributes != 0)
    {
        XalanNode* nameAttribute = attributes->getNamedItem(XalanDOMString("name"));

        if (nameAttribute != 0 &&
            !nameAttribute->getNodeValue().empty())
        {
            m_name = nameAttribute->getNodeValue();
        }
        else
        {
            logger.error() << "Invalid configuration file: "
                << runFileName.c_str()
                << ", error: Missing name attribute" 
                << endl;

            return false;
        }
    }

    XalanNode* currentNode = runNode->getFirstChild();

    while(currentNode != 0)
    {
        if (currentNode->getNodeType() != XalanNode::ELEMENT_NODE)
        {
            currentNode = currentNode->getNextSibling();
            continue;
        }

        // description element 
        if (currentNode->getNodeName() == XalanDOMString("description") &&
            currentNode->getFirstChild() != 0 &&
            currentNode->getFirstChild()->getNodeType() == XalanNode::TEXT_NODE)
        {
            m_description = currentNode->getFirstChild()->getNodeValue();
        }
        // results element
        else if (currentNode->getNodeName() == XalanDOMString("results"))
        {
            // file-path
            const XalanNamedNodeMap* resultAttributes = currentNode->getAttributes();
            XalanNode * attributeNode = resultAttributes->getNamedItem(XalanDOMString("file-path"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE)
            {
                if (!m_resultDirectory.empty())
                {
                    m_resultDirectory += getPathSep();
                }
                m_resultDirectory += attributeNode->getNodeValue();
            }

            // result report file name
            XalanNode * resultsNode = currentNode->getFirstChild();
            if (resultsNode != 0 
                && resultsNode->getNodeType() == XalanNode::TEXT_NODE
                && !resultsNode->getNodeValue().empty())
            {
                m_resultFile = resultsNode->getNodeValue();
            }
        }
        // baseline element
        else if (currentNode->getNodeName() == XalanDOMString("baseline"))
        {
            const XalanNamedNodeMap* attributeParams = currentNode->getAttributes();
            XalanNode* attributeNode;
            // threshold
            attributeNode = attributeParams->getNamedItem(XalanDOMString("threshold"));
            if (attributeNode != 0
                && !attributeNode->getNodeValue().empty())
            {
                m_threshold = attributeNode->getNodeValue().c_str();
            }
            // file-path
            attributeNode = attributeParams->getNamedItem(XalanDOMString("file-path"));
            if (attributeNode != 0)
            {
                if (!m_baselineDirectory.empty())
                {
                    m_baselineDirectory += getPathSep();
                }
                m_baselineDirectory += attributeNode->getNodeValue();
            }
            // baseline file
            XalanNode * baselineNode = currentNode->getFirstChild();
            if (baselineNode != 0
                && baselineNode->getNodeType() == XalanNode::TEXT_NODE
                && !baselineNode->getNodeValue().empty())
            {
                m_baselineFile += baselineNode->getNodeValue();
            }
        }
        // default parameters
        else if (currentNode->getNodeName() == XalanDOMString("default-parameter-set"))
        {
            const XalanNamedNodeMap* attributeParams = currentNode->getAttributes();
            XalanNode* attributeNode;
            attributeNode = attributeParams->getNamedItem(XalanDOMString("input-mode"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE &&
                !attributeNode->getNodeValue().empty())
            {
                m_defaultTestCase.inputMode = attributeNode->getNodeValue(); 
            }
            // number of iterations
            attributeNode = attributeParams->getNamedItem(XalanDOMString("num-iterations"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE &&
                WideStringToLong(attributeNode->getNodeValue().c_str()) > 0)
            {
                m_defaultTestCase.numIterations = WideStringToLong(attributeNode->getNodeValue().c_str()); 
            }
            // minimum time to execute
            attributeNode = attributeParams->getNamedItem(XalanDOMString("min-time-to-execute"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE &&
                WideStringToLong(attributeNode->getNodeValue().c_str()) > 0)
            {
                m_defaultTestCase.minTimeToExecute = WideStringToLong(attributeNode->getNodeValue().c_str());
            }
            // verify the result
            attributeNode = attributeParams->getNamedItem(XalanDOMString("verify-result"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE &&
                compareIgnoreCaseASCII(attributeNode->getNodeValue().c_str(),XalanDOMString("yes").c_str()) == 0)
            {
                m_defaultTestCase.verifyResult = true;
            }
            // get processor specific parameters
            XalanNode * options = currentNode->getFirstChild();
            while (options != 0)
            {
                if (options->getNodeType() != XalanNode::ELEMENT_NODE)
                {
                    options = options->getNextSibling();
                    continue;
                }

                const XalanDOMString* processorName = 0;

                const XalanNamedNodeMap* optionAttributes = options->getAttributes();
                XalanNode* processorOption = optionAttributes->getNamedItem(XalanDOMString("processor"));
                if (processorOption != 0 &&
                    processorOption->getNodeType() == XalanNode::ATTRIBUTE_NODE)
                {
                    processorName = &(processorOption->getNodeValue());
                }

                if (processorName != 0 && processorName->empty() == true)
                {
                    options = options->getNextSibling();
                    continue;
                }

                if (options->getNodeName() == XalanDOMString("init-options"))
                {
                    m_defaultTestCase.processorOptions[*processorName].initOptions = options->getFirstChild();
                }
                else if (options->getNodeName() == XalanDOMString("compile-options"))
                {
                    m_defaultTestCase.processorOptions[*processorName].compileOptions = options->getFirstChild();
                }
                else if (options->getNodeName() == XalanDOMString("parse-options"))
                {
                    m_defaultTestCase.processorOptions[*processorName].parseOptions = options->getFirstChild();
                }
                else if (options->getNodeName() == XalanDOMString("result-options"))
                {
                    m_defaultTestCase.processorOptions[*processorName].resultOptions = options->getFirstChild();
                }
                else if (options->getNodeName() == XalanDOMString("transform-options"))
                {
                    m_defaultTestCase.processorOptions[*processorName].transformOptions = options->getFirstChild();
                }

                options = options->getNextSibling();
            }
        }
        else if (currentNode->getNodeName() == XalanDOMString("testcases"))
        {
            // file-path
            const XalanNamedNodeMap* testCasesAttributes = currentNode->getAttributes();
            XalanNode * attributeNode = testCasesAttributes->getNamedItem(XalanDOMString("file-path"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE)
            {
                if (!m_testDirectory.empty())
                {
                    m_testDirectory += getPathSep();
                }
                m_testDirectory += attributeNode->getNodeValue();
            }
            // gold file-path
            attributeNode = testCasesAttributes->getNamedItem(XalanDOMString("gold-file-path"));
            if (attributeNode != 0 &&
                attributeNode->getNodeType() == XalanNode::ATTRIBUTE_NODE)
            {
                m_goldDirectory = attributeNode->getNodeValue();
            }
        }
        currentNode = currentNode->getNextSibling();
    }

    return true;
}



bool
Parameters::readTestCases(
    XalanFileUtility&   fileUtility,
    Logger&             logger)
{
    if (!fileUtility.checkDir(m_testDirectory))
    {
        logger.error() << "Invalid test directory: " << m_testDirectory.c_str() << endl;
        return false;
    }

    if (m_goldDirectory.empty())
    {
        logger.message() << "No gold directory specified" << endl;
        if (!fileUtility.checkDir(m_goldDirectory))
        {
            logger.warning() << "Invalid gold directory: " << m_goldDirectory.c_str() << endl;
        }
    }

    fileUtility.checkAndCreateDir(m_resultDirectory);

    typedef XalanFileUtility::FileNameVectorType FileNamesType;
    FileNamesType dirNames;

    fileUtility.getDirectoryNames(m_testDirectory, dirNames);

    FileNamesType::iterator dirIter = dirNames.begin();
    
    // for each test directory
    while (dirIter != dirNames.end())
    {
       
        FileNamesType xslTestFiles;
        fileUtility.getTestFileNames(m_testDirectory, *dirIter, true, xslTestFiles);

        FileNamesType::const_iterator xslIter = xslTestFiles.begin();

        // for each stylesheet
        while (xslIter != xslTestFiles.end())
        {
            // configure the stylesheet
            TestCase testCase = m_defaultTestCase;
            testCase.stylesheet = m_testDirectory;
            testCase.stylesheet += *dirIter;
            testCase.stylesheet += getPathSep();
            testCase.stylesheet += *xslIter;

            
            bool status = true;

            // configure the input document
            fileUtility.generateFileName(testCase.stylesheet, "xml", testCase.inputDocument, &status);
            if (status != true)
            {
                logger.warning() << "No matching input file for: " << testCase.stylesheet.c_str() << endl;
                ++xslIter;
                continue;
            }

            // configure result directory
            testCase.resultDirectory = m_resultDirectory;
            testCase.resultDirectory += *dirIter;
 
            // configure result document        
            XalanDOMString outFile = testCase.resultDirectory;
            outFile += getPathSep();
            outFile += *xslIter;

            fileUtility.generateFileName(outFile, "out", testCase.resultDocument);

            // configure gold result
            outFile = m_goldDirectory;
            outFile += getPathSep();
            outFile += *dirIter;
            outFile += getPathSep();
            outFile += *xslIter;

            status = true;
            fileUtility.generateFileName(outFile, "out", testCase.goldResult, &status);
            if (true == testCase.verifyResult && !status)
            {
                logger.warning() << "Verification on, but no matching gold file: " << testCase.goldResult.c_str() << endl;
            }

            m_testCases.push_back(testCase);
            
            ++xslIter;
        }

        ++dirIter;
    }

    return true;
}