Project

Profile

Help

How to connect?
Download (71.8 KB) Statistics
| Branch: | Revision:

he / src / main / c / samples / cppTests / testXSLT30.cpp @ f299305b

1

    
2
#include <sstream>
3
#include <stdio.h>
4

    
5
#include "../../Saxon.C.API/SaxonProcessor.h"
6
#include "../../Saxon.C.API/XdmValue.h"
7
#include "../../Saxon.C.API/XdmItem.h"
8
#include "../../Saxon.C.API/XdmNode.h"
9
#include "../../Saxon.C.API/XdmFunctionItem.h"
10
#include "../../Saxon.C.API/XdmMap.h"
11
#include "../../Saxon.C.API/XdmArray.h"
12
#include "cppExtensionFunction.h"
13
#include "CppTestUtils.h"
14
#include <string>
15
#include <thread>
16

    
17

    
18

    
19

    
20
using namespace std;
21

    
22

    
23
#ifdef MEM_DEBUG
24
#define new new(__FILE__, __LINE__)
25
#endif
26

    
27

    
28

    
29

    
30

    
31
char fname[] = "_nativeCall";
32
char funcParameters[] = "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;";
33

    
34
JNINativeMethod cppMethods[] =
35
        {
36
                {
37
                        fname,
38
                        funcParameters,
39
                        (void *) &cppExtensionFunction::cppNativeCall
40
                }
41
        };
42

    
43

    
44
/*
45
* Test transform to String. Source and stylesheet supplied as arguments
46
*/
47
void testApplyTemplatesString1(Xslt30Processor *trans, sResultCount *sresult) {
48

    
49
    cout << "Test: testApplyTemplatesString1:" << endl;
50
//    trans->setupXslMessage(false);
51

    
52
    XsltExecutable *executable = trans->compileFromFile("test.xsl");
53

    
54
    if(executable== nullptr) {
55
        sresult->failure++;
56
        sresult->failureList.push_back("testApplyTemplatesString1");
57
        cerr << "testApplyTemplatesString1 NULL found" << endl;
58
        if (trans->exceptionOccurred()) {
59
            cerr <<"testApplyTemplatesString1 error: "<< trans->getErrorMessage() << endl;
60
        }
61
        return;
62
    }
63
    executable->setInitialMatchSelectionAsFile("cat.xml");
64

    
65
    const char *output = executable->applyTemplatesReturningString();
66
    if (output == nullptr) {
67
        printf("result is null ====== FAIL ====== \n");
68
        sresult->failure++;
69
        fflush(stdout);
70
        sresult->failureList.push_back("testApplyTemplatesString1-0");
71
    } else if (string(output).find(string("<out>text2</out>")) != std::string::npos) {
72
        printf("%s", output);
73
        printf("result is OK \n");
74
        sresult->success++;
75
        delete output;
76
    } else {
77
        printf("result is null ====== FAIL ====== \n");
78
        sresult->failure++;
79
        sresult->failureList.push_back("testApplyTemplatesString1-1");
80
            std::cout<<"output="<<output<<std::endl;
81
        delete output;
82
    }
83
    fflush(stdout);
84
    delete executable;
85

    
86
}
87

    
88
/*
89
* Test transform to String. Source and stylesheet supplied as arguments
90
*/
91
void testTransformToStringExtensionFunc(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
92

    
93
    cout << endl << "Test: TransformToStringExtensionFunc:" << endl;
94
    bool nativeFound = processor->registerNativeMethods(SaxonProcessor::sxn_environ->env,
95
                                                        "com/saxonica/functions/extfn/cpp/NativeCall",
96
                                                        cppMethods, sizeof(cppMethods) / sizeof(cppMethods[0]));
97

    
98
    XsltExecutable * executable = trans->compileFromFile("testExtension.xsl");
99

    
100
    if(executable== nullptr) {
101
        sresult->failure++;
102
        sresult->failureList.push_back("TransformToStringExtensionFunc");
103
        cerr << "TransformToStringExtensionFunc NULL found" << endl;
104
        if (trans->exceptionOccurred()) {
105
            cerr <<"TransformToStringExtensionFunc error: "<< trans->getErrorMessage() << endl;
106
        }
107
        return;
108
    }
109

    
110
    executable->setProperty("extc", "home/ond1/work/svn/latest9.9-saxonc/samples/cppTests/cppExtensionFunction");
111

    
112
    if (nativeFound) {
113
        const char *output = executable->transformFileToString("cat.xml");
114

    
115
        if (output == nullptr) {
116
            SaxonProcessor::sxn_environ->env->ExceptionDescribe();
117
            printf("result is null ====== FAIL ======  \n");
118
            sresult->failure++;
119
            sresult->failureList.push_back("testTransformToStringExnteionFunc");
120
        } else {
121
            sresult->success++;
122
            printf("result is OK \n");
123
        }
124
        fflush(stdout);
125
        delete output;
126
    } else {
127
        printf("native Class not foun ====== FAIL ====== ");
128
        sresult->failure++;
129
        sresult->failureList.push_back("testTransformToStringExtensionFunc");
130
    }
131
    delete executable;
132
}
133

    
134

    
135
/*
136
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
137
*/
138
void testApplyTemplatesString2(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
139

    
140
    cout << "Test: testApplyTemplatesString2:" << endl;
141

    
142

    
143
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
144

    
145
    if (input== nullptr) {
146
        cout << "Source document is null." << endl;
147
        if(processor->exceptionOccurred()) {
148
            cerr<<processor->getErrorMessage()<<endl;
149
        }
150
        sresult->failure++;
151
        sresult->failureList.push_back("testApplyTemplatesString2");
152
        return;
153

    
154
    }
155
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
156
    if(executable == nullptr) {
157
        sresult->failure++;
158
        sresult->failureList.push_back("testApplyTemplatesString2");
159
        if (trans->exceptionOccurred()) {
160
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
161
        }
162
        delete input;
163
        return;
164
    }
165

    
166
    executable->setInitialMatchSelection((XdmValue *) input);
167

    
168
    const char *output = executable->applyTemplatesReturningString();
169
    if (output== nullptr) {
170
        printf("result is null ====== FAIL ======  \n");
171
        sresult->failureList.push_back("testApplyTemplatesString2");
172
        sresult->failure++;
173
    } else {
174
        sresult->success++;
175
        printf("%s", output);
176
        printf("result is OK \n");
177
        delete output;
178
    }
179
    fflush(stdout);
180
    delete input;
181
    delete executable;
182
}
183

    
184
/*
185
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
186
Should be error. Stylesheet file does not exist
187
*/
188
void testApplyTemplates2a_Error(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
189

    
190
    cout << "Test: TransformToString2a_Error:" << endl;
191

    
192
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
193

    
194
    if (input == nullptr) {
195
        cout << "Source document is null. ====== FAIL ======" << endl;
196
        if(processor->exceptionOccurred()) {
197
            cerr<<processor->getErrorMessage()<<endl;
198
        }
199
        sresult->failure++;
200
        sresult->failureList.push_back("testApplyTemplates2a_Error");
201
        trans->exceptionClear();
202
        return;
203
    }
204
    XsltExecutable * executable = trans->compileFromFile("test-error.xsl");
205

    
206
    if(executable== nullptr) {
207
        printf("Expected result is null \n");
208
        sresult->success++;
209

    
210
        if (trans->exceptionOccurred()) {
211
            cerr <<"testApplyTemplates2a_Error error: "<< trans->getErrorMessage() << endl;
212
        }
213
        delete input;
214
        trans->exceptionClear();
215
        return;
216
    }
217

    
218
    sresult->failure++;
219
    sresult->failureList.push_back("testApplyTemplates2a_Error");
220
    fflush(stdout);
221

    
222
    delete executable;
223

    
224
}
225

    
226
/*
227
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
228
Should be error. Source file does not exist
229
*/
230
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
231

    
232
    cout << endl << "Test: TransformToString2b:" << endl;
233

    
234
    const char * result  = trans->transformFileToString("cat-error.xml", "test-error.xsl");
235

    
236
    if(result== nullptr) {
237
        if(trans->exceptionOccurred()) {
238
            cerr<<"Expected failure = "<<trans->getErrorMessage()<<endl;
239
        }
240
        sresult->success++;
241
        trans->exceptionClear();
242
        return;
243
    }else {
244
        delete result;
245
        sresult->failure++;
246
        sresult->failureList.push_back("testTransformToString2b");
247
        cerr << "testTransformToString2b NULL found" << endl;
248
        if (trans->exceptionOccurred()) {
249
            cerr <<"Error: "<< trans->getErrorMessage() << endl;
250
        }
251
        trans->exceptionClear();
252
        return;
253
    }
254

    
255

    
256
}
257

    
258

    
259
/*
260
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
261
and integer parmater created and supplied
262
*/
263
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
264

    
265
    cout << endl << "Test: testTransformToString3" << endl;
