Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 339c45ee

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
        cout << "Result is null ====== FAIL ====== " << endl;
648
        sresult->failure++;
649
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
650
        return;
651
    }
652
    XdmItem *rootItem = rootValue->getHead();
653
    if (rootItem== nullptr) {
654

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

    
681
}
682

    
683
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
684
    cout << "ExampleSimple1 taken from PHP:" << endl;
685

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

    
710
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
711
    cout << "ExampleSimple1Err taken from PHP:" << endl;
712

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

    
726
        delete executable;
727
    }
728

    
729
}
730

    
731

    
732
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
733
    cout << "<b>exampleSimple2:</b><br/>" << endl;
734

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

    
746
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
747
    const char *filename = "output1.xml";
748
    executable->setOutputFile(filename);
749
    executable->applyTemplatesReturningFile("output1.xml");
750

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

    
764
    delete executable;
765

    
766
}
767

    
768
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
769
    cout << "<b>exampleSimple3:</b><br/>" << endl;
770
    proc->clearParameters();
771

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

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

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

    
796
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
797
    sresult->success++;
798
    delete xdmNode;
799
    delete executable;
800
}
801

    
802
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
803
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
804

    
805
    XsltExecutable * executable = proc->compileFromFile(nullptr);
806

    
807

    
808
    if (executable == nullptr) {
809

    
810

    
811
        cout << "Expected failure of test exampleSimple3aError:" << endl;
812
        if(proc->exceptionOccurred()) {
813
            cout<<proc->getErrorMessage()<<endl;
814
        }
815

    
816
        sresult->success++;
817
        proc->exceptionClear();
818
        return;
819
    }
820

    
821
    cout << "Error: executable is not nullptr'" << endl;
822
    sresult->failure++;
823
    sresult->failureList.push_back("exampleSimple3aError");
824
    delete executable;
825
    return;
826

    
827

    
828
}
829

    
830
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
831
    cout << "Test: ExampleParam" << endl;
832

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

    
835

    
836
    if(executable == nullptr) {
837
        cout << "executable is NULL" <<endl;
838
        if(proc->exceptionOccurred()) {
839
            cout << "Error : " << proc->getErrorMessage() <<endl;
840

    
841
        }
842
        sresult->failure++;
843
        sresult->failureList.push_back("exampleParam");
844
        proc->exceptionClear();
845
        return;
846

    
847
    }
848

    
849
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
850

    
851
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
852
    if (xdmvalue != nullptr) {
853

    
854
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
855

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

    
886
    //proc->clearParameters();
887
    //unset($result);
888
    //echo 'again with a no parameter value<br/>';
889

    
890
    executable->setProperty("!indent", "yes");
891
    const char *result2 = executable->applyTemplatesReturningString();
892

    
893

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

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

    
917
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
918

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

    
933
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
934
    delete xdmvalue;
935

    
936

    
937
    const char *result3 = executable->applyTemplatesReturningString();
938
    if (result3 != nullptr) {
939
        cout << "Output =" << result3 << endl;
940

    
941
        sresult->success++;
942
        delete result3;
943

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

    
954
    }
955
    delete xdmValue2;
956
    delete executable;
957

    
958

    
959

    
960
}
961

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

    
968

    
969
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
970
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
971
    if (xdmvalue != NULL) {
972

    
973
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
974
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
975

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

    
998
//proc->clearParameters();
999
//unset($result);
1000
//echo 'again with a no parameter value<br/>';
1001

    
1002
    executable->setProperty("!indent", "no");
1003
    executable->setProperty("!indent", "yes");
1004
    const char *result2 = executable->applyTemplatesReturningString();
1005

    
1006
    executable->clearProperties();
1007

    
1008
    if(result2 != nullptr) {
1009
        cout<<result2<<endl;
1010
        sresult->success++;
1011

    
1012
    }
1013

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

    
1019
    const char *result3 = executable->applyTemplatesReturningString();
1020
    if(result3 != nullptr) {
1021
        cout<<"Output ="<<result3<<endl;
1022

    
1023
        sresult->success++;
1024
    } else {
1025
        cout<<"Error in result ===== FAIL ======="<<endl;
1026
        sresult->failure++;
1027
        sresult->failureList.push_back("exampleParam");
1028
    }
1029

    
1030
}
1031

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

    
1036
    proc->setJustInTimeCompilation(true);
