Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 0c4104cf

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 != nullptr) {
569
                cout << name1.str();
570
                if (valuei->itemAt(0) != nullptr)
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

    
599
    if(executable== nullptr) {
600
        sresult->failure++;
601
        sresult->failureList.push_back("testValidation");
602
        if (trans->exceptionOccurred()) {
603
            cerr <<"Error message: "<< trans->getErrorMessage() << endl;
604
        }
605
        trans->exceptionClear();
606
        return;
607
    }
608

    
609

    
610

    
611
    const char *rootValue = executable->callTemplateReturningString("main");
612

    
613

    
614
    if (rootValue== nullptr) {
615
        std::cout << "NULL found" << std::endl;
616
        sresult->failure++;
617
        sresult->failureList.push_back("testValidation");
618
        return;
619

    
620
    } else {
621
        std::cout << "Result=" << rootValue << endl;
622
        sresult->success++;
623
    }
624
    delete executable;
625
}
626

    
627

    
628
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
629

    
630
    std::cout << "testXdmNodeOutput" << std::endl;
631
    XsltExecutable * executable = trans->compileFromString(
632
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
633

    
634

    
635
    if(executable== nullptr) {
636
        sresult->failure++;
637
        sresult->failureList.push_back("testXdmNodeOutput");
638
        if (trans->exceptionOccurred()) {
639
            cerr <<"Error message: "<< trans->getErrorMessage() << endl;
640
        }
641
        trans->exceptionClear();
642
        return;
643
    }
644

    
645
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
646
    if (rootValue== nullptr) {
647
        if (executable->exceptionOccurred()) {
648
            SaxonApiException *exception = executable->getException();
649
            cerr << "Error: " << exception->getMessage() << endl;
650
            delete exception;
651
        }
652
        sresult->failure++;
653
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
654
        return;
655
    }
656
    XdmItem *rootItem = rootValue->getHead();
657
    if (rootItem== nullptr) {
658

    
659
        cout << "Result is null ====== FAIL ====== " << endl;
660
        sresult->failure++;
661
        sresult->failureList.push_back("testXdmNodeOutput-0");
662
        delete rootValue;
663
        delete executable;
664
        return;
665
    }
666
    XdmNode *root = (XdmNode *) rootItem;
667
    if (root->getNodeKind() == DOCUMENT) {
668
        cout << "Result is a Document" << endl;
669
    } else {
670
        cout << "Node is of kind:" << root->getNodeKind() << endl;
671
    }
672
    const char *result = executable->callTemplateReturningString("go");
673
    if (string(result).find(string("<a/>")) != std::string::npos) {
674
        sresult->success++;
675
        delete result;
676
    } else {
677
        //TODO - this test case prints the XML declaration. Check if this correct
678
        sresult->failure++;
679
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
680
        sresult->failureList.push_back("testXdmNodeOutput");
681
    }
682
    delete rootValue;
683
    delete executable;
684

    
685
}
686

    
687
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
688
    cout << "ExampleSimple1 taken from PHP:" << endl;
689

    
690
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
691
    if(executable== nullptr) {
692
        sresult->failure++;
693
        sresult->failureList.push_back("exampleSimple1");
694
        cerr << "exampleSimple1 NULL found" << endl;
695
        if (proc->exceptionOccurred()) {
696
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
697
        }
698
        return;
699
    }
700
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
701
    const char *result = executable->applyTemplatesReturningString();
702
    if (result != NULL) {
703
        cout << result << endl;
704
        sresult->success++;
705
        delete result;
706
    } else {
707
        cout << "Result is null ====== FAIL ====== " << endl;
708
        sresult->failure++;
709
    }
710
    proc->clearParameters();
711
    delete executable;
712
}
713

    
714
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
715
    cout << "ExampleSimple1Err taken from PHP:" << endl;
716

    
717
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
718
    if(executable == nullptr || proc->exceptionOccurred()) {
719
        if( proc->exceptionOccurred()) {
720
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
721
        }
722
        cout << "Result expected as null " << endl;
723
        proc->exceptionClear();
724
        sresult->success++;
725
        return;
726
    } else {
727
        sresult->failure++;
728
        sresult->failureList.push_back("exampleSimple1Err");
729

    
730
        delete executable;
731
    }
732

    
733
}
734

    
735

    
736
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
737
    cout << "<b>exampleSimple2:</b><br/>" << endl;
738

    
739
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
740
    if(executable== nullptr) {
741
        sresult->failure++;
742
        sresult->failureList.push_back("exampleSimple2");
743
        cerr << "exampleSimple2 NULL found" << endl;
744
        if (proc->exceptionOccurred()) {
745
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
746
        }
747
        return;
748
    }
749

    
750
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
751
    const char *filename = "output1.xml";
752
    executable->setOutputFile(filename);
753
    executable->applyTemplatesReturningFile("output1.xml");
754

    
755
    if (CppTestUtils::exists("output1.xml")) {
756
        cout << "The file $filename exists" << endl;
757
        remove("output1.xml");
758
        sresult->success++;
759
    } else {
760
        cout << "The file " << filename << " does not exist" << endl;
761
        if (executable->exceptionOccurred()) {
762
            cout << proc->getErrorMessage() << endl;
763
        }
764
        sresult->failure++;
765
        sresult->failureList.push_back("exampleSimple2");
766
    }
767

    
768
    delete executable;
769

    
770
}
771

    
772
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
773
    cout << "<b>exampleSimple3:</b><br/>" << endl;
774
    proc->clearParameters();
775

    
776
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
777
    if (xdmNode== nullptr) {
778
        cout << "Error: xdmNode is null'" << endl;
779
        if(saxonProc->exceptionOccurred()) {
780
            cout<<"Error message="<<saxonProc->getErrorMessage()<<endl;
781
        }
782
        sresult->failure++;
783
        sresult->failureList.push_back("exampleSimple3");
784
        return;
785
    }
786

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

    
789
   if(executable == nullptr) {
790
        cout << "executable is NULL" <<endl;
791
        if(proc->exceptionOccurred()) {
792
                cout << proc->getErrorMessage() <<endl;
793
                
794
        }
795
        sresult->failure++;
796
        sresult->failureList.push_back("exampleSimple3");
797
        return;
798
   }
799

    
800
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
801
    sresult->success++;
802
    delete xdmNode;
803
    delete executable;
804
}
805

    
806
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
807
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
808

    
809
    XsltExecutable * executable = proc->compileFromFile(nullptr);