266

    
267
    XdmNode *inputi = processor->parseXmlFromString(
268
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
269

    
270
    if (inputi== nullptr) {
271
        cout << "Source document inputi is null." << endl;
272
        if (trans->exceptionOccurred()) {
273
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
274
        }
275
        sresult->failure++;
276
        sresult->failureList.push_back("testTransformToString3");
277

    
278
        return;
279
    }
280

    
281
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
282

    
283
    if (value1== nullptr) {
284
        cout << "value1 is null." << endl;
285
        if (trans->exceptionOccurred()) {
286
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
287
        }
288
        sresult->failure++;
289
        sresult->failureList.push_back("testTransformToString3");
290
        delete inputi;
291
        return;
292
    }
293

    
294
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
295

    
296
    if(executable== nullptr) {
297
        sresult->failure++;
298
        sresult->failureList.push_back("testTransformToString3");
299
        cerr << "testTransformToString3 NULL found" << endl;
300
        if (trans->exceptionOccurred()) {
301
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
302
        }
303
        delete value1;
304
        return;
305
    }
306

    
307

    
308
    executable->setParameter("numParam", (XdmValue *) value1);
309

    
310
    executable->setInitialMatchSelection((XdmNode *) inputi);
311
    const char *output = executable->applyTemplatesReturningString();
312
    if (output == nullptr) {
313
        printf("result is null ====== FAIL ====== \n");
314
        sresult->failureList.push_back("testTransformToString3");
315
    } else {
316
        printf("%s", output);
317
        printf("result is OK \n");
318
        delete output;
319
    }
320
    fflush(stdout);
321

    
322
    delete value1;
323
    delete inputi;
324
    inputi = nullptr;
325
    delete executable;
326

    
327
}
328

    
329
/*
330
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
331
and integer parmater created and supplied
332
*/
333
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
334

    
335
    cout <<  "Test: testTransformToString4:" << endl;
336

    
337
    XdmNode *input = processor->parseXmlFromString(
338
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
339

    
340
    if (input== nullptr) {
341
        cout << "Source document is null. ====== FAIL ====== " << endl;
342
        sresult->failure++;
343
        sresult->failureList.push_back("testTransformToString4");
344
    }
345

    
346

    
347
    XdmValue *values = new XdmValue();
348
    XdmAtomicValue * a1 = processor->makeIntegerValue(10);
349
    XdmAtomicValue * a2 = processor->makeIntegerValue(5);
350
    XdmAtomicValue * a3 = processor->makeIntegerValue(6);
351
    XdmAtomicValue * a4 = processor->makeIntegerValue(7);
352
    values->addXdmItem(a1);
353
    values->addXdmItem(a2);
354
    values->addXdmItem(a3);
355
    values->addXdmItem(a4);
356

    
357

    
358
    XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
359

    
360
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
361

    
362
    if(executable== nullptr) {
363
        sresult->failure++;
364
        sresult->failureList.push_back("testTransformToString4");
365
        cerr << "testTransformToString4 NULL found" << endl;
366
        if (trans->exceptionOccurred()) {
367
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
368
        }
369
        delete values;
370
        return;
371
    }
372

    
373
    executable->setParameter("values", (XdmValue *) values);
374
    executable->setInitialMatchSelection((XdmNode *) input);
375
    const char *output = executable->applyTemplatesReturningString();
376
    if (output== nullptr) {
377
        printf("result is null \n");
378
        sresult->failure++;
379
        sresult->failureList.push_back("testTransformToString4");
380
    } else {
381
        printf("%s", output);
382
        printf("result is OK \n");
383
        delete output;
384
    }
385
    fflush(stdout);
386
    delete sheet;
387
    delete executable;
388
    delete input;
389
    delete values;
390

    
391
}
392

    
393
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
394
    cout << endl << "Test: testTransfromFromstring: "<< endl;
395

    
396
    if(processor == nullptr) {
397
        cout<<" processor is null"<<endl;
398
        return;
399

    
400
    }
401
    XdmNode *input = processor->parseXmlFromString(
402
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
403

    
404
    if(input == nullptr) {
405
        sresult->failure++;
406
        sresult->failureList.push_back("testTransformFromstring");
407
        cerr << "testTransformFromstring NULL found" << endl;
408
        if (trans->exceptionOccurred()) {
409
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
410
        }
411
        return;
412
    }
413

    
414
    XsltExecutable * executable = trans->compileFromString(
415
            "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out></xsl:for-each></output></xsl:template></xsl:stylesheet>");
416

    
417
    if(executable== nullptr) {
418
        sresult->failure++;
419
        sresult->failureList.push_back("testTransformFromstring");
420
        cerr << "testTransformFromstring NULL found" << endl;
421
        if (trans->exceptionOccurred()) {
422
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
423
        }
424
        delete input;
425
        return;
426
    }
427

    
428
    const char *output = executable->transformToString((XdmNode *) input);
429
    if (output== nullptr) {
430
        printf("result is null ====== FAIL ====== \n");
431
        sresult->failure++;
432
        sresult->failureList.push_back("testTransformFromString");
433
        if (trans->exceptionOccurred()) {
434
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
435
        }
436
    } else {
437
        printf("%s", output);
438
        printf("result is OK \n");
439
        sresult->success++;
440
        delete output;
441
    }
442
    fflush(stdout);
443
    delete input;
444
    delete executable;
445

    
446

    
447
}
448

    
449
//Test case has error in the stylesheet
450
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
451
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
452

    
453
    XsltExecutable * executable = trans->compileFromString(
454
            "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out><xsl:for-each></output></xsl:template><xsl:stylesheet>");
455

    
456
    if(executable== nullptr) {
457
        sresult->success++;
458

    
459
        if (trans->exceptionOccurred()) {
460
            cerr <<"Error expected: "<< trans->getErrorMessage() << endl;
461
        }
462
        trans->exceptionClear();
463
        return;
464
    }
465

    
466

    
467
    sresult->failure++;
468
    sresult->failureList.push_back("testTransfromFromstring2-Error");
469
    delete executable;
470

    
471
}
472

    
473
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
474

    
475
    cout << endl << "Test: TrackingOfValueReference:" << endl;
476
    ostringstream test;
477
    ostringstream valStr;
478
    ostringstream name;
479
    for (int i = 0; i < 10; i++) {
480
        test << "v" << i;
481
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
482
               << "</value></out>";
483
        name << "value" << i;
484

    
485
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
486
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
487
        trans->setParameter(name.str().c_str(), values);
488
        test.str("");
489
        valStr.str("");
490
        name.str("");
491

    
492
        if(values == nullptr) {
493
            cerr<<"TrackingOfValueReference failed to create XdmNode object" <<endl;
494
            sresult->failure++;
495
            sresult->failureList.push_back("TrackingOfValueReference");
496

    
497
        }
498
    }
499

    
500
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
501
    if (parMap.size() > 0) {
502
        //cout << "Parameter size: " << parMap.size() << endl;
503
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
504
        ostringstream name1;
505
        for (int i = 0; i < 10; i++) {
506
            name1 << "sparam:value" << i;
507
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
508
            XdmValue *valuei = parMap[name1.str()];
509
            if (valuei != nullptr) {
510
                cout << name1.str();
511
                if (valuei->itemAt(0) != nullptr)
512
                    cout << "= " << valuei->itemAt(0)->getStringValue();
513
                cout << endl;
514

    
515
            } else {
516
                sresult->failure++;
517
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
518
                sresult->failureList.push_back("testTrackingOfValueReference");
519
                return;
520
            }
521
            name1.str("");
522
        }
523
    }
524
    if(parMap.size() > 0 ) {
525
        cerr<<"Deleting map"<<endl;
526
        trans->clearParameters(true);
527
    }
528
    sresult->success++;
529
}
530

    
531
/*Test case should be error.*/
532
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
533
    trans->clearParameters();
534

    
535
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
536
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
537
    ostringstream test;
538
    ostringstream valStr;
539
    ostringstream name;
540
    for (int i = 0; i < 2; i++) {
541
        test << "v" << i;
542
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
543
               << "<value></out>";
544
        name << "value" << i;
545

    
546
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
547
        if(values == nullptr) {
548
            cerr<<"values NULL ====== FAIL ======="<<endl;
549
            processor->exceptionClear();
550
        } else {
551
            trans->setParameter(name.str().c_str(), values);
552
        }
553
        test.str("");
554
        valStr.str("");
555
        name.str("");
556
    }
557
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
558
    cout << "Parameter Map size: " << parMap.size() << endl;
559

    
560
    ostringstream name1;
561
    bool errorFound = false;