1037

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

    
1061
}
1062

    
1063

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

    
1068
    proc->setJustInTimeCompilation(true);
1069

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

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

    
1097
    proc->setJustInTimeCompilation(false);
1098

    
1099
}
1100

    
1101
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1102
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
1103
    cout << "exampleSimple_xmark test - test q12.xsl:" << endl;
1104

    
1105
    proc->setJustInTimeCompilation(true);
1106

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

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

    
1125
}
1126

    
1127
/*
1128
* Test saving nd loading a Xslt package
1129
*/
1130
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1131

    
1132
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1133
    trans->clearParameters();
1134

    
1135

    
1136
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1137
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1138

    
1139
    if (output== nullptr) {
1140
        printf("result is null \n");
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("testPackage1");
1147

    
1148
    } else {
1149
        printf("%s", output);
1150
        printf("result is OK \n");
1151
        sresult->success++;
1152
    }
1153
    fflush(stdout);
1154
    delete output;
1155
}
1156

    
1157

    
1158
/*
1159
* Test saving and loading a Xslt package
1160
*/
1161
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1162

    
1163
    cout << endl << "Test: testPackage1a" << endl;
1164
    trans->clearParameters();
1165

    
1166
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1167

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

    
1179

    
1180

    
1181
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1182

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

    
1195

    
1196
    }
1197

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

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

    
1220

    
1221
/*
1222
* Test saving and loading a Xslt package
1223
*/
1224
void testPackage2_Error(Xslt30Processor *trans, sResultCount *sresult) {
1225

    
1226
    cout << endl << "Test: testPackage2_Error:" << endl;
1227

    
1228
    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>";
1229

    
1230
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1231

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

    
1241
    }
1242
    trans->exceptionClear();
1243
    sresult->failure++;
1244
    sresult->failureList.push_back("testPackage2_Error");
1245

    
1246
}
1247

    
1248
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1249

    
1250
    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>";
1251
    cout << endl << "Test: testCallFunction:" << endl;
1252
    XsltExecutable * executable = trans->compileFromString(source);
1253

    
1254
    if(executable == nullptr) {
1255
        if(trans->exceptionOccurred()) {
1256
            SaxonApiException * exception = trans->getException();
1257
            if(exception != nullptr) {
1258
                cerr << "Error: " << exception->getMessage() << endl;
1259
                delete exception;
1260
            }
1261

    
1262
        }
1263
        sresult->failure++;
1264
        sresult->failureList.push_back("testCallFunction");
1265
        trans->exceptionClear();
1266
        return;
1267

    
1268

    
1269
    }
1270

    
1271
    XdmValue **valueArray = new XdmValue *[2];
1272

    
1273
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1274
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1275
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1276

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

    
1303
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1304

    
1305

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

    
1317
    executable->setResultAsRawValue(false);
1318
    std::map<std::string, XdmValue *> parameterValues;
1319

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

    
1335
    delete executable;
1336
    delete a1;
1337
    delete a2;
1338
    delete node;
1339
    parameterValues.clear();
1340
}
1341

    
1342
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1343
    cout << endl << "Test: testResolveUri:" << endl;
1344

    
1345
    XsltExecutable * executable = trans->compileFromString(
1346
            "<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>");
1347

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

    
1362

    
1363
    if (value== nullptr) {
1364

    
1365
        sresult->failure++;
1366
        sresult->failureList.push_back("testResolveUri");
1367
    } else {
1368

    
1369
        const char *svalue = value->itemAt(0)->getStringValue();
1370
        cout << "testResolveUri = " << svalue << endl;
1371
        sresult->success++;
1372
        delete value;
1373
    }
1374

    
1375
    delete executable;
1376
}
1377

    
1378
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1379
    cout << endl << "Test: testContextNotRoot" << endl;
1380

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

    
1383
    XsltExecutable *executable = trans->compileFromString(
1384
            "<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>");
1385

    
1386
    if (executable == nullptr) {
1387
        sresult->failure++;
1388
        sresult->failureList.push_back("testContextNotRoot");
1389
        if (trans->exceptionOccurred()) {
1390
            cout << "Error: " << trans->getErrorMessage() << endl;
1391
        }
1392
        return;
1393
    }