810

    
811

    
812
    if (executable == nullptr) {
813

    
814

    
815
        cout << "Expected failure of test exampleSimple3aError:" << endl;
816
        if(proc->exceptionOccurred()) {
817
            cout<<proc->getErrorMessage()<<endl;
818
        }
819

    
820
        sresult->success++;
821
        proc->exceptionClear();
822
        return;
823
    }
824

    
825
    cout << "Error: executable is not nullptr'" << endl;
826
    sresult->failure++;
827
    sresult->failureList.push_back("exampleSimple3aError");
828
    delete executable;
829
    return;
830

    
831

    
832
}
833

    
834
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
835
    cout << "Test: ExampleParam" << endl;
836

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

    
839

    
840
    if(executable == nullptr) {
841
        cout << "executable is NULL" <<endl;
842
        if(proc->exceptionOccurred()) {
843
            cout << "Error : " << proc->getErrorMessage() <<endl;
844

    
845
        }
846
        sresult->failure++;
847
        sresult->failureList.push_back("exampleParam");
848
        proc->exceptionClear();
849
        return;
850

    
851
    }
852

    
853
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
854

    
855
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
856
    if (xdmvalue != nullptr) {
857

    
858
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
859

    
860
    } else {
861
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
862
        sresult->failure++;
863
        sresult->failureList.push_back("exampleParam");
864
        if(executable->exceptionOccurred()) {
865
            SaxonApiException * exception = executable->getException();
866
            cerr<<"Error: " << exception->getMessage() <<endl;
867
            delete exception;
868
        }
869
        delete executable;
870
        return;
871
    }
872
    const char *result = executable->applyTemplatesReturningString();
873
    if (result != nullptr) {
874
        cout << "Output:" << result << endl;
875
        sresult->success++;
876
        delete result;
877
    } else {
878
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
879
        sresult->failure++;
880
        sresult->failureList.push_back("exampleParam");
881
        if(executable->exceptionOccurred()) {
882
            SaxonApiException * exception = executable->getException();
883
            cerr<<"Error: " << exception->getMessage() <<endl;
884
            delete exception;
885
        }
886
        delete executable;
887
        return;
888
    }
889

    
890
    //proc->clearParameters();
891
    //unset($result);
892
    //echo 'again with a no parameter value<br/>';
893

    
894
    executable->setProperty("!indent", "yes");
895
    const char *result2 = executable->applyTemplatesReturningString();
896

    
897

    
898
    if (result2 != nullptr) {
899
        cout << "Result2 output= " << result2 << endl;
900
        sresult->success++;
901
        delete result2;
902
    } else {
903
        cout << "Result2 is NULL<br/>  ======= fail =====" << endl;
904
        sresult->failure++;
905
        sresult->failureList.push_back("exampleParam");
906
        if(executable->exceptionOccurred()) {
907
            SaxonApiException * exception = executable->getException();
908
            cerr<<"Error: " << exception->getMessage() <<endl;
909
            delete exception;
910
        }
911
        delete xdmvalue;
912
        delete executable;
913
        return;
914
    }
915

    
916
    //  unset($result);
917
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
918
    //delete xdmvalue;
919
    //executable->clearParameters();
920

    
921
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
922

    
923
    if (xdmValue2 == nullptr) {
924
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
925
        sresult->failure++;
926
        sresult->failureList.push_back("exampleParam");
927
        if(executable->exceptionOccurred()) {
928
            SaxonApiException * exception = executable->getException();
929
            cerr<<"Error: " << exception->getMessage() <<endl;
930
            delete exception;
931
        }
932
        delete executable;
933
        delete xdmvalue;
934
        return;
935
    }
936

    
937
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
938
    delete xdmvalue;
939

    
940

    
941
    const char *result3 = executable->applyTemplatesReturningString();
942
    if (result3 != nullptr) {
943
        cout << "Output =" << result3 << endl;
944

    
945
        sresult->success++;
946
        delete result3;
947

    
948
    } else {
949
        cout << "Error in result ===== FAIL =======" << endl;
950
        sresult->failure++;
951
        sresult->failureList.push_back("exampleParam");
952
        if(executable->exceptionOccurred()) {
953
            SaxonApiException * exception = executable->getException();
954
            cerr<<"Error: " << exception->getMessage() <<endl;
955
            delete exception;
956
        }
957

    
958
    }
959
    delete xdmValue2;
960
    delete executable;
961

    
962

    
963

    
964
}
965

    
966
// test parameter and properties maps where we update key, value pair.
967
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
968
    cout << "\nExampleParam:</b><br/>" << endl;
969
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
970
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
971

    
972

    
973
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
974
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
975
    if (xdmvalue != NULL) {
976

    
977
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
978
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
979

    
980
    } else {
981
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
982
        sresult->failure++;
983
        sresult->failureList.push_back("exampleParam-1");
984
    }
985
    const char *result = executable->applyTemplatesReturningString();
986
    if (result != NULL) {
987
        string sresulti = string(result);
988
        if (sresulti.compare("Hello from me") == 0) {
989
            cout << "Output:" << result << endl;
990
            sresult->success++;
991
        } else {
992
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
993
            sresult->failure++;
994
            sresult->failureList.push_back("exampleParam-2");
995
        }
996
    } else {
997
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
998
        sresult->failure++;
999
        sresult->failureList.push_back("exampleParam-2");
1000
    }
1001

    
1002
//proc->clearParameters();
1003
//unset($result);
1004
//echo 'again with a no parameter value<br/>';
1005

    
1006
    executable->setProperty("!indent", "no");
1007
    executable->setProperty("!indent", "yes");
1008
    const char *result2 = executable->applyTemplatesReturningString();
1009

    
1010
    executable->clearProperties();
1011

    
1012
    if(result2 != nullptr) {
1013
        cout<<result2<<endl;
1014
        sresult->success++;
1015

    
1016
    }
1017

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

    
1023
    const char *result3 = executable->applyTemplatesReturningString();
1024
    if(result3 != nullptr) {
1025
        cout<<"Output ="<<result3<<endl;
1026

    
1027
        sresult->success++;
1028
    } else {
1029
        cout<<"Error in result ===== FAIL ======="<<endl;
1030
        sresult->failure++;
1031
        sresult->failureList.push_back("exampleParam");
1032
    }
1033

    
1034
}
1035

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

    
1040
    proc->setJustInTimeCompilation(true);