562
    for (int i = 0; i < 2; i++) {
563
        name1 << "sparam:value" << i;
564
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
565
        try {
566

    
567
            XdmValue *valuei = parMap.at(name1.str());
568
            if (valuei != NULL) {
569
                cout << name1.str();
570
                if (valuei->itemAt(0) != NULL)
571
                    cout << "= " << valuei->itemAt(0)->getStringValue();
572
                cout << endl;
573
            }
574
        } catch (const std::out_of_range &oor) {
575
            cout << "Out of range exception occurred. Exception " << endl;
576
            if (!errorFound) {
577
                sresult->success++;
578
                errorFound = true;
579

    
580
                return;
581
            }
582
        }
583
        name1.str("");
584
    }
585
    sresult->failure++;
586
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
587

    
588
}
589

    
590
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
591
    trans->clearParameters();
592

    
593

    
594

    
595
    XsltExecutable * executable = trans->compileFromString(
596
            "<?xml version='1.0'?><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'                 xmlns:xs='http://www.w3.org/2001/XMLSchema' version='3.0' exclude-result-prefixes='#all'>     <xsl:import-schema><xs:schema><xs:element name='x' type='xs:int'/></xs:schema></xsl:import-schema> <xsl:template name='main'>          <xsl:result-document validation='strict'> <x>3</x>   </xsl:result-document>    </xsl:template>    </xsl:stylesheet>");
597

    
598
    const char *rootValue = executable->callTemplateReturningString("main");
599

    
600

    
601
    if (rootValue== nullptr) {
602
        std::cout << "NULL found" << std::endl;
603
        sresult->failure++;
604
        sresult->failureList.push_back("testValidation");
605
        return;
606

    
607
    } else {
608
        std::cout << "Result=" << rootValue << endl;
609
        sresult->success++;
610
    }
611
    delete executable;
612
}
613

    
614

    
615
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
616

    
617
    std::cout << "testXdmNodeOutput" << std::endl;
618
    XsltExecutable * executable = trans->compileFromString(
619
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
620
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
621
    if (rootValue== nullptr) {
622
        cout << "Result is null ====== FAIL ====== " << endl;
623
        sresult->failure++;
624
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
625
        return;
626
    }
627
    XdmItem *rootItem = rootValue->getHead();
628
    if (rootItem== nullptr) {
629

    
630
        cout << "Result is null ====== FAIL ====== " << endl;
631
        sresult->failure++;
632
        sresult->failureList.push_back("testXdmNodeOutput-0");
633
        delete rootValue;
634
        delete executable;
635
        return;
636
    }
637
    XdmNode *root = (XdmNode *) rootItem;
638
    if (root->getNodeKind() == DOCUMENT) {
639
        cout << "Result is a Document" << endl;
640
    } else {
641
        cout << "Node is of kind:" << root->getNodeKind() << endl;
642
    }
643
    const char *result = executable->callTemplateReturningString("go");
644
    if (string(result).find(string("<a/>")) != std::string::npos) {
645
        sresult->success++;
646
        delete result;
647
    } else {
648
        //TODO - this test case prints the XML declaration. Check if this correct
649
        sresult->failure++;
650
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
651
        sresult->failureList.push_back("testXdmNodeOutput");
652
    }
653
    delete rootValue;
654
    delete executable;
655

    
656
}
657

    
658
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
659
    cout << "ExampleSimple1 taken from PHP:" << endl;
660

    
661
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
662
    if(executable== nullptr) {
663
        sresult->failure++;
664
        sresult->failureList.push_back("exampleSimple1");
665
        cerr << "exampleSimple1 NULL found" << endl;
666
        if (proc->exceptionOccurred()) {
667
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
668
        }
669
        return;
670
    }
671
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
672
    const char *result = executable->applyTemplatesReturningString();
673
    if (result != NULL) {
674
        cout << result << endl;
675
        sresult->success++;
676
        delete result;
677
    } else {
678
        cout << "Result is null ====== FAIL ====== " << endl;
679
        sresult->failure++;
680
    }
681
    proc->clearParameters();
682
    delete executable;
683
}
684

    
685
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
686
    cout << "ExampleSimple1Err taken from PHP:" << endl;
687

    
688
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
689
    if(executable == nullptr || proc->exceptionOccurred()) {
690
        if( proc->exceptionOccurred()) {
691
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
692
        }
693
        cout << "Result expected as null " << endl;
694
        proc->exceptionClear();
695
        sresult->success++;
696
        return;
697
    } else {
698
        sresult->failure++;
699
        sresult->failureList.push_back("exampleSimple1Err");
700

    
701
        delete executable;
702
    }
703

    
704
}
705

    
706

    
707
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
708
    cout << "<b>exampleSimple2:</b><br/>" << endl;
709

    
710
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
711
    if(executable== nullptr) {
712
        sresult->failure++;
713
        sresult->failureList.push_back("exampleSimple2");
714
        cerr << "exampleSimple2 NULL found" << endl;
715
        if (proc->exceptionOccurred()) {
716
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
717
        }
718
        return;
719
    }
720

    
721
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
722
    const char *filename = "output1.xml";
723
    executable->setOutputFile(filename);
724
    executable->applyTemplatesReturningFile("output1.xml");
725

    
726
    if (CppTestUtils::exists("output1.xml")) {
727
        cout << "The file $filename exists" << endl;
728
        remove("output1.xml");
729
        sresult->success++;
730
    } else {
731
        cout << "The file " << filename << " does not exist" << endl;
732
        if (executable->exceptionOccurred()) {
733
            cout << proc->getErrorMessage() << endl;
734
        }
735
        sresult->failure++;
736
        sresult->failureList.push_back("exampleSimple2");
737
    }
738

    
739
    delete executable;
740

    
741
}
742

    
743
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
744
    cout << "<b>exampleSimple3:</b><br/>" << endl;
745
    proc->clearParameters();
746

    
747
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
748
    if (xdmNode== nullptr) {
749
        cout << "Error: xdmNode is null'" << endl;
750
        if(saxonProc->exceptionOccurred()) {
751
            cout<<"Error message="<<saxonProc->getErrorMessage()<<endl;
752
        }
753
        sresult->failure++;
754
        sresult->failureList.push_back("exampleSimple3");
755
        return;
756
    }
757

    
758
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
759

    
760
   if(executable == nullptr) {
761
        cout << "executable is NULL" <<endl;
762
        if(proc->exceptionOccurred()) {
763
                cout << proc->getErrorMessage() <<endl;
764
                
765
        }
766
        sresult->failure++;
767
        sresult->failureList.push_back("exampleSimple3");
768
        return;
769
   }
770

    
771
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
772
    sresult->success++;
773
    delete xdmNode;
774
    delete executable;
775
}
776

    
777
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
778
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
779

    
780
    XsltExecutable * executable = proc->compileFromFile(nullptr);
781

    
782

    
783
    if (executable == nullptr) {
784

    
785

    
786
        cout << "Expected failure of test exampleSimple3aError:" << endl;
787
        if(proc->exceptionOccurred()) {
788
            cout<<proc->getErrorMessage()<<endl;
789
        }
790

    
791
        sresult->success++;
792
        proc->exceptionClear();
793
        return;
794
    }
795

    
796
    cout << "Error: executable is not nullptr'" << endl;
797
    sresult->failure++;
798
    sresult->failureList.push_back("exampleSimple3aError");
799
    delete executable;
800
    return;
801

    
802

    
803
}
804

    
805
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
806
    cout << "Test: ExampleParam" << endl;
807

    
808
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
809

    
810

    
811
    if(executable == nullptr) {
812
        cout << "executable is NULL" <<endl;
813
        if(proc->exceptionOccurred()) {
814
            cout << proc->getErrorMessage() <<endl;
815

    
816
        }
817
        sresult->failure++;
818
        sresult->failureList.push_back("exampleParam");
819
        return;
820

    
821
    }
822

    
823
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
824

    
825
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
826
    if (xdmvalue != nullptr) {
827

    
828
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
829

    
830
    } else {
831
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
832
        sresult->failure++;
833
        sresult->failureList.push_back("exampleParam");
834
        if(executable->exceptionOccurred()) {
835
            SaxonApiException * exception = executable->getException();
836
            cerr<<"Error: " << exception->getMessage() <<endl;
837
            delete exception;
838
        }
839
        delete executable;
840
        return;
841
    }
842
    const char *result = executable->applyTemplatesReturningString();
843
    if (result != nullptr) {
844
        cout << "Output:" << result << endl;
845
        sresult->success++;
846
        delete result;
847
    } else {
848
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
849
        sresult->failure++;
850
        sresult->failureList.push_back("exampleParam");
851
        if(executable->exceptionOccurred()) {
852
            SaxonApiException * exception = executable->getException();
853
            cerr<<"Error: " << exception->getMessage() <<endl;
854
            delete exception;
855
        }
856
        delete executable;
857
        return;
858
    }
859

    
860
    //proc->clearParameters();
861
    //unset($result);
862
    //echo 'again with a no parameter value<br/>';
863

    
864
    executable->setProperty("!indent", "yes");
865
    const char *result2 = executable->applyTemplatesReturningString();