1394

    
1395
    executable->setGlobalContextItem(node);
1396
    if (node->getChildCount() > 0) {
1397
        XdmNode **eNodeL1 = node->getChildren();
1398
        if (eNodeL1 != nullptr) {
1399
            XdmNode ** eNodeChildren = eNodeL1[0]->getChildren();
1400
            if (eNodeChildren != nullptr) {
1401
                XdmNode *eNode = eNodeChildren[0];
1402
                cout << "Node content = " << eNode->toString() << endl;
1403
                executable->setInitialMatchSelection(eNode);
1404
                const char *result = executable->applyTemplatesReturningString();
1405

    
1406
                if (result == nullptr) {
1407

    
1408
                    cout << "testContextNotRoot ======= FAIL ======" << endl;
1409
                    if (executable->exceptionOccurred()) {
1410
                        SaxonApiException *exception = executable->getException();
1411
                        cerr << "Error: " << exception->getMessage() << endl;
1412
                        delete exception;
1413
                    }
1414
                    sresult->failure++;
1415
                    sresult->failureList.push_back("testContextNotRoot");
1416

    
1417

    
1418
                } else {
1419

    
1420
                    cout << "testContextNotRoot = " << result << endl;
1421
                    sresult->success++;
1422
                    delete result;
1423

    
1424
                }
1425

    
1426
            }
1427
        }
1428
    } else {
1429
        sresult->failure++;
1430
        sresult->failureList.push_back("testContextNotRoot");
1431
    }
1432

    
1433
    delete executable;
1434
    delete node;
1435
}
1436

    
1437

    
1438
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1439
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1440

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

    
1443
    if(node == nullptr) {
1444
        if(proc->exceptionOccurred()) {
1445
            const char * message = proc->getErrorMessage();
1446
            if(message != nullptr) {
1447
                cerr << "Error: " << message << endl;
1448
            }
1449
            proc->exceptionClear();
1450
        }
1451
        sresult->failure++;
1452
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1453
        return;
1454
    }
1455

    
1456
    XsltExecutable * executable = trans->compileFromString(
1457
            "<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>");
1458

    
1459
    if(executable == nullptr) {
1460
        sresult->failure++;
1461
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1462
        if(trans->exceptionOccurred()) {
1463
            cout << "Error: "<< trans->getErrorMessage() << endl;
1464
        }
1465
        delete node;
1466
        return;
1467
    }
1468

    
1469
    executable->setGlobalContextItem(node);
1470
    const char *result = executable->callTemplateReturningString("main");
1471

    
1472
    if (result== nullptr) {
1473

    
1474
        cout << "testContextNotRootNameTemplate ======= FAIL ======" << endl;
1475
        if(executable->exceptionOccurred()) {
1476
            SaxonApiException * exception = executable->getException();
1477
            cerr<<"Error: " << exception->getMessage() <<endl;
1478
            delete exception;
1479
        }
1480
        sresult->failure++;
1481
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1482
    } else {
1483

    
1484
        cout << "testContextNotRoot = " << result << endl;
1485
        sresult->success++;
1486
        delete result;
1487
    }
1488
    delete node;
1489
    delete executable;
1490

    
1491
}
1492

    
1493

    
1494
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1495
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1496

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

    
1499
    if(node == nullptr) {
1500
        if(proc->exceptionOccurred()) {
1501
            const char * message = proc->getErrorMessage();
1502
            if(message != nullptr) {
1503
                cerr << "Error: " << message << endl;
1504
            }
1505
            proc->exceptionClear();
1506
        }
1507
        sresult->failure++;
1508
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1509
        return;
1510
    }
1511

    
1512
    XsltExecutable * executable = trans->compileFromString(
1513
            "<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>");
1514

    
1515
    if(executable == nullptr) {
1516
        sresult->failure++;
1517
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1518
        if(trans->exceptionOccurred()) {
1519
            cout << "Error: "<< trans->getErrorMessage() << endl;
1520
        }
1521
        delete node;
1522
        return;
1523
    }
1524

    
1525
    executable->setGlobalContextItem(node);
1526
    XdmValue *result = executable->callTemplateReturningValue("main");