1041

    
1042
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1043
    if (result != nullptr && !proc->exceptionOccurred()) {
1044
        cout << "XdmNode returned" << endl;
1045
        sresult->success++;
1046
        delete result;
1047
    } else {
1048
        printf("result is null \nCheck For errors:");
1049
        sresult->failure++;
1050
        sresult->failureList.push_back("xmarkTest1");
1051
        if (proc->exceptionOccurred()) {
1052
            const char * message = proc->getErrorMessage();
1053
            if(message != nullptr) {
1054
                cerr << proc->getErrorMessage() << endl;
1055
                proc->exceptionClear();
1056
            } else {
1057
                cerr << "Message is nullptr" << endl;
1058
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1059
                SaxonProcessor::sxn_environ->env->ExceptionClear();
1060
            }
1061
        }
1062
    }
1063
    proc->setJustInTimeCompilation(false);
1064

    
1065
}
1066

    
1067

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

    
1072
    proc->setJustInTimeCompilation(true);
1073

    
1074
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1075
    if (result != nullptr && !proc->exceptionOccurred()) {
1076
        cout << "XdmNode returned" << endl;
1077
        sresult->success++;
1078
        delete result;
1079

    
1080
    } else {
1081
        printf("result is null \nCheck For errors:");
1082
        sresult->failure++;
1083
        sresult->failureList.push_back("xmarkTest2");
1084
        if (proc->exceptionOccurred() > 0) {
1085
            SaxonApiException * exception = proc->getException();
1086
            if(exception != nullptr) {
1087
                const char *message = exception->getMessage();
1088
                if (message != nullptr) {
1089
                    cerr << message << endl;
1090
                    proc->exceptionClear();
1091
                }
1092
                delete exception;
1093
            } else {
1094
                cerr << "Exception object is nullptr" << endl;
1095
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1096
                SaxonProcessor::sxn_environ->env->ExceptionClear();
1097
            }
1098
        }
1099
    }
1100

    
1101
    proc->setJustInTimeCompilation(false);
1102

    
1103
}
1104

    
1105
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1106
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
1107
    cout << "exampleSimple_xmark test - test q12.xsl:" << endl;
1108

    
1109
    proc->setJustInTimeCompilation(true);
1110

    
1111
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1112

    
1113
    if (result != nullptr && !proc->exceptionOccurred()) {
1114
        cout << "XdmNode returned" << endl;
1115
        sresult->success++;
1116
        delete result;
1117
    } else {
1118
        printf("Result is null \nCheck For errors:");
1119
        if (proc->exceptionOccurred() > 0) {
1120
            cout << proc->getErrorMessage() << endl;
1121
        }
1122
        sresult->failure++;
1123
        sresult->failureList.push_back("exampleSimple_xmark");
1124
        proc->exceptionClear();
1125
    }
1126
    proc->clearParameters();
1127
    proc->setJustInTimeCompilation(false);
1128

    
1129
}
1130

    
1131
/*
1132
* Test saving nd loading a Xslt package
1133
*/
1134
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1135

    
1136
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1137
    trans->clearParameters();
1138

    
1139

    
1140
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1141
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1142

    
1143
    if (output== nullptr) {
1144
        printf("result is null \n");
1145
        const char *message = trans->getErrorMessage();
1146
        if (message != NULL) {
1147
            cout << "Error message =" << message << endl;
1148
        }
1149
        sresult->failure++;
1150
        sresult->failureList.push_back("testPackage1");
1151

    
1152
    } else {
1153
        printf("%s", output);
1154
        printf("result is OK \n");
1155
        sresult->success++;
1156
    }
1157
    fflush(stdout);
1158
    delete output;
1159
}
1160

    
1161

    
1162
/*
1163
* Test saving and loading a Xslt package
1164
*/
1165
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1166

    
1167
    cout << endl << "Test: testPackage1a" << endl;
1168
    trans->clearParameters();
1169

    
1170
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1171

    
1172
    if(trans->exceptionOccurred()) {
1173
        const char *message = trans->getErrorMessage();
1174
        if (message != nullptr) {
1175
            cout << "Error message =" << message << endl;
1176
        }
1177
        sresult->failure++;
1178
        sresult->failureList.push_back("testPackage1a");
1179
        trans->exceptionClear();
1180
        return;
1181
    }
1182

    
1183

    
1184

    
1185
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1186

    
1187
    if(executable == nullptr) {
1188
        if(trans->exceptionOccurred()) {
1189
          const char *message = trans->getErrorMessage();
1190
          if (message != nullptr) {
1191
            cout << "Error message =" << message << endl;
1192
          }
1193
          sresult->failure++;
1194
          sresult->failureList.push_back("testPackage1a");
1195
          trans->exceptionClear();
1196
            }
1197
        return;   
1198

    
1199

    
1200
    }
1201

    
1202
    executable->setInitialMatchSelectionAsFile("cat.xml");
1203
    const char * output = executable->applyTemplatesReturningString();
1204
    if (output== nullptr) {
1205
        printf("result is null \n");
1206
        const char *message = trans->getErrorMessage();
1207
        if (message != NULL) {
1208
            cout << "Error message =" << message << endl;
1209
        }
1210
        sresult->failure++;
1211
        sresult->failureList.push_back("testPackage1a");
1212
        trans->exceptionClear();
1213

    
1214
    } else {
1215
        printf("%s", output);
1216
        printf("result is OK \n");
1217
        sresult->success++;
1218
        delete output;
1219
    }
1220
    fflush(stdout);
1221
    delete executable;
1222
}
1223

    
1224

    
1225
/*
1226
* Test saving and loading a Xslt package
1227
*/
1228
void testPackage2_Error(Xslt30Processor *trans, sResultCount *sresult) {
1229

    
1230
    cout << endl << "Test: testPackage2_Error:" << endl;
1231

    
1232
    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>";
1233

    
1234
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1235

    
1236
    if(trans->exceptionOccurred() || trans->getException() != nullptr) {
1237
        const char *message = trans->getErrorMessage();
1238
        if (message != nullptr) {
1239
            cout << "Error message =" << message << endl;
1240
        }
1241
        sresult->success++;
1242
        trans->exceptionClear();
1243
        return;
1244

    
1245
    }
1246
    trans->exceptionClear();
1247
    sresult->failure++;
1248
    sresult->failureList.push_back("testPackage2_Error");
1249

    
1250
}
1251

    
1252
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1253

    
1254
    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>";
1255
    cout << endl << "Test: testCallFunction:" << endl;
1256
    XsltExecutable * executable = trans->compileFromString(source);