866

    
867

    
868
    if (result2 != nullptr) {
869
        cout << "Result2 output= " << result2 << endl;
870
        sresult->success++;
871
        delete result2;
872
    } else {
873
        cout << "Result2 is NULL<br/>  ======= fail =====" << endl;
874
        sresult->failure++;
875
        sresult->failureList.push_back("exampleParam");
876
        if(executable->exceptionOccurred()) {
877
            SaxonApiException * exception = executable->getException();
878
            cerr<<"Error: " << exception->getMessage() <<endl;
879
            delete exception;
880
        }
881
        delete xdmvalue;
882
        delete executable;
883
        return;
884
    }
885

    
886
    //  unset($result);
887
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
888
    //delete xdmvalue;
889
    //executable->clearParameters();
890

    
891
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
892

    
893
    if (xdmValue2 == nullptr) {
894
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
895
        sresult->failure++;
896
        sresult->failureList.push_back("exampleParam");
897
        if(executable->exceptionOccurred()) {
898
            SaxonApiException * exception = executable->getException();
899
            cerr<<"Error: " << exception->getMessage() <<endl;
900
            delete exception;
901
        }
902
        delete executable;
903
        delete xdmvalue;
904
        return;
905
    }
906

    
907
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
908
    delete xdmvalue;
909

    
910

    
911
    const char *result3 = executable->applyTemplatesReturningString();
912
    if (result3 != nullptr) {
913
        cout << "Output =" << result3 << endl;
914

    
915
        sresult->success++;
916
        delete result3;
917

    
918
    } else {
919
        cout << "Error in result ===== FAIL =======" << endl;
920
        sresult->failure++;
921
        sresult->failureList.push_back("exampleParam");
922
        if(executable->exceptionOccurred()) {
923
            SaxonApiException * exception = executable->getException();
924
            cerr<<"Error: " << exception->getMessage() <<endl;
925
            delete exception;
926
        }
927

    
928
    }
929
    delete xdmValue2;
930
    delete executable;
931

    
932

    
933

    
934
}
935

    
936
// test parameter and properties maps where we update key, value pair.
937
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
938
    cout << "\nExampleParam:</b><br/>" << endl;
939
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
940
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
941

    
942

    
943
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
944
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
945
    if (xdmvalue != NULL) {
946

    
947
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
948
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
949

    
950
    } else {
951
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
952
        sresult->failure++;
953
        sresult->failureList.push_back("exampleParam-1");
954
    }
955
    const char *result = executable->applyTemplatesReturningString();
956
    if (result != NULL) {
957
        string sresulti = string(result);
958
        if (sresulti.compare("Hello from me") == 0) {
959
            cout << "Output:" << result << endl;
960
            sresult->success++;
961
        } else {
962
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
963
            sresult->failure++;
964
            sresult->failureList.push_back("exampleParam-2");
965
        }
966
    } else {
967
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
968
        sresult->failure++;
969
        sresult->failureList.push_back("exampleParam-2");
970
    }
971

    
972
//proc->clearParameters();
973
//unset($result);
974
//echo 'again with a no parameter value<br/>';
975

    
976
    executable->setProperty("!indent", "no");
977
    executable->setProperty("!indent", "yes");
978
    const char *result2 = executable->applyTemplatesReturningString();
979

    
980
    executable->clearProperties();
981

    
982
    if(result2 != NULL) {
983
        cout<<result2<<endl;
984
        sresult->success++;
985

    
986
    }
987

    
988
    //  unset($result);
989
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
990
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
991
    executable->setParameter("a-param", (XdmValue*)xdmValue2);
992

    
993
    const char *result3 = executable->applyTemplatesReturningString();
994
    if(result3 != NULL) {
995
        cout<<"Output ="<<result3<<endl;
996

    
997
        sresult->success++;
998
    } else {
999
        cout<<"Error in result ===== FAIL ======="<<endl;
1000
        sresult->failure++;
1001
        sresult->failureList.push_back("exampleParam");
1002
    }
1003

    
1004
}
1005

    
1006
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1007
void xmarkTest1(Xslt30Processor *proc, sResultCount *sresult) {
1008
    cout << "Test: xmarkTest1 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1009

    
1010
    proc->setJustInTimeCompilation(true);
1011

    
1012
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1013
    if (result != NULL) {
1014
        cout << "XdmNode returned" << endl;
1015
        sresult->success++;
1016
        delete result;
1017
    } else {
1018
        printf("result is null \nCheck For errors:");
1019
        sresult->failure++;
1020
        sresult->failureList.push_back("xmarkTest1");
1021
        cerr << "xmarkTest1 cp0" << endl;
1022
        if (proc->exceptionOccurred()) {
1023
            cerr << "xmarkTest1 cp1" << endl;
1024
            const char * message = proc->getErrorMessage();
1025
            cerr << "xmarkTest1 cp2" << endl;
1026
            if(message != nullptr) {
1027
                cerr << "xmarkTest1 cp2-1" << endl;
1028
                cerr << proc->getErrorMessage() << endl;
1029
            } else {
1030
                cerr << "Message is nullptr" << endl;
1031
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1032
            }
1033
        }
1034
        cerr << "xmarkTest1 cp3" << endl;
1035
    }
1036

    
1037

    
1038
}
1039

    
1040

    
1041
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1042
void xmarkTest2(Xslt30Processor *proc, sResultCount *sresult) {
1043
    cout << "Test: xmarkTest2 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1044

    
1045

    
1046
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1047
    if (result != NULL) {
1048
        cout << "XdmNode returned" << endl;
1049
        sresult->success++;
1050
        delete result;
1051

    
1052
    } else {
1053
        printf("result is null \nCheck For errors:");
1054
        sresult->failure++;
1055
        sresult->failureList.push_back("xmarkTest2");
1056
        if (proc->exceptionOccurred() > 0) {
1057
            SaxonApiException * exception = proc->getException();
1058
            if(exception != nullptr) {
1059
                const char *message = exception->getMessage();
1060
                if (message != nullptr) {
1061
                    cerr << "xmarkTest2 cp2-1" << endl;
1062
                    cerr << message << endl;
1063
                }
1064
                delete exception;
1065
            } else {
1066
                cerr << "Exception object is nullptr" << endl;
1067
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1068
            }
1069
        }
1070
    }
1071

    
1072

    
1073
}
1074

    
1075
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1076
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
1077
    cout << "exampleSimple_xmark test - test q12.xsl:" << endl;
1078

    
1079
    proc->setJustInTimeCompilation(true);
1080

    
1081
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1082
    if (result != NULL) {
1083
        cout << "XdmNode returned" << endl;
1084
        sresult->success++;
1085
        delete result;
1086
    } else {
1087
        printf("Result is null \nCheck For errors:");
1088
        if (proc->exceptionOccurred() > 0) {
1089
            cout << proc->getErrorMessage() << endl;
1090
        }
1091
        sresult->failure++;
1092
        sresult->failureList.push_back("exampleSimple_xmark");
1093
        proc->exceptionClear();
1094
    }
1095
    proc->clearParameters();
1096

    
1097
}
1098

    
1099
/*
1100
* Test saving nd loading a Xslt package
1101
*/
1102
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1103

    
1104
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1105
    trans->clearParameters();
1106

    
1107

    
1108
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1109
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1110

    
1111
    if (output== nullptr) {
1112
        printf("result is null \n");
1113
        const char *message = trans->getErrorMessage();
1114
        if (message != NULL) {
1115
            cout << "Error message =" << message << endl;
1116
        }
1117
        sresult->failure++;
1118
        sresult->failureList.push_back("testPackage1");
1119

    
1120
    } else {
1121
        printf("%s", output);
1122
        printf("result is OK \n");
1123
        sresult->success++;
1124
    }
1125
    fflush(stdout);
1126
    delete output;
1127
}
1128

    
1129

    
1130
/*
1131
* Test saving and loading a Xslt package
1132
*/
1133
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1134

    
1135
    cout << endl << "Test: testPackage1a" << endl;
1136
    trans->clearParameters();
1137

    
1138
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1139

    
1140
    if(trans->exceptionOccurred()) {
1141
        const char *message = trans->getErrorMessage();
1142
        if (message != NULL) {
1143
            cout << "Error message =" << message << endl;
1144
        }
1145
        sresult->failure++;
1146
        sresult->failureList.push_back("testPackage1a");
1147
        trans->exceptionClear();
1148
        return;
1149
    }
1150

    
1151

    
1152

    
1153
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1154
    executable->setInitialMatchSelectionAsFile("cat.xml");
1155
    const char * output = executable->applyTemplatesReturningString();