1527

    
1528
    if (result == nullptr) {
1529

    
1530
        cout << "testCallFunction ======= FAIL ======" << endl;
1531
        if(executable->exceptionOccurred()) {
1532
            SaxonApiException * exception = executable->getException();
1533
            cerr<<"Error: " << exception->getMessage() <<endl;
1534
            delete exception;
1535
        }
1536
        sresult->failure++;
1537
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1538
    } else {
1539

    
1540
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1541
        sresult->success++;
1542
        delete result;
1543
        result = nullptr;
1544
    }
1545

    
1546
    delete node;
1547
    delete executable;
1548

    
1549

    
1550
}
1551

    
1552
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1553

    
1554
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1555
                                                            1);
1556
    if (fi== nullptr) {
1557
        sresult->failure++;
1558
        sresult->failureList.push_back("testCallSystemFunction");
1559
        return;
1560
    }
1561

    
1562
    XdmValue ** xdmValue = new XdmValue*[1];
1563
    xdmValue[0] = new XdmValue();
1564
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1565
    XdmValue *result = fi->call(xdmValue, 1);
1566

    
1567
    std::cerr << "Result = " << result->toString() << endl;
1568
    if(result->size() == 3) {
1569

    
1570
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1571
        sresult->success++;
1572

    
1573
    } else {
1574
        sresult->failure++;
1575
        sresult->failureList.push_back("testCallSystemFunction");
1576

    
1577
    }
1578

    
1579
}
1580

    
1581

    
1582
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1583
    cout << endl << "Test: testPipeline" << endl;
1584

    
1585
    Xslt30Processor * trans = proc->newXslt30Processor();
1586
    if(trans == nullptr) {
1587
        cout << "Error: Xslt30Processor is null - maybe unclean state of JNI" << endl;
1588
        sresult->failure++;
1589
        sresult->failureList.push_back("testPipeline");
1590
        return;
1591
    }
1592
    XsltExecutable * stage1 = trans->compileFromString(
1593
            "<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>");
1594

    
1595
    if(stage1 == nullptr) {
1596
        sresult->failure++;
1597
        sresult->failureList.push_back("testPipeline");
1598
        if(trans->exceptionOccurred()) {
1599
            const char * message = trans->getErrorMessage();
1600
            cout << "stage 1 Error: " << message << endl;
1601

    
1602
        }
1603
        cout << "Stage 1 Error - exit method " << endl;
1604
        delete trans;
1605
        return;
1606
    }
1607

    
1608
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1609

    
1610
    XsltExecutable *stage2 = trans->compileFromString(
1611
            "<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>");
1612

    
1613
    if(stage2 == nullptr) {
1614
        sresult->failure++;
1615
        sresult->failureList.push_back("testPipeline");
1616
        if(trans->exceptionOccurred()) {
1617
            const char * message = trans->getErrorMessage();
1618
            cout << "stage 2 Error: " << message << endl;
1619

    
1620
        }
1621
        cout << "Stage 2 Error - exit method " << endl;
1622
        delete stage1;
1623
        delete trans;
1624
        delete inn;
1625
        return;
1626
    }
1627

    
1628

    
1629
    XsltExecutable *stage3 = trans->compileFromString(
1630
            "<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>");
1631

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

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

    
1650
    XsltExecutable *stage4 = trans->compileFromString(
1651
            "<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>");
1652

    
1653
    if(stage4 == nullptr) {
1654
        sresult->failure++;
1655
        sresult->failureList.push_back("testPipeline");
1656
        if(trans->exceptionOccurred()) {
1657
            const char * message = trans->getErrorMessage();
1658
            cout << "stage 4 Error: " << message << endl;
1659

    
1660
        } else {
1661
            cout << "Stage 4 Error: testPipeline failed with exception" << endl;
1662
        }
1663
        cout << "Stage 4 Error - exit method " << endl;
1664
        delete stage1;
1665
        delete stage2;
1666
        delete stage3;
1667
        delete trans;
1668
        delete inn;
1669
        return;
1670
    }
1671

    
1672
    XsltExecutable *stage5 = trans->compileFromString(
1673
            "<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>");