1257

    
1258
    if(executable == nullptr) {
1259
        if(trans->exceptionOccurred()) {
1260
            SaxonApiException * exception = trans->getException();
1261
            if(exception != nullptr) {
1262
                cerr << "Error: " << exception->getMessage() << endl;
1263
                delete exception;
1264
            }
1265

    
1266
        }
1267
        sresult->failure++;
1268
        sresult->failureList.push_back("testCallFunction");
1269
        trans->exceptionClear();
1270
        return;
1271

    
1272

    
1273
    }
1274

    
1275
    XdmValue **valueArray = new XdmValue *[2];
1276

    
1277
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1278
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1279
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1280

    
1281
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1282
        sresult->success++;
1283
        delete v;
1284
    } else {
1285
        if (v != NULL && !(v->getHead())->isAtomic()) {
1286
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1287
        }
1288
        cout << "testCallFunction ======= FAIL ======" << endl;
1289
        if(executable->exceptionOccurred()) {
1290
            SaxonApiException * exception = executable->getException();
1291
            if(exception != nullptr) {
1292
                cerr << "Error: " << exception->getMessage() << endl;
1293
                delete exception;
1294
            }
1295
        }
1296
        sresult->failure++;
1297
        sresult->failureList.push_back("testCallFunction");
1298
        executable->exceptionClear();
1299
        delete v;
1300
    }
1301
    delete valueArray[0];
1302
    delete valueArray[1];
1303
    delete [] valueArray;
1304
    delete executable;
1305
}
1306

    
1307
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1308

    
1309

    
1310
    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>";
1311
    cout << endl << "Test:testInitialTemplate" << endl;
1312
    XsltExecutable * executable = trans->compileFromString(source);
1313
    if(executable == nullptr) {
1314
        if(trans->exceptionOccurred()) {
1315
            cout << "Error: "<< trans->getErrorMessage() << endl;
1316
        }
1317
        return;
1318
    }
1319
    XdmNode * node = proc->parseXmlFromString("<e/>");
1320

    
1321
    executable->setResultAsRawValue(false);
1322
    std::map<std::string, XdmValue *> parameterValues;
1323

    
1324
    XdmAtomicValue * a1 = proc->makeIntegerValue(12);
1325
    XdmAtomicValue * a2 = proc->makeIntegerValue(5);;
1326
    parameterValues["a"] = a1;
1327
    parameterValues["b"] = a2;
1328
    executable->setInitialTemplateParameters(parameterValues, false);
1329
    executable->setInitialMatchSelection(node);
1330
    XdmValue *result = executable->applyTemplatesReturningValue();
1331
    if (result != NULL) {
1332
        sresult->success++;
1333
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1334
        delete result;
1335
    } else {
1336
        sresult->failure++;
1337
    }
1338

    
1339
    delete executable;
1340
    delete a1;
1341
    delete a2;
1342
    delete node;
1343
    parameterValues.clear();
1344
}
1345

    
1346
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1347
    cout << endl << "Test: testResolveUri:" << endl;
1348

    
1349
    XsltExecutable * executable = trans->compileFromString(
1350
            "<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>");
1351

    
1352
    if(executable == nullptr) {
1353
        sresult->failure++;
1354
        sresult->failureList.push_back("testResolveUri");
1355
        if(trans->exceptionOccurred()) {
1356
            const char * message = trans->getErrorMessage();
1357
            if(message != nullptr) {
1358
                cout << "Error: " << trans->getErrorMessage() << endl;
1359
            }
1360
        }
1361
        trans->exceptionClear();
1362
        return;
1363
    }
1364
    XdmValue *value = executable->callTemplateReturningValue("go");
1365

    
1366

    
1367
    if (value== nullptr) {
1368
        if (executable->exceptionOccurred()) {
1369
            SaxonApiException *exception = executable->getException();
1370
            cerr << "Error: " << exception->getMessage() << endl;
1371
            delete exception;
1372
        }
1373
        sresult->failure++;
1374
        sresult->failureList.push_back("testResolveUri");
1375
    } else {
1376

    
1377
        const char *svalue = value->itemAt(0)->getStringValue();
1378
        cout << "testResolveUri = " << svalue << endl;
1379
        sresult->success++;
1380
        delete value;
1381
    }
1382

    
1383
    delete executable;
1384
}
1385

    
1386
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1387
    cout << endl << "Test: testContextNotRoot" << endl;
1388

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

    
1391
    XsltExecutable *executable = trans->compileFromString(
1392
            "<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>");
1393

    
1394
    if (executable == nullptr) {
1395
        sresult->failure++;
1396
        sresult->failureList.push_back("testContextNotRoot");
1397
        if (trans->exceptionOccurred()) {
1398
            cout << "Error: " << trans->getErrorMessage() << endl;
1399
        }
1400
        return;
1401
    }
1402

    
1403
    executable->setGlobalContextItem(node);
1404
    if (node->getChildCount() > 0) {
1405
        XdmNode **eNodeL1 = node->getChildren();
1406
        if (eNodeL1 != nullptr) {
1407
            XdmNode ** eNodeChildren = eNodeL1[0]->getChildren();
1408
            if (eNodeChildren != nullptr) {
1409
                XdmNode *eNode = eNodeChildren[0];
1410
                cout << "Node content = " << eNode->toString() << endl;
1411
                executable->setInitialMatchSelection(eNode);
1412
                const char *result = executable->applyTemplatesReturningString();
1413

    
1414
                if (result == nullptr) {
1415

    
1416
                    cout << "testContextNotRoot ======= FAIL ======" << endl;
1417
                    if (executable->exceptionOccurred()) {
1418
                        SaxonApiException *exception = executable->getException();
1419
                        cerr << "Error: " << exception->getMessage() << endl;
1420
                        delete exception;
1421
                    }
1422
                    sresult->failure++;
1423
                    sresult->failureList.push_back("testContextNotRoot");
1424

    
1425

    
1426
                } else {
1427

    
1428
                    cout << "testContextNotRoot = " << result << endl;
1429
                    sresult->success++;
1430
                    delete result;
1431

    
1432
                }
1433

    
1434
            }
1435
        }
1436
    } else {
1437
        sresult->failure++;
1438
        sresult->failureList.push_back("testContextNotRoot");
1439
    }
1440

    
1441
    delete executable;
1442
    delete node;
1443
}
1444

    
1445

    
1446
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1447
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1448

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

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

    
1464
    XsltExecutable * executable = trans->compileFromString(
1465
            "<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>");