1156
    if (output== nullptr) {
1157
        printf("result is null \n");
1158
        const char *message = trans->getErrorMessage();
1159
        if (message != NULL) {
1160
            cout << "Error message =" << message << endl;
1161
        }
1162
        sresult->failure++;
1163
        sresult->failureList.push_back("testPackage1a");
1164
        trans->exceptionClear();
1165

    
1166
    } else {
1167
        printf("%s", output);
1168
        printf("result is OK \n");
1169
        sresult->success++;
1170
        delete output;
1171
    }
1172
    fflush(stdout);
1173
    delete executable;
1174
}
1175

    
1176

    
1177
/*
1178
* Test saving and loading a Xslt package
1179
*/
1180
void testPackage2_Error(Xslt30Processor *trans, sResultCount *sresult) {
1181

    
1182
    cout << endl << "Test: testPackage2_Error:" << endl;
1183

    
1184
    const char *stylesheet = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out><xsl:for-each></output></xsl:template><xsl:stylesheet>";
1185

    
1186
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1187

    
1188
    if(trans->exceptionOccurred()) {
1189
        const char *message = trans->getErrorMessage();
1190
        if (message != NULL) {
1191
            cout << "Error message =" << message << endl;
1192
        }
1193
        sresult->success++;
1194
        trans->exceptionClear();
1195
        return;
1196

    
1197
    }
1198

    
1199
    sresult->failure++;
1200
    sresult->failureList.push_back("testPackage2_Error");
1201

    
1202
}
1203

    
1204
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1205

    
1206
    const char *source = "<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:f='http://localhost/' version='3.0'> <xsl:function name='f:add' visibility='public'>    <xsl:param name='a'/><xsl:param name='b'/> <xsl:sequence select='$a + $b'/></xsl:function></xsl:stylesheet>";
1207
    cout << endl << "Test: testCallFunction:" << endl;
1208
    XsltExecutable * executable = trans->compileFromString(source);
1209

    
1210
    if(executable == nullptr) {
1211
        if(trans->exceptionOccurred()) {
1212
            SaxonApiException * exception = trans->getException();
1213
            if(exception != nullptr) {
1214
                cerr << "Error: " << exception->getMessage() << endl;
1215
                delete exception;
1216
            }
1217

    
1218
        }
1219
        sresult->failure++;
1220
        sresult->failureList.push_back("testCallFunction");
1221
        trans->exceptionClear();
1222
        return;
1223

    
1224

    
1225
    }
1226

    
1227
    XdmValue **valueArray = new XdmValue *[2];
1228

    
1229
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1230
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1231
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1232

    
1233
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1234
        sresult->success++;
1235
        delete v;
1236
    } else {
1237
        if (v != NULL && !(v->getHead())->isAtomic()) {
1238
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1239
        }
1240
        cout << "testCallFunction ======= FAIL ======" << endl;
1241
        if(executable->exceptionOccurred()) {
1242
            SaxonApiException * exception = executable->getException();
1243
            if(exception != nullptr) {
1244
                cerr << "Error: " << exception->getMessage() << endl;
1245
                delete exception;
1246
            }
1247
        }
1248
        sresult->failure++;
1249
        sresult->failureList.push_back("testCallFunction");
1250
        executable->exceptionClear();
1251
        delete v;
1252
    }
1253
    delete valueArray[0];
1254
    delete valueArray[1];
1255
    delete [] valueArray;
1256
    delete executable;
1257
}
1258

    
1259
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1260

    
1261

    
1262
    const char *source = "<?xml version='1.0'?>  <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'  xmlns:xs='http://www.w3.org/2001/XMLSchema'  version='3.0'>  <xsl:template match='*'>     <xsl:param name='a' as='xs:double'/>     <xsl:param name='b' as='xs:float'/>     <xsl:sequence select='., $a + $b'/>  </xsl:template>  </xsl:stylesheet>";
1263
    cout << endl << "Test:testInitialTemplate" << endl;
1264
    XsltExecutable * executable = trans->compileFromString(source);
1265
    if(executable == nullptr) {
1266
        if(trans->exceptionOccurred()) {
1267
            cout << "Error: "<< trans->getErrorMessage() << endl;
1268
        }
1269
        return;
1270
    }
1271
    XdmNode * node = proc->parseXmlFromString("<e/>");
1272

    
1273
    executable->setResultAsRawValue(false);
1274
    std::map<std::string, XdmValue *> parameterValues;
1275

    
1276
    XdmAtomicValue * a1 = proc->makeIntegerValue(12);
1277
    XdmAtomicValue * a2 = proc->makeIntegerValue(5);;
1278
    parameterValues["a"] = a1;
1279
    parameterValues["b"] = a2;
1280
    executable->setInitialTemplateParameters(parameterValues, false);
1281
    executable->setInitialMatchSelection(node);
1282
    XdmValue *result = executable->applyTemplatesReturningValue();
1283
    if (result != NULL) {
1284
        sresult->success++;
1285
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1286
        delete result;
1287
    } else {
1288
        sresult->failure++;
1289
    }
1290

    
1291
    delete executable;
1292
    delete a1;
1293
    delete a2;
1294
    delete node;
1295
    parameterValues.clear();
1296
}
1297

    
1298
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1299
    cout << endl << "Test: testResolveUri:" << endl;
1300

    
1301
    XsltExecutable * executable = trans->compileFromString(
1302
            "<xsl:stylesheet version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:err='http://www.w3.org/2005/xqt-errors'><xsl:template name='go'><xsl:try><xsl:variable name='uri' as='xs:anyURI' select=\"resolve-uri('notice trailing space /out.xml')\"/> <xsl:message select='$uri'/><xsl:result-document href='{$uri}'><out/></xsl:result-document><xsl:catch><xsl:sequence select=\"'$err:code: ' || $err:code  || ', $err:description: ' || $err:description\"/></xsl:catch></xsl:try></xsl:template></xsl:stylesheet>");
1303

    
1304
    if(executable == nullptr) {
1305
        sresult->failure++;
1306
        sresult->failureList.push_back("testResolveUri");
1307
        if(trans->exceptionOccurred()) {
1308
            const char * message = trans->getErrorMessage();
1309
            if(message != nullptr) {
1310
                cout << "Error: " << trans->getErrorMessage() << endl;
1311
            }
1312
        }
1313
        trans->exceptionClear();
1314
        return;
1315
    }
1316
    XdmValue *value = executable->callTemplateReturningValue("go");
1317

    
1318

    
1319
    if (value== nullptr) {
1320

    
1321
        sresult->failure++;
1322
        sresult->failureList.push_back("testResolveUri");
1323
    } else {
1324

    
1325
        const char *svalue = value->itemAt(0)->getStringValue();
1326
        cout << "testResolveUri = " << svalue << endl;
1327
        sresult->success++;
1328
        delete value;
1329
    }
1330

    
1331
    delete executable;
1332
}
1333

    
1334
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1335
    cout << endl << "Test: testContextNotRoot" << endl;
1336

    
1337
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1338

    
1339
    XsltExecutable *executable = trans->compileFromString(
1340
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template match='e'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1341

    
1342
    if (executable == nullptr) {
1343
        sresult->failure++;
1344
        sresult->failureList.push_back("testContextNotRoot");
1345
        if (trans->exceptionOccurred()) {
1346
            cout << "Error: " << trans->getErrorMessage() << endl;
1347
        }
1348
        return;
1349
    }
1350

    
1351
    executable->setGlobalContextItem(node);
1352
    if (node->getChildCount() > 0) {
1353
        XdmNode **eNodeL1 = node->getChildren();
1354
        if (eNodeL1 != nullptr) {
1355
            XdmNode ** eNodeChildren = eNodeL1[0]->getChildren();
1356
            if (eNodeChildren != nullptr) {
1357
                XdmNode *eNode = eNodeChildren[0];
1358
                cout << "Node content = " << eNode->toString() << endl;
1359
                executable->setInitialMatchSelection(eNode);
1360
                const char *result = executable->applyTemplatesReturningString();
1361

    
1362
                if (result == nullptr) {
1363

    
1364
                    cout << "testContextNotRoot ======= FAIL ======" << endl;
1365
                    if (executable->exceptionOccurred()) {
1366
                        SaxonApiException *exception = executable->getException();
1367
                        cerr << "Error: " << exception->getMessage() << endl;
1368
                        delete exception;
1369
                    }
1370
                    sresult->failure++;
1371
                    sresult->failureList.push_back("testContextNotRoot");
1372

    
1373

    
1374
                } else {
1375

    
1376
                    cout << "testContextNotRoot = " << result << endl;
1377
                    sresult->success++;
1378
                    delete result;
1379

    
1380
                }
1381

    
1382
            }
1383
        }
1384
    } else {
1385
        sresult->failure++;
1386
        sresult->failureList.push_back("testContextNotRoot");
1387
    }
1388

    
1389
    delete executable;
1390
    delete node;
1391
}
1392

    
1393

    
1394
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1395
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1396

    
1397
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1398

    
1399
    if(node == nullptr) {
1400
        if(proc->exceptionOccurred()) {
1401
            const char * message = proc->getErrorMessage();
1402
            if(message != nullptr) {
1403
                cerr << "Error: " << message << endl;
1404
            }
1405
            proc->exceptionClear();
1406
        }
1407
        sresult->failure++;
1408
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1409
        return;
1410
    }