1674

    
1675
    if(stage5 == nullptr) {
1676
        sresult->failure++;
1677
        sresult->failureList.push_back("testPipeline");
1678
        if(trans->exceptionOccurred()) {
1679
            const char * message = trans->getErrorMessage();
1680
            cout << "stage 5 Error: " << message << endl;
1681

    
1682
        } else {
1683
            cout << "Stage 5 Error: testPipeline failed with exception" << endl;
1684
        }
1685
        cout << "Stage 5     Error - exit method " << endl;
1686
        delete stage1;
1687
        delete stage2;
1688
        delete stage3;
1689
        delete stage4;
1690
        delete trans;
1691
        delete inn;
1692
        return;
1693
    }
1694

    
1695
    stage1->setProperty("!indent", "no");
1696
    stage1->setInitialMatchSelection(inn);
1697

    
1698
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1699
    if(d1 == nullptr) {
1700
        if (stage1->exceptionOccurred()) {
1701
            sresult->failure++;
1702
            sresult->failureList.push_back("testPipeline");
1703
            if (stage1->exceptionOccurred()) {
1704
                SaxonApiException *exception = stage1->getException();
1705
                if (exception != nullptr) {
1706
                    cerr << "Error: " << exception->getMessage() << endl;
1707
                    delete exception;
1708
                    exception = nullptr;
1709
                }
1710
            }
1711
            cout << "Stage d1 Error - exit method " << endl;
1712
            delete stage1;
1713
            delete stage2;
1714
            delete stage3;
1715
            delete stage4;
1716
            delete stage5;
1717
            delete trans;
1718
            delete inn;
1719
            return;
1720
        }
1721
    }
1722

    
1723
    XdmItem *d11 = d1->getHead();
1724
    if (d11== nullptr) {
1725

    
1726
        cout << "d11 is NULL\n" << endl;
1727
        sresult->failure++;
1728
        sresult->failureList.push_back("testPipeline-1");
1729
        delete stage1;
1730
        delete stage2;
1731
        delete stage3;
1732
        delete stage4;
1733
        delete stage5;
1734
        delete d1;
1735
        delete inn;
1736
        delete trans;
1737
        return;
1738
    }
1739
    const char *data = d11->toString();
1740

    
1741
    if (data != NULL) {
1742
        cout << "d1 result=" << data << endl;
1743
    } else {
1744

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

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

    
1869
    delete stage2;
1870
    stage2 = nullptr;
1871

    
1872

    
1873
    delete stage3;
1874
    stage3 = nullptr;
1875

    
1876
    delete stage4;
1877
    stage4 = nullptr;
1878

    
1879
    delete stage5;
1880
    stage5 = nullptr;
1881

    
1882
    delete trans;
1883
    trans = nullptr;
1884

    
1885
    delete sw;
1886
    sw = nullptr;
1887

    
1888
    delete d4;
1889
    d4 = nullptr;
1890

    
1891
    delete d3;
1892
    d3 = nullptr;
1893

    
1894
    delete d2;
1895
    d2 = nullptr;
1896

    
1897
    delete d1;
1898
    d1 = nullptr;
1899

    
1900
    delete inn;
1901
    inn = nullptr;
1902

    
1903
}
1904

    
1905

    
1906
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1907

    
1908

    
1909
    cout << endl << "Test: testCatalog" << endl;
1910
    bool trace = false;
1911
    proc->setcwd(cwd);
1912
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1913

    
1914
    Xslt30Processor * trans = proc->newXslt30Processor();
1915

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

    
1918
    if(executable == nullptr) {
1919

    
1920
        if (trans->exceptionOccurred()) {
1921

    
1922
            if(trans->getErrorMessage() != nullptr) {
1923
                const char *message = trans->getErrorMessage();
1924
                cerr << "exception=" << message << endl;
1925
            } else {
1926
                const char *message = proc->getErrorMessage();
1927
                if(message != nullptr) {
1928
                    cerr << "exception-proc=" << message << endl;
1929
                }
1930
            }
1931
        }
1932
        sresult->failure++;
1933
        sresult->failureList.push_back("testCatalog");
1934
        trans->exceptionClear();
1935
        delete trans;
1936
        trans= nullptr;
1937
        return;
1938
    }
1939

    
1940
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1941

    
1942
    const char *result = executable->applyTemplatesReturningString();