1466

    
1467
    if(executable == nullptr) {
1468
        sresult->failure++;
1469
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1470
        if(trans->exceptionOccurred()) {
1471
            cout << "Error: "<< trans->getErrorMessage() << endl;
1472
        }
1473
        delete node;
1474
        return;
1475
    }
1476

    
1477
    executable->setGlobalContextItem(node);
1478
    const char *result = executable->callTemplateReturningString("main");
1479

    
1480
    if (result== nullptr) {
1481

    
1482
        cout << "testContextNotRootNameTemplate ======= FAIL ======" << endl;
1483
        if(executable->exceptionOccurred()) {
1484
            SaxonApiException * exception = executable->getException();
1485
            cerr<<"Error: " << exception->getMessage() <<endl;
1486
            delete exception;
1487
        }
1488
        sresult->failure++;
1489
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1490
    } else {
1491

    
1492
        cout << "testContextNotRoot = " << result << endl;
1493
        sresult->success++;
1494
        delete result;
1495
    }
1496
    delete node;
1497
    delete executable;
1498

    
1499
}
1500

    
1501

    
1502
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1503
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1504

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

    
1507
    if(node == nullptr) {
1508
        if(proc->exceptionOccurred()) {
1509
            const char * message = proc->getErrorMessage();
1510
            if(message != nullptr) {
1511
                cerr << "Error: " << message << endl;
1512
            }
1513
            proc->exceptionClear();
1514
        }
1515
        sresult->failure++;
1516
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1517
        return;
1518
    }
1519

    
1520
    XsltExecutable * executable = trans->compileFromString(
1521
            "<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>");
1522

    
1523
    if(executable == nullptr) {
1524
        sresult->failure++;
1525
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1526
        if(trans->exceptionOccurred()) {
1527
            cout << "Error: "<< trans->getErrorMessage() << endl;
1528
        }
1529
        delete node;
1530
        return;
1531
    }
1532

    
1533
    executable->setGlobalContextItem(node);
1534
    XdmValue *result = executable->callTemplateReturningValue("main");
1535

    
1536
    if (result == nullptr) {
1537

    
1538
        cout << "testCallFunction ======= FAIL ======" << endl;
1539
        if(executable->exceptionOccurred()) {
1540
            SaxonApiException * exception = executable->getException();
1541
            cerr<<"Error: " << exception->getMessage() <<endl;
1542
            delete exception;
1543
        }
1544
        sresult->failure++;
1545
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1546
    } else {
1547

    
1548
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1549
        sresult->success++;
1550
        delete result;
1551
        result = nullptr;
1552
    }
1553

    
1554
    delete node;
1555
    delete executable;
1556

    
1557

    
1558
}
1559

    
1560
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1561

    
1562
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1563
                                                            1);
1564
    if (fi== nullptr) {
1565
        sresult->failure++;
1566
        sresult->failureList.push_back("testCallSystemFunction");
1567
        return;
1568
    }
1569

    
1570
    XdmValue ** xdmValue = new XdmValue*[1];
1571
    xdmValue[0] = new XdmValue();
1572
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1573
    XdmValue *result = fi->call(xdmValue, 1);
1574

    
1575
    std::cerr << "Result = " << result->toString() << endl;
1576
    if(result->size() == 3) {
1577

    
1578
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1579
        sresult->success++;
1580

    
1581
    } else {
1582
        sresult->failure++;
1583
        sresult->failureList.push_back("testCallSystemFunction");
1584

    
1585
    }
1586

    
1587
}
1588

    
1589

    
1590
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1591
    cout << endl << "Test: testPipeline" << endl;
1592

    
1593
    Xslt30Processor * trans = proc->newXslt30Processor();
1594
    if(trans == nullptr) {
1595
        cout << "Error: Xslt30Processor is null - maybe unclean state of JNI" << endl;
1596
        sresult->failure++;
1597
        sresult->failureList.push_back("testPipeline");
1598
        return;
1599
    }
1600
    XsltExecutable * stage1 = trans->compileFromString(
1601
            "<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>");
1602

    
1603
    if(stage1 == nullptr) {
1604
        sresult->failure++;
1605
        sresult->failureList.push_back("testPipeline");
1606
        if(trans->exceptionOccurred()) {
1607
            const char * message = trans->getErrorMessage();
1608
            cout << "stage 1 Error: " << message << endl;
1609

    
1610
        }
1611
        cout << "Stage 1 Error - exit method " << endl;
1612
        delete trans;
1613
        return;
1614
    }
1615

    
1616
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1617

    
1618
    XsltExecutable *stage2 = trans->compileFromString(
1619
            "<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>");
1620

    
1621
    if(stage2 == nullptr) {
1622
        sresult->failure++;
1623
        sresult->failureList.push_back("testPipeline");
1624
        if(trans->exceptionOccurred()) {
1625
            const char * message = trans->getErrorMessage();
1626
            cout << "stage 2 Error: " << message << endl;
1627

    
1628
        }
1629
        cout << "Stage 2 Error - exit method " << endl;
1630
        delete stage1;
1631
        delete trans;
1632
        delete inn;
1633
        return;
1634
    }
1635

    
1636

    
1637
    XsltExecutable *stage3 = trans->compileFromString(
1638
            "<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>");
1639

    
1640
    if(stage3 == nullptr) {
1641
        sresult->failure++;
1642
        sresult->failureList.push_back("testPipeline");
1643
        if(trans->exceptionOccurred()) {
1644
            const char * message = trans->getErrorMessage();
1645
            cout << "stage 3 Error: " << message << endl;
1646

    
1647
        } else {
1648
            cout << "Stage 3 Error: testPipeline failed with exception" << endl;
1649
        }
1650
        cout << "Stage 3 Error - exit method " << endl;
1651
        delete stage1;
1652
        delete stage2;
1653
        delete trans;
1654
        delete inn;
1655
        return;
1656
    }
1657

    
1658
    XsltExecutable *stage4 = trans->compileFromString(
1659
            "<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>");
1660

    
1661
    if(stage4 == nullptr) {
1662
        sresult->failure++;
1663
        sresult->failureList.push_back("testPipeline");
1664
        if(trans->exceptionOccurred()) {
1665
            const char * message = trans->getErrorMessage();
1666
            cout << "stage 4 Error: " << message << endl;
1667

    
1668
        } else {
1669
            cout << "Stage 4 Error: testPipeline failed with exception" << endl;
1670
        }
1671
        cout << "Stage 4 Error - exit method " << endl;
1672
        delete stage1;
1673
        delete stage2;
1674
        delete stage3;
1675
        delete trans;
1676
        delete inn;
1677
        return;
1678
    }