1411

    
1412
    XsltExecutable * executable = trans->compileFromString(
1413
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template name='main'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1414

    
1415
    if(executable == nullptr) {
1416
        sresult->failure++;
1417
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1418
        if(trans->exceptionOccurred()) {
1419
            cout << "Error: "<< trans->getErrorMessage() << endl;
1420
        }
1421
        delete node;
1422
        return;
1423
    }
1424

    
1425
    executable->setGlobalContextItem(node);
1426
    const char *result = executable->callTemplateReturningString("main");
1427

    
1428
    if (result== nullptr) {
1429

    
1430
        cout << "testContextNotRootNameTemplate ======= FAIL ======" << endl;
1431
        if(executable->exceptionOccurred()) {
1432
            SaxonApiException * exception = executable->getException();
1433
            cerr<<"Error: " << exception->getMessage() <<endl;
1434
            delete exception;
1435
        }
1436
        sresult->failure++;
1437
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1438
    } else {
1439

    
1440
        cout << "testContextNotRoot = " << result << endl;
1441
        sresult->success++;
1442
        delete result;
1443
    }
1444
    delete node;
1445
    delete executable;
1446

    
1447
}
1448

    
1449

    
1450
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1451
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1452

    
1453
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1454

    
1455
    if(node == nullptr) {
1456
        if(proc->exceptionOccurred()) {
1457
            const char * message = proc->getErrorMessage();
1458
            if(message != nullptr) {
1459
                cerr << "Error: " << message << endl;
1460
            }
1461
            proc->exceptionClear();
1462
        }
1463
        sresult->failure++;
1464
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1465
        return;
1466
    }
1467

    
1468
    XsltExecutable * executable = trans->compileFromString(
1469
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template name='main'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1470

    
1471
    if(executable == nullptr) {
1472
        sresult->failure++;
1473
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1474
        if(trans->exceptionOccurred()) {
1475
            cout << "Error: "<< trans->getErrorMessage() << endl;
1476
        }
1477
        delete node;
1478
        return;
1479
    }
1480

    
1481
    executable->setGlobalContextItem(node);
1482
    XdmValue *result = executable->callTemplateReturningValue("main");
1483

    
1484
    if (result == nullptr) {
1485

    
1486
        cout << "testCallFunction ======= FAIL ======" << endl;
1487
        if(executable->exceptionOccurred()) {
1488
            SaxonApiException * exception = executable->getException();
1489
            cerr<<"Error: " << exception->getMessage() <<endl;
1490
            delete exception;
1491
        }
1492
        sresult->failure++;
1493
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1494
    } else {
1495

    
1496
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1497
        sresult->success++;
1498
        delete result;
1499
        result = nullptr;
1500
    }
1501

    
1502
    delete node;
1503
    delete executable;
1504

    
1505

    
1506
}
1507

    
1508
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1509

    
1510
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1511
                                                            1);
1512
    if (fi== nullptr) {
1513
        sresult->failure++;
1514
        sresult->failureList.push_back("testCallSystemFunction");
1515
        return;
1516
    }
1517

    
1518
    XdmValue ** xdmValue = new XdmValue*[1];
1519
    xdmValue[0] = new XdmValue();
1520
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1521
    XdmValue *result = fi->call(xdmValue, 1);
1522

    
1523
    std::cerr << "Result = " << result->toString() << endl;
1524
    if(result->size() == 3) {
1525

    
1526
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1527
        sresult->success++;
1528

    
1529
    } else {
1530
        sresult->failure++;
1531
        sresult->failureList.push_back("testCallSystemFunction");
1532

    
1533
    }
1534

    
1535
}
1536

    
1537

    
1538
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1539
    cout << endl << "Test: testPipeline" << endl;
1540

    
1541
    Xslt30Processor * trans = proc->newXslt30Processor();
1542
    if(trans == nullptr) {
1543
        cout << "Error: Xslt30Processor is null - maybe unclean state of JNI" << endl;
1544
        sresult->failure++;
1545
        sresult->failureList.push_back("testPipeline");
1546
        return;
1547
    }