1943

    
1944
    if(result != NULL) {
1945
        std::cerr << "testCatalog result= " << result << std::endl;
1946
        delete result;
1947
        sresult->success++; // TODO - check the results more carefully
1948
    } else {
1949
        sresult->failure++;
1950
        sresult->failureList.push_back("testCatalog");
1951
    }
1952

    
1953

    
1954
    delete executable;
1955
    executable = nullptr;
1956
    delete trans;
1957
    trans = nullptr;
1958

    
1959
}
1960

    
1961
static int NUM_THREADS = 10;
1962

    
1963
void RunThread(XsltExecutable * executable, int tid, const std::string filename) {
1964
    JavaVMAttachArgs att_arg;
1965
    att_arg.version = JNI_VERSION_1_2;
1966
    att_arg.name = NULL;
1967
    att_arg.group = NULL;
1968

    
1969
   // SaxonProcessor::sxn_environ->jvm->AttachCurrentThread((void**)&SaxonProcessor::sxn_environ->env, &att_arg);
1970
    cerr<<endl<<"RunThread cp0,  THEAD ID="<<tid<<endl;
1971
    fflush(stderr);
1972
    fflush(stdout);
1973

    
1974
    if(executable != nullptr) {
1975
       executable->setInitialMatchSelectionAsFile(filename.c_str());
1976

    
1977
        cerr << "RunThread cp1" << endl;
1978

    
1979
       const char *result = nullptr;
1980
        result = executable->applyTemplatesReturningString();
1981
        if (result != nullptr) {
1982
            cout << " Result from THREAD ID: " << tid << ", " << result << endl;
1983
            delete result;
1984
        } else {
1985
            cerr << " ===== Failed in THREAD ID: " << tid << endl;
1986
           /* if(executable->exceptionOccurred()) {
1987
                SaxonApiException *exception = executable->getException();
1988

1989
                if (exception != nullptr) {
1990
                    const char *message = exception->getMessage();
1991
                    if (message != nullptr) {
1992
                        cerr << "Error = " << message << endl;
1993
                    }
1994
                    delete exception;
1995
                }
1996
            } else {
1997
            cerr << "No exception found - result is nullptr" << endl;
1998
            }*/
1999

    
2000
        }
2001
    delete executable;
2002
    } else {
2003
        cerr << "XsltExecutable is nullptr" << endl;
2004
    }
2005

    
2006
   // (   SaxonProcessor::sxn_environ->jvm)->DetachCurrentThread();
2007

    
2008
}
2009

    
2010
void testThreads (SaxonProcessor * processor, Xslt30Processor * trans, sResultCount *sresult) {
2011

    
2012
    std::vector<std::string> s = {
2013
            "../data/xmark100k.xml",
2014
            "../data/xmark1.xml",
2015
            "../data/xmark4.xml",
2016
            "../data/xmark10.xml"
2017
    };
2018

    
2019
    std::vector<std::thread> threads;
2020

    
2021
    for (int i = 0; i < s.size(); i++) {
2022

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

    
2026
    }
2027
    cerr<<"testThreads cp0"<<endl;
2028
    for (auto &th : threads) {
2029
        th.join();
2030
        cerr<<"testThreads cp1 - loop"<<endl;
2031
    }
2032
}
2033

    
2034

    
2035

    
2036
int main(int argc, char* argv[]) {
2037

    
2038

    
2039
    const char * cwd = nullptr;
2040
    if(argc > 0) {
2041
        cwd = argv[1];
2042
    }
2043

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

    
2058
        std::cerr << "Processor is Schema Aware" << std::endl;
2059
    } else {
2060
        std::cerr << "Processor is not Schema Aware" << std::endl;
2061
    }
2062

    
2063
    sResultCount *sresult = new sResultCount();
2064
    Xslt30Processor *trans = processor->newXslt30Processor();
2065

    
2066
    if( trans == nullptr) {
2067
        cout<< "Error creating Xslt30Processor"<<endl;
2068
        if(processor->exceptionOccurred()) {
2069
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
2070

    
2071
        }
2072
        return -1;
2073
    }
2074
    //testValidation(trans,sresult);
2075

    
2076
    testInitialTemplate(processor, trans, sresult);
2077

    
2078
    cout<<endl<<"============================================================="<<endl<<endl;