1679

    
1680
    XsltExecutable *stage5 = trans->compileFromString(
1681
            "<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>");
1682

    
1683
    if(stage5 == nullptr) {
1684
        sresult->failure++;
1685
        sresult->failureList.push_back("testPipeline");
1686
        if(trans->exceptionOccurred()) {
1687
            const char * message = trans->getErrorMessage();
1688
            cout << "stage 5 Error: " << message << endl;
1689

    
1690
        } else {
1691
            cout << "Stage 5 Error: testPipeline failed with exception" << endl;
1692
        }
1693
        cout << "Stage 5     Error - exit method " << endl;
1694
        delete stage1;
1695
        delete stage2;
1696
        delete stage3;
1697
        delete stage4;
1698
        delete trans;
1699
        delete inn;
1700
        return;
1701
    }
1702

    
1703
    stage1->setProperty("!indent", "no");
1704
    stage1->setInitialMatchSelection(inn);
1705

    
1706
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1707
    if(d1 == nullptr) {
1708
        if (stage1->exceptionOccurred()) {
1709
            sresult->failure++;
1710
            sresult->failureList.push_back("testPipeline");
1711
            if (stage1->exceptionOccurred()) {
1712
                SaxonApiException *exception = stage1->getException();
1713
                if (exception != nullptr) {
1714
                    cerr << "Error: " << exception->getMessage() << endl;
1715
                    delete exception;
1716
                    exception = nullptr;
1717
                }
1718
            }
1719
            cout << "Stage d1 Error - exit method " << endl;
1720
            delete stage1;
1721
            delete stage2;
1722
            delete stage3;
1723
            delete stage4;
1724
            delete stage5;
1725
            delete trans;
1726
            delete inn;
1727
            return;
1728
        }
1729
    }
1730

    
1731
    XdmItem *d11 = d1->getHead();
1732
    if (d11== nullptr) {
1733

    
1734
        cout << "d11 is NULL\n" << endl;
1735
        sresult->failure++;
1736
        sresult->failureList.push_back("testPipeline-1");
1737
        delete stage1;
1738
        delete stage2;
1739
        delete stage3;
1740
        delete stage4;
1741
        delete stage5;
1742
        delete d1;
1743
        delete inn;
1744
        delete trans;
1745
        return;
1746
    }
1747
    const char *data = d11->toString();
1748

    
1749
    if (data != NULL) {
1750
        cout << "d1 result=" << data << endl;
1751
    } else {
1752

    
1753
        cout << "d1 result Error - toString is NULL" << endl;
1754
        delete stage1;
1755
        delete stage2;
1756
        delete stage3;
1757
        delete stage4;
1758
        delete stage5;
1759
        delete d1;
1760
        delete inn;
1761
        delete trans;
1762
        return;
1763
    }
1764
    stage2->setProperty("!indent", "no");
1765
    stage2->setInitialMatchSelection(d11);
1766
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1767
    if (d2== nullptr) {
1768
        cout << "ERROR-11\n" << endl;
1769
        sresult->failure++;
1770
        sresult->failureList.push_back("testPipeline-2");
1771
        if (stage2->exceptionOccurred()) {
1772
            SaxonApiException *exception = stage2->getException();
1773
            if (exception != nullptr) {
1774
                cerr << "Error: " << exception->getMessage() << endl;
1775
                delete exception;
1776
                exception = nullptr;
1777
            }
1778
        }
1779
        cout << "Stage d2 Error - exit method " << endl;
1780
        delete d1;
1781
        delete inn;
1782
        delete stage1;
1783
        delete stage2;
1784
        delete stage3;
1785
        delete stage4;
1786
        delete stage5;
1787
        delete trans;
1788
        return;
1789
    }
1790
    stage3->setProperty("!indent", "no");
1791
    stage3->setInitialMatchSelection(d2);
1792
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1793
    if(d3 == nullptr){
1794
        sresult->failure++;
1795
        sresult->failureList.push_back("testPipeline-3");
1796
        if (stage3->exceptionOccurred()) {
1797
            SaxonApiException *exception = stage3->getException();
1798
            if (exception != nullptr) {
1799
                cerr << "Error: " << exception->getMessage() << endl;
1800
                delete exception;
1801
                exception = nullptr;
1802
            }
1803
        }
1804
        cout << "Stage d3 Error - exit method " << endl;
1805
        delete d1;
1806
        delete d2;
1807
        delete inn;
1808

    
1809
        delete stage1;
1810
        delete stage2;
1811
        delete stage3;
1812
        delete stage4;
1813
        delete stage5;
1814
        delete trans;
1815
        return;
1816
      }
1817
    stage4->setProperty("!indent", "no");
1818
    stage4->setInitialMatchSelection(d3);
1819
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1820
    if(d4== nullptr){
1821
        sresult->failure++;
1822
        sresult->failureList.push_back("testPipeline-4");
1823
        if (stage4->exceptionOccurred()) {
1824
            SaxonApiException *exception = stage4->getException();
1825
            if (exception != nullptr) {
1826
                cerr << "Error: " << exception->getMessage() << endl;
1827
                delete exception;
1828
                exception = nullptr;
1829
            }
1830
        }
1831
        cout << "Stage d4 Error - exit method " << endl;
1832
        delete d3;
1833
        delete d2;
1834
        delete d1;
1835
        delete inn;
1836
        delete stage1;
1837
        delete stage2;
1838
        delete stage3;
1839
        delete stage4;
1840
        delete stage5;
1841
        delete trans;
1842
        return;
1843
      }
1844
    stage5->setProperty("!indent", "no");
1845
    stage5->setInitialMatchSelection(d4);
1846
    const char * sw = stage5->applyTemplatesReturningString();
1847
    if(sw == nullptr){
1848
        sresult->failure++;
1849
        sresult->failureList.push_back("testPipeline-5");
1850
        if (stage5->exceptionOccurred()) {
1851
            SaxonApiException *exception = stage5->getException();
1852
            if (exception != nullptr) {
1853
                cerr << "Error: " << exception->getMessage() << endl;
1854
                delete exception;
1855
            }
1856
        }
1857
        cout << "Stage sw Error - exit method " << endl;
1858
        delete stage1;
1859
        delete stage2;
1860
        delete stage3;
1861
        delete stage4;
1862
        delete stage5;
1863
        delete trans;
1864
        delete d4;
1865
        delete d3;
1866
        delete d2;
1867
        delete d1;
1868
        delete inn;
1869
        return;
1870
      }