1548
    XsltExecutable * stage1 = trans->compileFromString(
1549
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1550

    
1551
    if(stage1 == nullptr) {
1552
        sresult->failure++;
1553
        sresult->failureList.push_back("testPipeline");
1554
        if(trans->exceptionOccurred()) {
1555
            const char * message = trans->getErrorMessage();
1556
            cout << "stage 1 Error: " << message << endl;
1557

    
1558
        }
1559
        cout << "Stage 1 Error - exit method " << endl;
1560
        delete trans;
1561
        return;
1562
    }
1563

    
1564
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1565

    
1566
    XsltExecutable *stage2 = trans->compileFromString(
1567
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1568

    
1569
    if(stage2 == nullptr) {
1570
        sresult->failure++;
1571
        sresult->failureList.push_back("testPipeline");
1572
        if(trans->exceptionOccurred()) {
1573
            const char * message = trans->getErrorMessage();
1574
            cout << "stage 2 Error: " << message << endl;
1575

    
1576
        }
1577
        cout << "Stage 2 Error - exit method " << endl;
1578
        delete stage1;
1579
        delete trans;
1580
        delete inn;
1581
        return;
1582
    }
1583

    
1584

    
1585
    XsltExecutable *stage3 = trans->compileFromString(
1586
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1587

    
1588
    if(stage3 == nullptr) {
1589
        sresult->failure++;
1590
        sresult->failureList.push_back("testPipeline");
1591
        if(trans->exceptionOccurred()) {
1592
            const char * message = trans->getErrorMessage();
1593
            cout << "stage 3 Error: " << message << endl;
1594

    
1595
        } else {
1596
            cout << "Stage 3 Error: testPipeline failed with exception" << endl;
1597
        }
1598
        cout << "Stage 3 Error - exit method " << endl;
1599
        delete stage1;
1600
        delete stage2;
1601
        delete trans;
1602
        delete inn;
1603
        return;
1604
    }
1605

    
1606
    XsltExecutable *stage4 = trans->compileFromString(
1607
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1608

    
1609
    if(stage4 == nullptr) {
1610
        sresult->failure++;
1611
        sresult->failureList.push_back("testPipeline");
1612
        if(trans->exceptionOccurred()) {
1613
            const char * message = trans->getErrorMessage();
1614
            cout << "stage 4 Error: " << message << endl;
1615

    
1616
        } else {
1617
            cout << "Stage 4 Error: testPipeline failed with exception" << endl;
1618
        }
1619
        cout << "Stage 4 Error - exit method " << endl;
1620
        delete stage1;
1621
        delete stage2;
1622
        delete stage3;
1623
        delete trans;
1624
        delete inn;
1625
        return;
1626
    }
1627

    
1628
    XsltExecutable *stage5 = trans->compileFromString(
1629
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1630

    
1631
    if(stage5 == nullptr) {
1632
        sresult->failure++;
1633
        sresult->failureList.push_back("testPipeline");
1634
        if(trans->exceptionOccurred()) {
1635
            const char * message = trans->getErrorMessage();
1636
            cout << "stage 5 Error: " << message << endl;
1637

    
1638
        } else {
1639
            cout << "Stage 5 Error: testPipeline failed with exception" << endl;
1640
        }
1641
        cout << "Stage 5     Error - exit method " << endl;
1642
        delete stage1;
1643
        delete stage2;
1644
        delete stage3;
1645
        delete stage4;
1646
        delete trans;
1647
        delete inn;
1648
        return;
1649
    }
1650

    
1651
    stage1->setProperty("!indent", "no");
1652
    stage1->setInitialMatchSelection(inn);
1653

    
1654
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1655
    if(d1 == nullptr) {
1656
        if (stage1->exceptionOccurred()) {
1657
            sresult->failure++;
1658
            sresult->failureList.push_back("testPipeline");
1659
            if (stage1->exceptionOccurred()) {
1660
                SaxonApiException *exception = stage1->getException();
1661
                if (exception != nullptr) {
1662
                    cerr << "Error: " << exception->getMessage() << endl;
1663
                    delete exception;
1664
                    exception = nullptr;
1665
                }
1666
            }
1667
            cout << "Stage d1 Error - exit method " << endl;
1668
            delete stage1;
1669
            delete stage2;
1670
            delete stage3;
1671
            delete stage4;
1672
            delete stage5;
1673
            delete trans;
1674
            delete inn;
1675
            return;
1676
        }
1677
    }
1678

    
1679
    XdmItem *d11 = d1->getHead();
1680
    if (d11== nullptr) {
1681

    
1682
        cout << "d11 is NULL\n" << endl;
1683
        sresult->failure++;
1684
        sresult->failureList.push_back("testPipeline-1");
1685
        delete stage1;
1686
        delete stage2;
1687
        delete stage3;
1688
        delete stage4;
1689
        delete stage5;
1690
        delete d1;
1691
        delete inn;
1692
        delete trans;
1693
        return;
1694
    }
1695
    const char *data = d11->toString();
1696

    
1697
    if (data != NULL) {
1698
        cout << "d1 result=" << data << endl;
1699
    } else {
1700

    
1701
        cout << "d1 result Error - toString is NULL" << endl;
1702
        delete stage1;
1703
        delete stage2;
1704
        delete stage3;
1705
        delete stage4;
1706
        delete stage5;
1707
        delete d1;
1708
        delete inn;
1709
        delete trans;
1710
        return;
1711
    }
1712
    stage2->setProperty("!indent", "no");
1713
    stage2->setInitialMatchSelection(d11);
1714
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1715
    if (d2== nullptr) {
1716
        cout << "ERROR-11\n" << endl;
1717
        sresult->failure++;
1718
        sresult->failureList.push_back("testPipeline-2");
1719
        if (stage2->exceptionOccurred()) {
1720
            SaxonApiException *exception = stage2->getException();
1721
            if (exception != nullptr) {
1722
                cerr << "Error: " << exception->getMessage() << endl;
1723
                delete exception;
1724
                exception = nullptr;
1725
            }
1726
        }
1727
        cout << "Stage d2 Error - exit method " << endl;
1728
        delete d1;
1729
        delete inn;
1730
        delete stage1;
1731
        delete stage2;
1732
        delete stage3;
1733
        delete stage4;
1734
        delete stage5;
1735
        delete trans;
1736
        return;
1737
    }
1738
    stage3->setProperty("!indent", "no");
1739
    stage3->setInitialMatchSelection(d2);
1740
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1741
    if(d3 == nullptr){
1742
        sresult->failure++;
1743
        sresult->failureList.push_back("testPipeline-3");
1744
        if (stage3->exceptionOccurred()) {
1745
            SaxonApiException *exception = stage3->getException();
1746
            if (exception != nullptr) {
1747
                cerr << "Error: " << exception->getMessage() << endl;
1748
                delete exception;
1749
                exception = nullptr;
1750
            }
1751
        }
1752
        cout << "Stage d3 Error - exit method " << endl;
1753
        delete d1;
1754
        delete d2;
1755
        delete inn;
1756

    
1757
        delete stage1;
1758
        delete stage2;
1759
        delete stage3;
1760
        delete stage4;
1761
        delete stage5;
1762
        delete trans;
1763
        return;
1764
      }
1765
    stage4->setProperty("!indent", "no");
1766
    stage4->setInitialMatchSelection(d3);
1767
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1768
    if(d4== nullptr){
1769
        sresult->failure++;
1770
        sresult->failureList.push_back("testPipeline-4");
1771
        if (stage4->exceptionOccurred()) {
1772
            SaxonApiException *exception = stage4->getException();
1773
            if (exception != nullptr) {
1774
                cerr << "Error: " << exception->getMessage() << endl;
1775
                delete exception;
1776
                exception = nullptr;
1777
            }
1778
        }
1779
        cout << "Stage d4 Error - exit method " << endl;
1780
        delete d3;
1781
        delete d2;
1782
        delete d1;
1783
        delete inn;
1784
        delete stage1;
1785
        delete stage2;
1786
        delete stage3;
1787
        delete stage4;
1788
        delete stage5;
1789
        delete trans;
1790
        return;
1791
      }
1792
    stage5->setProperty("!indent", "no");
1793
    stage5->setInitialMatchSelection(d4);
1794
    const char * sw = stage5->applyTemplatesReturningString();
1795
    if(sw == nullptr){
1796
        sresult->failure++;
1797
        sresult->failureList.push_back("testPipeline-5");
1798
        if (stage5->exceptionOccurred()) {
1799
            SaxonApiException *exception = stage5->getException();
1800
            if (exception != nullptr) {
1801
                cerr << "Error: " << exception->getMessage() << endl;
1802
                delete exception;
1803
            }
1804
        }
1805
        cout << "Stage sw Error - exit method " << endl;
1806
        delete stage1;
1807
        delete stage2;
1808
        delete stage3;
1809
        delete stage4;
1810
        delete stage5;
1811
        delete trans;
1812
        delete d4;
1813
        delete d3;
1814
        delete d2;
1815
        delete d1;
1816
        delete inn;
1817
        return;
1818
      }
1819
    cout<<sw<<endl;
1820
    cout << "testPipeline = " << sw << endl;
1821
    sresult->success++;
1822
    delete stage1;
1823
    stage1 = nullptr;
1824

    
1825
    delete stage2;
1826
    stage2 = nullptr;
1827

    
1828

    
1829
    delete stage3;
1830
    stage3 = nullptr;
1831

    
1832
    delete stage4;
1833
    stage4 = nullptr;
1834

    
1835
    delete stage5;
1836
    stage5 = nullptr;
1837

    
1838
    delete trans;
1839
    trans = nullptr;
1840

    
1841
    delete sw;
1842
    sw = nullptr;
1843

    
1844
    delete d4;
1845
    d4 = nullptr;
1846

    
1847
    delete d3;
1848
    d3 = nullptr;
1849

    
1850
    delete d2;
1851
    d2 = nullptr;
1852

    
1853
    delete d1;
1854
    d1 = nullptr;
1855

    
1856
    delete inn;
1857
    inn = nullptr;
1858

    
1859
}
1860

    
1861

    
1862
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1863

    
1864

    
1865
    cout << endl << "Test: testCatalog" << endl;
1866
    bool trace = false;
1867
    proc->setcwd(cwd);
1868
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1869

    
1870
    Xslt30Processor * trans = proc->newXslt30Processor();
1871

    
1872
    XsltExecutable  * executable = trans->compileFromFile("../php/catalog-test/test1.xsl");
1873

    
1874
    if(executable == nullptr) {
1875

    
1876
        if (trans->exceptionOccurred()) {
1877

    
1878
            if(trans->getErrorMessage() != nullptr) {
1879
                const char *message = trans->getErrorMessage();
1880
                cerr << "exception=" << message << endl;
1881
            } else {
1882
                const char *message = proc->getErrorMessage();
1883
                if(message != nullptr) {
1884
                    cerr << "exception-proc=" << message << endl;
1885
                }
1886
            }
1887
        }
1888
        sresult->failure++;
1889
        sresult->failureList.push_back("testCatalog");
1890
        trans->exceptionClear();
1891
        delete trans;
1892
        trans= nullptr;
1893
        return;
1894
    }
1895

    
1896
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1897

    
1898
    const char *result = executable->applyTemplatesReturningString();
1899

    
1900
    if(result != NULL) {
1901
        std::cerr << "testCatalog result= " << result << std::endl;
1902
        delete result;
1903
        sresult->success++; // TODO - check the results more carefully
1904
    } else {
1905
        sresult->failure++;
1906
        sresult->failureList.push_back("testCatalog");
1907
    }
1908

    
1909

    
1910
    delete executable;
1911
    executable = nullptr;
1912
    delete trans;
1913
    trans = nullptr;
1914

    
1915
}
1916

    
1917
static int NUM_THREADS = 10;
1918

    
1919
void RunThread(XsltExecutable * executable, int tid, const std::string filename) {
1920
    JavaVMAttachArgs att_arg;
1921
    att_arg.version = JNI_VERSION_1_2;
1922
    att_arg.name = NULL;
1923
    att_arg.group = NULL;
1924

    
1925
   // SaxonProcessor::sxn_environ->jvm->AttachCurrentThread((void**)&SaxonProcessor::sxn_environ->env, &att_arg);
1926
    cerr<<endl<<"RunThread cp0,  THEAD ID="<<tid<<endl;
1927
    fflush(stderr);
1928
    fflush(stdout);
1929

    
1930
    if(executable != nullptr) {
1931
       executable->setInitialMatchSelectionAsFile(filename.c_str());
1932

    
1933
        cerr << "RunThread cp1" << endl;
1934

    
1935
       const char *result = nullptr;
1936
        result = executable->applyTemplatesReturningString();
1937
        if (result != nullptr) {
1938
            cout << " Result from THREAD ID: " << tid << ", " << result << endl;
1939
            delete result;
1940
        } else {
1941
            cerr << " ===== Failed in THREAD ID: " << tid << endl;
1942
           /* if(executable->exceptionOccurred()) {
1943
                SaxonApiException *exception = executable->getException();
1944

1945
                if (exception != nullptr) {
1946
                    const char *message = exception->getMessage();
1947
                    if (message != nullptr) {
1948
                        cerr << "Error = " << message << endl;
1949
                    }
1950
                    delete exception;
1951
                }
1952
            } else {
1953
            cerr << "No exception found - result is nullptr" << endl;
1954
            }*/
1955

    
1956
        }
1957
    delete executable;
1958
    } else {
1959
        cerr << "XsltExecutable is nullptr" << endl;
1960
    }
1961

    
1962
   // (   SaxonProcessor::sxn_environ->jvm)->DetachCurrentThread();
1963

    
1964
}
1965

    
1966
void testThreads (SaxonProcessor * processor, Xslt30Processor * trans, sResultCount *sresult) {
1967

    
1968
    std::vector<std::string> s = {
1969
            "../data/xmark100k.xml",
1970
            "../data/xmark1.xml",
1971
            "../data/xmark4.xml",
1972
            "../data/xmark10.xml"
1973
    };
1974

    
1975
    std::vector<std::thread> threads;
1976

    
1977
    for (int i = 0; i < s.size(); i++) {
1978

    
1979
        XsltExecutable * executable = trans->compileFromFile("q12.xsl");
1980
            threads.push_back(std::thread(RunThread, executable, i, s[i]));
1981

    
1982
    }
1983
    cerr<<"testThreads cp0"<<endl;
1984
    for (auto &th : threads) {
1985
        th.join();
1986
        cerr<<"testThreads cp1 - loop"<<endl;
1987
    }
1988
}
1989

    
1990

    
1991

    
1992
int main(int argc, char* argv[]) {
1993

    
1994

    
1995
    const char * cwd = nullptr;
1996
    if(argc > 0) {
1997
        cwd = argv[1];
1998
    }
1999

    
2000
    SaxonProcessor *processor = new SaxonProcessor(true);
2001
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
2002
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
2003
    
2004
    char buff[FILENAME_MAX]; //create string buffer to hold path
2005
    GetCurrentDir( buff, FILENAME_MAX );
2006
    cout<<"CWD = "<< buff<<endl;
2007
    if(cwd != nullptr) {
2008
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
2009
    } else {
2010
        processor->setcwd(buff);
2011
    }
2012
    if (processor->isSchemaAwareProcessor()) {
2013

    
2014
        std::cerr << "Processor is Schema Aware" << std::endl;
2015
    } else {
2016
        std::cerr << "Processor is not Schema Aware" << std::endl;
2017
    }
2018

    
2019
    sResultCount *sresult = new sResultCount();
2020
    Xslt30Processor *trans = processor->newXslt30Processor();
2021

    
2022
    if( trans == nullptr) {
2023
        cout<< "Error creating Xslt30Processor"<<endl;
2024
        if(processor->exceptionOccurred()) {
2025
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
2026

    
2027
        }
2028
        return -1;
2029
    }
2030
    //testValidation(trans,sresult);
2031

    
2032
    testInitialTemplate(processor, trans, sresult);
2033

    
2034
    cout<<endl<<"============================================================="<<endl<<endl;
2035

    
2036
    exampleSimple1Err(trans, sresult);
2037

    
2038
    cout<<endl<<"============================================================="<<endl<<endl;
2039

    
2040
    exampleSimple1(trans, sresult);
2041

    
2042
    cout<<endl<<"============================================================="<<endl<<endl;
2043

    
2044
    exampleSimple_xmark(trans, sresult);
2045

    
2046
    cout<<endl<<"============================================================="<<endl<<endl;
2047

    
2048
    exampleSimple2(trans, sresult);
2049

    
2050
    cout<<endl<<"============================================================="<<endl<<endl;
2051

    
2052
    exampleSimple3(processor, trans, sresult);
2053

    
2054
    cout<<endl<<"============================================================="<<endl<<endl;
2055

    
2056
    exampleSimple3aError(processor, trans, sresult);
2057

    
2058
    cout<<endl<<"============================================================="<<endl<<endl;
2059

    
2060
    testApplyTemplatesString1(trans, sresult);
2061

    
2062
    cout<<endl<<"============================================================="<<endl<<endl;
2063

    
2064
    testApplyTemplatesString2(processor, trans, sresult);
2065

    
2066
    cout<<endl<<"============================================================="<<endl<<endl;
2067

    
2068

    
2069
    testApplyTemplates2a_Error(processor, trans, sresult);
2070

    
2071
    cout<<endl<<"============================================================="<<endl<<endl;
2072

    
2073
    testTransformToString4(processor, trans, sresult);
2074

    
2075
    cout<<endl<<"============================================================="<<endl<<endl;
2076

    
2077
    testCatalog(cwd, processor,  sresult);
2078

    
2079
    cout<<endl<<"============================================================="<<endl<<endl;
2080

    
2081
    testTransformToString2b(processor, trans, sresult);
2082

    
2083
    cout<<endl<<"============================================================="<<endl<<endl;
2084

    
2085
    testTransformToString3(processor, trans, sresult);
2086

    
2087
    cout<<endl<<"============================================================="<<endl<<endl;
2088
        
2089
    testTransformFromstring(processor, trans, sresult);
2090

    
2091

    
2092
    cout<<endl<<"============================================================="<<endl<<endl;
2093

    
2094
    testTransformFromstring2Err(processor, trans, sresult);
2095

    
2096
    cout<<endl<<"============================================================="<<endl<<endl;
2097

    
2098
    testTrackingOfValueReference(processor, trans, sresult);
2099

    
2100
    cout<<endl<<"============================================================="<<endl<<endl;
2101

    
2102
    testTrackingOfValueReferenceError(processor, trans, sresult);
2103

    
2104
    cout<<endl<<"============================================================="<<endl<<endl;
2105

    
2106
    testPackage1(trans, sresult);
2107

    
2108
    cout<<endl<<"============================================================="<<endl<<endl;
2109

    
2110
    testPackage1a(trans, sresult);
2111

    
2112
    cout<<endl<<"============================================================="<<endl<<endl;
2113

    
2114
    testPackage2_Error(trans, sresult);
2115

    
2116
    cout<<endl<<"============================================================="<<endl<<endl;
2117

    
2118
    testXdmNodeOutput(trans, sresult);
2119

    
2120
    cout<<endl<<"============================================================="<<endl<<endl;
2121

    
2122
    exampleParam(processor, trans, sresult);
2123

    
2124
    cout<<endl<<"============================================================="<<endl<<endl;
2125

    
2126
    xmarkTest1(trans, sresult);
2127

    
2128
    cout<<endl<<"============================================================="<<endl<<endl;
2129

    
2130
    xmarkTest2(trans, sresult);
2131

    
2132
    cout<<endl<<"============================================================="<<endl<<endl;
2133

    
2134
    testCallFunction(processor, trans, sresult);
2135

    
2136
    cout<<endl<<"============================================================="<<endl<<endl;
2137

    
2138
    testResolveUri(processor, trans, sresult);
2139

    
2140
    cout<<endl<<"============================================================="<<endl<<endl;
2141

    
2142
    testContextNotRoot(processor, trans, sresult);
2143

    
2144
    cout<<endl<<"============================================================="<<endl<<endl;
2145

    
2146
    testContextNotRootNamedTemplate(processor, trans, sresult);
2147

    
2148
    cout<<endl<<"============================================================="<<endl<<endl;
2149

    
2150
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
2151

    
2152
    cout<<endl<<"============================================================="<<endl<<endl;
2153

    
2154
   testPipeline(processor, sresult);
2155

    
2156

    
2157
    cout<<endl<<"============================================================="<<endl<<endl;
2158

    
2159
    fflush(stdout);
2160

    
2161

    
2162
    //Available in PE and EE
2163
    //testTransformToStringExtensionFunc(processor, trans);
2164

    
2165

    
2166

    
2167
    //processor->release();
2168
    //return 0;
2169

    
2170
    fflush(stdout);
2171

    
2172

    
2173
    //set to the current working directory
2174
    if(cwd != nullptr) {
2175
        processor->setcwd(cwd);
2176
    } else {
2177
        processor->setcwd(buff);
2178
    }
2179
    Xslt30Processor *trans2 = processor->newXslt30Processor();
2180
    cerr<<" after trans2 cp0"<<endl;
2181
    testApplyTemplatesString1(trans2, sresult);
2182
    cout<<endl<<"============================================================="<<endl<<endl;
2183

    
2184

    
2185

    
2186
    //testThreads (processor, trans2, sresult);
2187

    
2188

    
2189
    delete trans;
2190
    delete trans2;
2191
    delete processor;
2192
    processor->release();
2193

    
2194

    
2195
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
2196

    
2197
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
2198
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
2199

    
2200
    std::list<std::string>::iterator it;
2201
    std::cout << "Failed tests:" << std::endl;
2202
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
2203
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
2204
        //Print the contents
2205
        std::cout << it->c_str() << std::endl;
2206

    
2207
    }
2208

    
2209
    delete sresult;
2210

    
2211
#ifdef MEM_DEBUG
2212
    SaxonProcessor::getInfo();
2213
#endif
2214

    
2215
    return 0;
2216
}
(27-27/28)