2079

    
2080
    exampleSimple1Err(trans, sresult);
2081

    
2082
    cout<<endl<<"============================================================="<<endl<<endl;
2083

    
2084
    exampleSimple1(trans, sresult);
2085

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

    
2088
    exampleSimple_xmark(trans, sresult);
2089

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

    
2092
    exampleSimple2(trans, sresult);
2093

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

    
2096
    exampleSimple3(processor, trans, sresult);
2097

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

    
2100
    exampleSimple3aError(processor, trans, sresult);
2101

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

    
2104
    testApplyTemplatesString1(trans, sresult);
2105

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

    
2108
    testApplyTemplatesString2(processor, trans, sresult);
2109

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

    
2112

    
2113
    testApplyTemplates2a_Error(processor, trans, sresult);
2114

    
2115
    cout<<endl<<"============================================================="<<endl<<endl;
2116

    
2117
    testTransformToString4(processor, trans, sresult);
2118

    
2119
    cout<<endl<<"============================================================="<<endl<<endl;
2120

    
2121
    testCatalog(cwd, processor,  sresult);
2122

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

    
2125
    testTransformToString2b(processor, trans, sresult);
2126

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

    
2129
    testTransformToString3(processor, trans, sresult);
2130

    
2131
    cout<<endl<<"============================================================="<<endl<<endl;
2132
        
2133
    testTransformFromstring(processor, trans, sresult);
2134

    
2135

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

    
2138
    testTransformFromstring2Err(processor, trans, sresult);
2139

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

    
2142
    testTrackingOfValueReference(processor, trans, sresult);
2143

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

    
2146
    testTrackingOfValueReferenceError(processor, trans, sresult);
2147

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

    
2150
    testPackage1(trans, sresult);
2151

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

    
2154
    testPackage1a(trans, sresult);
2155

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

    
2158
    testPackage2_Error(trans, sresult);
2159

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

    
2162
    testXdmNodeOutput(trans, sresult);
2163

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

    
2166
    exampleParam(processor, trans, sresult);
2167

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

    
2170
    xmarkTest1(trans, sresult);
2171

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

    
2174
    xmarkTest2(trans, sresult);
2175

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

    
2178
    testCallFunction(processor, trans, sresult);
2179

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

    
2182
    testResolveUri(processor, trans, sresult);
2183

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

    
2186
    testContextNotRoot(processor, trans, sresult);
2187

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

    
2190
    testContextNotRootNamedTemplate(processor, trans, sresult);
2191

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

    
2194
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
2195

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

    
2198
   testPipeline(processor, sresult);
2199

    
2200

    
2201
    cout<<endl<<"============================================================="<<endl<<endl;
2202

    
2203
    fflush(stdout);
2204

    
2205

    
2206
    //Available in PE and EE
2207
    //testTransformToStringExtensionFunc(processor, trans);
2208

    
2209

    
2210

    
2211
    //processor->release();
2212
    //return 0;
2213

    
2214
    fflush(stdout);
2215

    
2216

    
2217
    //set to the current working directory
2218
    if(cwd != nullptr) {
2219
        processor->setcwd(cwd);
2220
    } else {
2221
        processor->setcwd(buff);
2222
    }
2223
    Xslt30Processor *trans2 = processor->newXslt30Processor();
2224
    cerr<<" after trans2 cp0"<<endl;
2225
    testApplyTemplatesString1(trans2, sresult);
2226
    cout<<endl<<"============================================================="<<endl<<endl;
2227

    
2228

    
2229

    
2230
    //testThreads (processor, trans2, sresult);
2231

    
2232

    
2233
    delete trans;
2234
    delete trans2;
2235
    delete processor;
2236
    processor->release();
2237

    
2238

    
2239
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
2240

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

    
2244
    std::list<std::string>::iterator it;
2245
    std::cout << "Failed tests:" << std::endl;
2246
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
2247
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
2248
        //Print the contents
2249
        std::cout << it->c_str() << std::endl;
2250

    
2251
    }
2252

    
2253
    delete sresult;
2254

    
2255
#ifdef MEM_DEBUG
2256
    SaxonProcessor::getInfo();
2257
#endif
2258

    
2259
    return 0;
2260
}
(27-27/28)