1871
    cout<<sw<<endl;
1872
    cout << "testPipeline = " << sw << endl;
1873
    sresult->success++;
1874
    delete stage1;
1875
    stage1 = nullptr;
1876

    
1877
    delete stage2;
1878
    stage2 = nullptr;
1879

    
1880

    
1881
    delete stage3;
1882
    stage3 = nullptr;
1883

    
1884
    delete stage4;
1885
    stage4 = nullptr;
1886

    
1887
    delete stage5;
1888
    stage5 = nullptr;
1889

    
1890
    delete trans;
1891
    trans = nullptr;
1892

    
1893
    delete sw;
1894
    sw = nullptr;
1895

    
1896
    delete d4;
1897
    d4 = nullptr;
1898

    
1899
    delete d3;
1900
    d3 = nullptr;
1901

    
1902
    delete d2;
1903
    d2 = nullptr;
1904

    
1905
    delete d1;
1906
    d1 = nullptr;
1907

    
1908
    delete inn;
1909
    inn = nullptr;
1910

    
1911
}
1912

    
1913

    
1914
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1915

    
1916

    
1917
    cout << endl << "Test: testCatalog" << endl;
1918
    bool trace = false;
1919
    proc->setcwd(cwd);
1920
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1921

    
1922
    Xslt30Processor * trans = proc->newXslt30Processor();
1923

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

    
1926
    if(executable == nullptr) {
1927

    
1928
        if (trans->exceptionOccurred()) {
1929

    
1930
            if(trans->getErrorMessage() != nullptr) {
1931
                const char *message = trans->getErrorMessage();
1932
                cerr << "exception=" << message << endl;
1933
            } else {
1934
                const char *message = proc->getErrorMessage();
1935
                if(message != nullptr) {
1936
                    cerr << "exception-proc=" << message << endl;
1937
                }
1938
            }
1939
        }
1940
        sresult->failure++;
1941
        sresult->failureList.push_back("testCatalog");
1942
        trans->exceptionClear();
1943
        delete trans;
1944
        trans= nullptr;
1945
        return;
1946
    }
1947

    
1948
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1949

    
1950
    const char *result = executable->applyTemplatesReturningString();
1951

    
1952
    if(result != NULL) {
1953
        std::cerr << "testCatalog result= " << result << std::endl;
1954
        delete result;
1955
        sresult->success++; // TODO - check the results more carefully
1956
    } else {
1957
        sresult->failure++;
1958
        sresult->failureList.push_back("testCatalog");
1959
    }
1960

    
1961

    
1962
    delete executable;
1963
    executable = nullptr;
1964
    delete trans;
1965
    trans = nullptr;
1966

    
1967
}
1968

    
1969
static int NUM_THREADS = 10;
1970

    
1971
void RunThread(XsltExecutable * executable, int tid, const std::string filename) {
1972
    JavaVMAttachArgs att_arg;
1973
    att_arg.version = JNI_VERSION_1_2;
1974
    att_arg.name = NULL;
1975
    att_arg.group = NULL;
1976

    
1977
   // SaxonProcessor::sxn_environ->jvm->AttachCurrentThread((void**)&SaxonProcessor::sxn_environ->env, &att_arg);
1978
    cerr<<endl<<"RunThread cp0,  THEAD ID="<<tid<<endl;
1979
    fflush(stderr);
1980
    fflush(stdout);
1981

    
1982
    if(executable != nullptr) {
1983
       executable->setInitialMatchSelectionAsFile(filename.c_str());
1984

    
1985
        cerr << "RunThread cp1" << endl;
1986

    
1987
       const char *result = nullptr;
1988
        result = executable->applyTemplatesReturningString();
1989
        if (result != nullptr) {
1990
            cout << " Result from THREAD ID: " << tid << ", " << result << endl;
1991
            delete result;
1992
        } else {
1993
            cerr << " ===== Failed in THREAD ID: " << tid << endl;
1994
           /* if(executable->exceptionOccurred()) {
1995
                SaxonApiException *exception = executable->getException();
1996

1997
                if (exception != nullptr) {
1998
                    const char *message = exception->getMessage();
1999
                    if (message != nullptr) {
2000
                        cerr << "Error = " << message << endl;
2001
                    }
2002
                    delete exception;
2003
                }
2004
            } else {
2005
            cerr << "No exception found - result is nullptr" << endl;
2006
            }*/
2007

    
2008
        }
2009
    delete executable;
2010
    } else {
2011
        cerr << "XsltExecutable is nullptr" << endl;
2012
    }
2013

    
2014
   // (   SaxonProcessor::sxn_environ->jvm)->DetachCurrentThread();
2015

    
2016
}
2017

    
2018
void testThreads (SaxonProcessor * processor, Xslt30Processor * trans, sResultCount *sresult) {
2019

    
2020
    std::vector<std::string> s = {
2021
            "../data/xmark100k.xml",
2022
            "../data/xmark1.xml",
2023
            "../data/xmark4.xml",
2024
            "../data/xmark10.xml"
2025
    };
2026

    
2027
    std::vector<std::thread> threads;
2028

    
2029
    for (int i = 0; i < s.size(); i++) {
2030

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

    
2034
    }
2035
    cerr<<"testThreads cp0"<<endl;
2036
    for (auto &th : threads) {
2037
        th.join();
2038
        cerr<<"testThreads cp1 - loop"<<endl;
2039
    }
2040
}
2041

    
2042

    
2043

    
2044
int main(int argc, char* argv[]) {
2045

    
2046

    
2047
    const char * cwd = nullptr;
2048
    if(argc > 0) {
2049
        cwd = argv[1];
2050
    }
2051

    
2052
    SaxonProcessor *processor = new SaxonProcessor(true);
2053
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
2054
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
2055
    
2056
    char buff[FILENAME_MAX]; //create string buffer to hold path
2057
    GetCurrentDir( buff, FILENAME_MAX );
2058
    cout<<"CWD = "<< buff<<endl;
2059
    if(cwd != nullptr) {
2060
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
2061
    } else {
2062
        processor->setcwd(buff);
2063
    }
2064
    if (processor->isSchemaAwareProcessor()) {
2065

    
2066
        std::cerr << "Processor is Schema Aware" << std::endl;
2067
    } else {
2068
        std::cerr << "Processor is not Schema Aware" << std::endl;
2069
    }
2070

    
2071
    sResultCount *sresult = new sResultCount();
2072
    Xslt30Processor *trans = processor->newXslt30Processor();
2073

    
2074
    if( trans == nullptr) {
2075
        cout<< "Error creating Xslt30Processor"<<endl;
2076
        if(processor->exceptionOccurred()) {
2077
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
2078

    
2079
        }
2080
        return -1;
2081
    }
2082
    //testValidation(trans,sresult);
2083

    
2084
    testInitialTemplate(processor, trans, sresult);
2085

    
2086
    cout<<endl<<"============================================================="<<endl<<endl;
2087

    
2088
    exampleSimple1Err(trans, sresult);
2089

    
2090
    cout<<endl<<"============================================================="<<endl<<endl;
2091

    
2092
    exampleSimple1(trans, sresult);
2093

    
2094
    cout<<endl<<"============================================================="<<endl<<endl;
2095

    
2096
    exampleSimple_xmark(trans, sresult);
2097

    
2098
    cout<<endl<<"============================================================="<<endl<<endl;
2099

    
2100
    exampleSimple2(trans, sresult);
2101

    
2102
    cout<<endl<<"============================================================="<<endl<<endl;
2103

    
2104
    exampleSimple3(processor, trans, sresult);
2105

    
2106
    cout<<endl<<"============================================================="<<endl<<endl;
2107

    
2108
    exampleSimple3aError(processor, trans, sresult);
2109

    
2110
    cout<<endl<<"============================================================="<<endl<<endl;
2111

    
2112
    testApplyTemplatesString1(trans, sresult);
2113

    
2114
    cout<<endl<<"============================================================="<<endl<<endl;
2115

    
2116
    testApplyTemplatesString2(processor, trans, sresult);
2117

    
2118
    cout<<endl<<"============================================================="<<endl<<endl;
2119

    
2120

    
2121
    testApplyTemplates2a_Error(processor, trans, sresult);
2122

    
2123
    cout<<endl<<"============================================================="<<endl<<endl;
2124

    
2125
    testTransformToString4(processor, trans, sresult);
2126

    
2127
    cout<<endl<<"============================================================="<<endl<<endl;
2128

    
2129
    testCatalog(cwd, processor,  sresult);
2130

    
2131
    cout<<endl<<"============================================================="<<endl<<endl;
2132

    
2133
    testTransformToString2b(processor, trans, sresult);
2134

    
2135
    cout<<endl<<"============================================================="<<endl<<endl;
2136

    
2137
    testTransformToString3(processor, trans, sresult);
2138

    
2139
    cout<<endl<<"============================================================="<<endl<<endl;
2140
        
2141
    testTransformFromstring(processor, trans, sresult);
2142

    
2143

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

    
2146
    testTransformFromstring2Err(processor, trans, sresult);
2147

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

    
2150
    testTrackingOfValueReference(processor, trans, sresult);
2151

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

    
2154
    testTrackingOfValueReferenceError(processor, trans, sresult);
2155

    
2156
    cout<<endl<<"============================================================="<<endl<<endl;
2157

    
2158
    testPackage1(trans, sresult);
2159

    
2160
    cout<<endl<<"============================================================="<<endl<<endl;
2161

    
2162
    testPackage1a(trans, sresult);
2163

    
2164
    cout<<endl<<"============================================================="<<endl<<endl;
2165

    
2166
    testPackage2_Error(trans, sresult);
2167

    
2168
    cout<<endl<<"============================================================="<<endl<<endl;
2169

    
2170
    testXdmNodeOutput(trans, sresult);
2171

    
2172
    cout<<endl<<"============================================================="<<endl<<endl;
2173

    
2174
    exampleParam(processor, trans, sresult);
2175

    
2176
    cout<<endl<<"============================================================="<<endl<<endl;
2177

    
2178
    xmarkTest1(trans, sresult);
2179

    
2180
    cout<<endl<<"============================================================="<<endl<<endl;
2181

    
2182
    xmarkTest2(trans, sresult);
2183

    
2184
    cout<<endl<<"============================================================="<<endl<<endl;
2185

    
2186
    testCallFunction(processor, trans, sresult);
2187

    
2188
    cout<<endl<<"============================================================="<<endl<<endl;
2189

    
2190
    testResolveUri(processor, trans, sresult);
2191

    
2192
    cout<<endl<<"============================================================="<<endl<<endl;
2193

    
2194
    testContextNotRoot(processor, trans, sresult);
2195

    
2196
    cout<<endl<<"============================================================="<<endl<<endl;
2197

    
2198
    testContextNotRootNamedTemplate(processor, trans, sresult);
2199

    
2200
    cout<<endl<<"============================================================="<<endl<<endl;
2201

    
2202
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
2203

    
2204
    cout<<endl<<"============================================================="<<endl<<endl;
2205

    
2206
   testPipeline(processor, sresult);
2207

    
2208

    
2209
    cout<<endl<<"============================================================="<<endl<<endl;
2210

    
2211
    fflush(stdout);
2212

    
2213

    
2214
    //Available in PE and EE
2215
    //testTransformToStringExtensionFunc(processor, trans);
2216

    
2217

    
2218

    
2219
    //processor->release();
2220
    //return 0;
2221

    
2222
    fflush(stdout);
2223

    
2224

    
2225
    //set to the current working directory
2226
    if(cwd != nullptr) {
2227
        processor->setcwd(cwd);
2228
    } else {
2229
        processor->setcwd(buff);
2230
    }
2231
    Xslt30Processor *trans2 = processor->newXslt30Processor();
2232
    cerr<<" after trans2 cp0"<<endl;
2233
    testApplyTemplatesString1(trans2, sresult);
2234
    cout<<endl<<"============================================================="<<endl<<endl;
2235

    
2236

    
2237

    
2238
    //testThreads (processor, trans2, sresult);
2239

    
2240

    
2241
    delete trans;
2242
    delete trans2;
2243
    delete processor;
2244
    processor->release();
2245

    
2246

    
2247
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
2248

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

    
2252
    std::list<std::string>::iterator it;
2253
    std::cout << "Failed tests:" << std::endl;
2254
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
2255
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
2256
        //Print the contents
2257
        std::cout << it->c_str() << std::endl;
2258

    
2259
    }
2260

    
2261
    delete sresult;
2262

    
2263
#ifdef MEM_DEBUG
2264
    SaxonProcessor::getInfo();
2265
#endif
2266

    
2267
    return 0;
2268
}
(27-27/28)