Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 4ee4355d

1
#include <sstream>
2
#include <stdio.h>
3
#include "../../Saxon.C.API/SaxonProcessor.h"
4
#include "../../Saxon.C.API/XdmValue.h"
5
#include "../../Saxon.C.API/XdmItem.h"
6
#include "../../Saxon.C.API/XdmNode.h"
7
#include "../../Saxon.C.API/XdmFunctionItem.h"
8
#include "cppExtensionFunction.h"
9
#include "CppTestUtils.h"
10
#include <string>
11

    
12
// TODO: write test case for checking parameters which are null
13

    
14

    
15
using namespace std;
16
char fname[] = "_nativeCall";
17
char funcParameters[] = "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;";
18

    
19
JNINativeMethod cppMethods[] =
20
        {
21
                {
22
                        fname,
23
                        funcParameters,
24
                        (void *) &cppExtensionFunction::cppNativeCall
25
                }
26
        };
27

    
28

    
29
/*
30
* Test transform to String. Source and stylesheet supplied as arguments
31
*/
32
void testApplyTemplatesString1(Xslt30Processor *trans, sResultCount *sresult) {
33

    
34
    cout << "Test: testApplyTemplatesString1:" << endl;
35
//    trans->setupXslMessage(false);
36

    
37
    XsltExecutable *executable = trans->compileFromFile("test.xsl");
38

    
39
    if(executable== nullptr) {
40
        sresult->failure++;
41
        sresult->failureList.push_back("testApplyTemplatesString1");
42
        cerr << "testApplyTemplatesString1 NULL found" << endl;
43
        if (trans->exceptionOccurred()) {
44
            cerr <<"testApplyTemplatesString1 error: "<< trans->getErrorMessage() << endl;
45
        }
46
        return;
47
    }
48
    executable->setInitialMatchSelectionAsFile("cat.xml");
49

    
50
    const char *output = executable->applyTemplatesReturningString();
51
    if (output == nullptr) {
52
        printf("result is null ====== FAIL ====== \n");
53
        sresult->failure++;
54
        fflush(stdout);
55
        sresult->failureList.push_back("testApplyTemplatesString1-0");
56
    } else if (string(output).find(string("<out>text2</out>")) != std::string::npos) {
57
        printf("%s", output);
58
        printf("result is OK \n");
59
        sresult->success++;
60
        delete output;
61
    } else {
62
        printf("result is null ====== FAIL ====== \n");
63
        sresult->failure++;
64
        sresult->failureList.push_back("testApplyTemplatesString1-1");
65
            std::cout<<"output="<<output<<std::endl;
66
        delete output;
67
    }
68
    fflush(stdout);
69
    delete executable;
70

    
71
}
72

    
73
/*
74
* Test transform to String. Source and stylesheet supplied as arguments
75
*/
76
void testTransformToStringExtensionFunc(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
77

    
78
    cout << endl << "Test: TransformToStringExtensionFunc:" << endl;
79
    bool nativeFound = processor->registerNativeMethods(SaxonProcessor::sxn_environ->env,
80
                                                        "com/saxonica/functions/extfn/cpp/NativeCall",
81
                                                        cppMethods, sizeof(cppMethods) / sizeof(cppMethods[0]));
82

    
83
    XsltExecutable * executable = trans->compileFromFile("testExtension.xsl");
84

    
85
    if(executable== nullptr) {
86
        sresult->failure++;
87
        sresult->failureList.push_back("TransformToStringExtensionFunc");
88
        cerr << "TransformToStringExtensionFunc NULL found" << endl;
89
        if (trans->exceptionOccurred()) {
90
            cerr <<"TransformToStringExtensionFunc error: "<< trans->getErrorMessage() << endl;
91
        }
92
        return;
93
    }
94

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

    
97
    if (nativeFound) {
98
        const char *output = executable->transformFileToString("cat.xml");
99

    
100
        if (output == nullptr) {
101
            SaxonProcessor::sxn_environ->env->ExceptionDescribe();
102
            printf("result is null ====== FAIL ======  \n");
103
            sresult->failure++;
104
            sresult->failureList.push_back("testTransformToStringExnteionFunc");
105
        } else {
106
            sresult->success++;
107
            printf("result is OK \n");
108
        }
109
        fflush(stdout);
110
        delete output;
111
    } else {
112
        printf("native Class not foun ====== FAIL ====== ");
113
        sresult->failure++;
114
        sresult->failureList.push_back("testTransformToStringExtensionFunc");
115
    }
116
    delete executable;
117
}
118

    
119

    
120
/*
121
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
122
*/
123
void testApplyTemplatesString2(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
124

    
125
    cout << "Test: testApplyTemplatesString2:" << endl;
126

    
127

    
128
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
129

    
130
    if (input== nullptr) {
131
        cout << "Source document is null." << endl;
132
        if(processor->exceptionOccurred()) {
133
            cerr<<processor->getErrorMessage()<<endl;
134
        }
135
        sresult->failure++;
136
        sresult->failureList.push_back("testApplyTemplatesString2");
137
        return;
138

    
139
    }
140
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
141
    if(executable == nullptr) {
142
        sresult->failure++;
143
        sresult->failureList.push_back("testApplyTemplatesString2");
144
        if (trans->exceptionOccurred()) {
145
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
146
        }
147
        delete input;
148
        return;
149
    }
150

    
151
    executable->setInitialMatchSelection((XdmValue *) input);
152

    
153
    const char *output = executable->applyTemplatesReturningString();
154
    if (output== nullptr) {
155
        printf("result is null ====== FAIL ======  \n");
156
        sresult->failureList.push_back("testApplyTemplatesString2");
157
        sresult->failure++;
158
    } else {
159
        sresult->success++;
160
        printf("%s", output);
161
        printf("result is OK \n");
162
        delete output;
163
    }
164
    fflush(stdout);
165
    delete input;
166
    delete executable;
167
}
168

    
169
/*
170
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
171
Should be error. Stylesheet file does not exist
172
*/
173
void testApplyTemplates2a_Error(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
174

    
175
    cout << "Test: TransformToString2a_Error:" << endl;
176

    
177
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
178

    
179
    if (input == nullptr) {
180
        cout << "Source document is null. ====== FAIL ======" << endl;
181
        if(processor->exceptionOccurred()) {
182
            cerr<<processor->getErrorMessage()<<endl;
183
        }
184
        sresult->failure++;
185
        sresult->failureList.push_back("testApplyTemplates2a_Error");
186
        trans->exceptionClear();
187
        return;
188
    }
189
    XsltExecutable * executable = trans->compileFromFile("test-error.xsl");
190

    
191
    if(executable== nullptr) {
192
        printf("Expected result is null \n");
193
        sresult->success++;
194

    
195
        if (trans->exceptionOccurred()) {
196
            cerr <<"testApplyTemplates2a_Error error: "<< trans->getErrorMessage() << endl;
197
        }
198
        delete input;
199
        trans->exceptionClear();
200
        return;
201
    }
202

    
203
    sresult->failure++;
204
    sresult->failureList.push_back("testApplyTemplates2a_Error");
205
    fflush(stdout);
206

    
207
    delete executable;
208

    
209
}
210

    
211
/*
212
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
213
Should be error. Source file does not exist
214
*/
215
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
216

    
217
    cout << endl << "Test: TransformToString2b:" << endl;
218

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

    
221
    if(result== nullptr) {
222
        if(trans->exceptionOccurred()) {
223
            cerr<<"Expected failure = "<<trans->getErrorMessage()<<endl;
224
        }
225
        sresult->success++;
226
        trans->exceptionClear();
227
        return;
228
    }else {
229
        delete result;
230
        sresult->failure++;
231
        sresult->failureList.push_back("testTransformToString2b");
232
        cerr << "testTransformToString2b NULL found" << endl;
233
        if (trans->exceptionOccurred()) {
234
            cerr <<"Error: "<< trans->getErrorMessage() << endl;
235
        }
236
        trans->exceptionClear();
237
        return;
238
    }
239

    
240

    
241
}
242

    
243

    
244
/*
245
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
246
and integer parmater created and supplied
247
*/
248
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
249

    
250
    cout << endl << "Test: TransformToString3" << endl;
251

    
252
    XdmNode *inputi = processor->parseXmlFromString(
253
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
254

    
255
    if (inputi== nullptr) {
256
        cout << "Source document inputi is null." << endl;
257
        if (trans->exceptionOccurred()) {
258
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
259
        }
260
        sresult->failure++;
261
        sresult->failureList.push_back("testTransformToString3");
262

    
263
        return;
264
    }
265

    
266
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
267

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

    
279
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
280

    
281
    if(executable== nullptr) {
282
        sresult->failure++;
283
        sresult->failureList.push_back("testTransformToString3");
284
        cerr << "testTransformToString3 NULL found" << endl;
285
        if (trans->exceptionOccurred()) {
286
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
287
        }
288
        delete value1;
289
        return;
290
    }
291

    
292

    
293
    executable->setParameter("numParam", (XdmValue *) value1);
294

    
295
    executable->setInitialMatchSelection((XdmNode *) inputi);
296
    const char *output = executable->applyTemplatesReturningString();
297
    if (output == nullptr) {
298
        printf("result is null ====== FAIL ====== \n");
299
        sresult->failureList.push_back("testTransformToString3");
300
    } else {
301
        printf("%s", output);
302
        printf("result is OK \n");
303
        delete output;
304
    }
305
    fflush(stdout);
306

    
307
    delete value1;
308
    delete inputi;
309
    delete executable;
310

    
311
}
312

    
313
/*
314
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
315
and integer parmater created and supplied
316
*/
317
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
318

    
319
    cout <<  "Test: testTransformToString4:" << endl;
320

    
321
    XdmNode *input = processor->parseXmlFromString(
322
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
323

    
324
    if (input== nullptr) {
325
        cout << "Source document is null. ====== FAIL ====== " << endl;
326
        sresult->failure++;
327
        sresult->failureList.push_back("testTransformToString4");
328
    }
329

    
330

    
331
    XdmValue *values = new XdmValue();
332
    XdmAtomicValue * a1 = processor->makeIntegerValue(10);
333
    XdmAtomicValue * a2 = processor->makeIntegerValue(5);
334
    XdmAtomicValue * a3 = processor->makeIntegerValue(6);
335
    XdmAtomicValue * a4 = processor->makeIntegerValue(7);
336
    values->addXdmItem(a1);
337
    values->addXdmItem(a2);
338
    values->addXdmItem(a3);
339
    values->addXdmItem(a4);
340

    
341

    
342
    XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
343

    
344
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
345

    
346
    if(executable== nullptr) {
347
        sresult->failure++;
348
        sresult->failureList.push_back("testTransformToString4");
349
        cerr << "testTransformToString4 NULL found" << endl;
350
        if (trans->exceptionOccurred()) {
351
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
352
        }
353
        delete values;
354
        return;
355
    }
356

    
357
    executable->setParameter("values", (XdmValue *) values);
358
    executable->setInitialMatchSelection((XdmNode *) input);
359
    const char *output = executable->applyTemplatesReturningString();
360
    if (output== nullptr) {
361
        printf("result is null \n");
362
        sresult->failure++;
363
        sresult->failureList.push_back("testTransformToString4");
364
    } else {
365
        printf("%s", output);
366
        printf("result is OK \n");
367
        delete output;
368
    }
369
    fflush(stdout);
370
    delete sheet;
371
    delete executable;
372
    delete input;
373
    delete values;
374

    
375
}
376

    
377
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
378
    cout << endl << "Test: testTransfromFromstring: "<< endl;
379

    
380
    if(processor == nullptr) {
381
        cout<<" processor is null"<<endl;
382
        return;
383

    
384
    }
385
    XdmNode *input = processor->parseXmlFromString(
386
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
387

    
388
    if(input == nullptr) {
389
        sresult->failure++;
390
        sresult->failureList.push_back("testTransformFromstring");
391
        cerr << "testTransformFromstring NULL found" << endl;
392
        if (trans->exceptionOccurred()) {
393
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
394
        }
395
        return;
396
    }
397

    
398
    XsltExecutable * executable = trans->compileFromString(
399
            "<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>");
400

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

    
412
    const char *output = executable->transformToString((XdmNode *) input);
413
    if (output== nullptr) {
414
        printf("result is null ====== FAIL ====== \n");
415
        sresult->failure++;
416
        sresult->failureList.push_back("testTransformFromString");
417
        if (trans->exceptionOccurred()) {
418
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
419
        }
420
    } else {
421
        printf("%s", output);
422
        printf("result is OK \n");
423
        sresult->success++;
424
        delete output;
425
    }
426
    fflush(stdout);
427
    delete input;
428
    delete executable;
429

    
430

    
431
}
432

    
433
//Test case has error in the stylesheet
434
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
435
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
436

    
437
    XsltExecutable * executable = trans->compileFromString(
438
            "<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>");
439

    
440
    if(executable== nullptr) {
441
        sresult->success++;
442

    
443
        if (trans->exceptionOccurred()) {
444
            cerr <<"Error expected: "<< trans->getErrorMessage() << endl;
445
        }
446
        return;
447
    }
448

    
449

    
450
    sresult->failure++;
451
    sresult->failureList.push_back("testTransfromFromstring2-Error");
452
    delete executable;
453

    
454
}
455

    
456
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
457

    
458
    cout << endl << "Test: TrackingOfValueReference:" << endl;
459
    ostringstream test;
460
    ostringstream valStr;
461
    ostringstream name;
462
    for (int i = 0; i < 10; i++) {
463
        test << "v" << i;
464
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
465
               << "</value></out>";
466
        name << "value" << i;
467

    
468
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
469
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
470
        trans->setParameter(name.str().c_str(), values);
471
        test.str("");
472
        valStr.str("");
473
        name.str("");
474

    
475
        if(values == nullptr) {
476
            cerr<<"TrackingOfValueReference failed to create XdmNode object" <<endl;
477
            sresult->failure++;
478
            sresult->failureList.push_back("TrackingOfValueReference");
479

    
480
        }
481
    }
482

    
483
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
484
    if (parMap.size() > 0) {
485
        //cout << "Parameter size: " << parMap.size() << endl;
486
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
487
        ostringstream name1;
488
        for (int i = 0; i < 10; i++) {
489
            name1 << "sparam:value" << i;
490
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
491
            XdmValue *valuei = parMap[name1.str()];
492
            if (valuei != nullptr) {
493
                cout << name1.str();
494
                if (valuei->itemAt(0) != nullptr)
495
                    cout << "= " << valuei->itemAt(0)->getStringValue();
496
                cout << endl;
497

    
498
            } else {
499
                sresult->failure++;
500
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
501
                sresult->failureList.push_back("testTrackingOfValueReference");
502
                return;
503
            }
504
            name1.str("");
505
        }
506
    }
507
    if(parMap.size() > 0 ) {
508
        cerr<<"Deleting map"<<endl;
509
        trans->clearParameters(true);
510
    }
511
    sresult->success++;
512
}
513

    
514
/*Test case should be error.*/
515
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
516
    trans->clearParameters();
517

    
518
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
519
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
520
    ostringstream test;
521
    ostringstream valStr;
522
    ostringstream name;
523
    for (int i = 0; i < 2; i++) {
524
        test << "v" << i;
525
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
526
               << "<value></out>";
527
        name << "value" << i;
528

    
529
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
530
        if(values == nullptr) {
531
            cerr<<"values NULL ====== FAIL ======="<<endl;
532
        } else {
533
            trans->setParameter(name.str().c_str(), values);
534
        }
535
        test.str("");
536
        valStr.str("");
537
        name.str("");
538
    }
539
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
540
    cout << "Parameter Map size: " << parMap.size() << endl;
541

    
542
    ostringstream name1;
543
    bool errorFound = false;
544
    for (int i = 0; i < 2; i++) {
545
        name1 << "sparam:value" << i;
546
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
547
        try {
548
            XdmValue *valuei = parMap.at(name1.str());
549
            if (valuei != NULL) {
550
                cout << name1.str();
551
                if (valuei->itemAt(0) != NULL)
552
                    cout << "= " << valuei->itemAt(0)->getStringValue();
553
                cout << endl;
554
            }
555
        } catch (const std::out_of_range &oor) {
556
            cout << "Out of range exception occurred. Exception no. " << endl;
557
            if (!errorFound) {
558
                sresult->success++;
559
                errorFound = true;
560

    
561
                return;
562
            }
563
        }
564
        name1.str("");
565
    }
566
    sresult->failure++;
567
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
568

    
569
}
570

    
571
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
572
    trans->clearParameters();
573

    
574

    
575

    
576
    XsltExecutable * executable = trans->compileFromString(
577
            "<?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>");
578

    
579
    const char *rootValue = executable->callTemplateReturningString("main");
580

    
581

    
582
    if (rootValue== nullptr) {
583
        std::cout << "NULL found" << std::endl;
584
        sresult->failure++;
585
        sresult->failureList.push_back("testValidation");
586
        return;
587

    
588
    } else {
589
        std::cout << "Result=" << rootValue << endl;
590
        sresult->success++;
591
    }
592
    delete executable;
593
}
594

    
595

    
596
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
597

    
598
    std::cout << "testXdmNodeOutput" << std::endl;
599
    XsltExecutable * executable = trans->compileFromString(
600
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
601
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
602
    if (rootValue== nullptr) {
603
        cout << "Result is null ====== FAIL ====== " << endl;
604
        sresult->failure++;
605
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
606
        return;
607
    }
608
    XdmItem *rootItem = rootValue->getHead();
609
    if (rootItem== nullptr) {
610

    
611
        cout << "Result is null ====== FAIL ====== " << endl;
612
        sresult->failure++;
613
        sresult->failureList.push_back("testXdmNodeOutput-0");
614
        delete rootValue;
615
        delete executable;
616
        return;
617
    }
618
    XdmNode *root = (XdmNode *) rootItem;
619
    if (root->getNodeKind() == DOCUMENT) {
620
        cout << "Result is a Document" << endl;
621
    } else {
622
        cout << "Node is of kind:" << root->getNodeKind() << endl;
623
    }
624
    const char *result = executable->callTemplateReturningString("go");
625
    if (string(result).find(string("<a/>")) != std::string::npos) {
626
        sresult->success++;
627
        delete result;
628
    } else {
629
        //TODO - this test case prints the XML declaration. Check if this correct
630
        sresult->failure++;
631
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
632
        sresult->failureList.push_back("testXdmNodeOutput");
633
    }
634
    delete rootValue;
635
    delete executable;
636

    
637
}
638

    
639
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
640
    cout << "ExampleSimple1 taken from PHP:" << endl;
641

    
642
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
643
    if(executable== nullptr) {
644
        sresult->failure++;
645
        sresult->failureList.push_back("exampleSimple1");
646
        cerr << "exampleSimple1 NULL found" << endl;
647
        if (proc->exceptionOccurred()) {
648
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
649
        }
650
        return;
651
    }
652
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
653
    const char *result = executable->applyTemplatesReturningString();
654
    if (result != NULL) {
655
        cout << result << endl;
656
        sresult->success++;
657
        delete result;
658
    } else {
659
        cout << "Result is null ====== FAIL ====== " << endl;
660
        sresult->failure++;
661
    }
662
    proc->clearParameters();
663
    delete executable;
664
}
665

    
666
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
667
    cout << "ExampleSimple1Err taken from PHP:" << endl;
668

    
669
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
670
    if(executable == nullptr || proc->exceptionOccurred()) {
671
        if( proc->exceptionOccurred()) {
672
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
673
        }
674
        cout << "Result expected as null " << endl;
675
        proc->exceptionClear();
676
        sresult->success++;
677
        return;
678
    } else {
679
        sresult->failure++;
680
        sresult->failureList.push_back("exampleSimple1Err");
681

    
682
        delete executable;
683
    }
684

    
685
}
686

    
687

    
688
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
689
    cout << "<b>exampleSimple2:</b><br/>" << endl;
690
    proc->setcwd("/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests");
691

    
692
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
693
    if(executable== nullptr) {
694
        sresult->failure++;
695
        sresult->failureList.push_back("exampleSimple2");
696
        cerr << "exampleSimple2 NULL found" << endl;
697
        if (proc->exceptionOccurred()) {
698
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
699
        }
700
        return;
701
    }
702

    
703
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
704
    const char *filename = "output1.xml";
705
    executable->setOutputFile(filename);
706
    executable->applyTemplatesReturningFile("output1.xml");
707

    
708
    if (CppTestUtils::exists("output1.xml")) {
709
        cout << "The file $filename exists" << endl;
710
        remove("output1.xml");
711
        sresult->success++;
712
    } else {
713
        cout << "The file " << filename << " does not exist" << endl;
714
        if (executable->exceptionOccurred()) {
715
            cout << proc->getErrorMessage() << endl;
716
        }
717
        sresult->failure++;
718
        sresult->failureList.push_back("exampleSimple2");
719
    }
720

    
721
    delete executable;
722

    
723
}
724

    
725
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
726
    cout << "<b>exampleSimple3:</b><br/>" << endl;
727
    proc->clearParameters();
728

    
729
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
730
    if (xdmNode== nullptr) {
731
        cout << "Error: xdmNode is null'" << endl;
732
        if(saxonProc->exceptionOccurred()) {
733
            cout<<"Error message="<<saxonProc->getErrorMessage()<<endl;
734
        }
735
        sresult->failure++;
736
        sresult->failureList.push_back("exampleSimple3");
737
        return;
738
    }
739

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

    
742
   if(executable == nullptr) {
743
        cout << "executable is NULL" <<endl;
744
        if(proc->exceptionOccurred()) {
745
                cout << proc->getErrorMessage() <<endl;
746
                
747
        }
748
        sresult->failure++;
749
        sresult->failureList.push_back("exampleSimple3");
750
        return;
751
   }
752

    
753
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
754
    sresult->success++;
755
    delete xdmNode;
756
    delete executable;
757
}
758

    
759
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
760
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
761

    
762
    XsltExecutable * executable = proc->compileFromFile(nullptr);
763

    
764

    
765
    if (executable == nullptr) {
766

    
767

    
768
        cout << "Expected failure of test exampleSimple3aError:" << endl;
769
        if(proc->exceptionOccurred()) {
770
            cout<<proc->getErrorMessage()<<endl;
771
        }
772

    
773
        sresult->success++;
774
        proc->exceptionClear();
775
        return;
776
    }
777

    
778
    cout << "Error: executable is not nullptr'" << endl;
779
    sresult->failure++;
780
    sresult->failureList.push_back("exampleSimple3aError");
781
    delete executable;
782
    return;
783

    
784

    
785
}
786

    
787
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
788
    cout << "Test: ExampleParam" << endl;
789

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

    
792

    
793
    if(executable == nullptr) {
794
        cout << "executable is NULL" <<endl;
795
        if(proc->exceptionOccurred()) {
796
            cout << proc->getErrorMessage() <<endl;
797

    
798
        }
799
        sresult->failure++;
800
        sresult->failureList.push_back("exampleParam");
801
        return;
802

    
803
    }
804

    
805
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
806

    
807
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
808
    if (xdmvalue != nullptr) {
809

    
810
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
811

    
812
    } else {
813
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
814
        sresult->failure++;
815
        sresult->failureList.push_back("exampleParam");
816
        if(executable->exceptionOccurred()) {
817
            SaxonApiException * exception = executable->getException();
818
            cerr<<"Error: " << exception->getMessage() <<endl;
819
            delete exception;
820
        }
821
        delete executable;
822
        return;
823
    }
824
    const char *result = executable->applyTemplatesReturningString();
825
    if (result != nullptr) {
826
        cout << "Output:" << result << endl;
827
        sresult->success++;
828
        delete result;
829
    } else {
830
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
831
        sresult->failure++;
832
        sresult->failureList.push_back("exampleParam");
833
        if(executable->exceptionOccurred()) {
834
            SaxonApiException * exception = executable->getException();
835
            cerr<<"Error: " << exception->getMessage() <<endl;
836
            delete exception;
837
        }
838
        delete executable;
839
        return;
840
    }
841

    
842
    //proc->clearParameters();
843
    //unset($result);
844
    //echo 'again with a no parameter value<br/>';
845

    
846
    executable->setProperty("!indent", "yes");
847
    const char *result2 = executable->applyTemplatesReturningString();
848

    
849

    
850
    if (result2 != nullptr) {
851
        cout << "Result2 output= " << result2 << endl;
852
        sresult->success++;
853
        delete result2;
854
    } else {
855
        cout << "Result2 is NULL<br/>  ======= fail =====" << endl;
856
        sresult->failure++;
857
        sresult->failureList.push_back("exampleParam");
858
        if(executable->exceptionOccurred()) {
859
            SaxonApiException * exception = executable->getException();
860
            cerr<<"Error: " << exception->getMessage() <<endl;
861
            delete exception;
862
        }
863
        delete xdmvalue;
864
        delete executable;
865
        return;
866
    }
867

    
868
    //  unset($result);
869
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
870
    //delete xdmvalue;
871
    //executable->clearParameters();
872

    
873
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
874

    
875
    if (xdmValue2 == nullptr) {
876
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
877
        sresult->failure++;
878
        sresult->failureList.push_back("exampleParam");
879
        if(executable->exceptionOccurred()) {
880
            SaxonApiException * exception = executable->getException();
881
            cerr<<"Error: " << exception->getMessage() <<endl;
882
            delete exception;
883
        }
884
        delete executable;
885
        delete xdmvalue;
886
        return;
887
    }
888

    
889
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
890
    delete xdmvalue;
891

    
892

    
893
    const char *result3 = executable->applyTemplatesReturningString();
894
    if (result3 != nullptr) {
895
        cout << "Output =" << result3 << endl;
896

    
897
        sresult->success++;
898
        delete result3;
899

    
900
    } else {
901
        cout << "Error in result ===== FAIL =======" << endl;
902
        sresult->failure++;
903
        sresult->failureList.push_back("exampleParam");
904
        if(executable->exceptionOccurred()) {
905
            SaxonApiException * exception = executable->getException();
906
            cerr<<"Error: " << exception->getMessage() <<endl;
907
            delete exception;
908
        }
909

    
910
    }
911
    delete xdmValue2;
912
    delete executable;
913

    
914

    
915

    
916
}
917

    
918
// test parameter and properties maps where we update key, value pair.
919
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
920
    cout << "\nExampleParam:</b><br/>" << endl;
921
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
922
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
923

    
924

    
925
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
926
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
927
    if (xdmvalue != NULL) {
928

    
929
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
930
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
931

    
932
    } else {
933
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
934
        sresult->failure++;
935
        sresult->failureList.push_back("exampleParam-1");
936
    }
937
    const char *result = executable->applyTemplatesReturningString();
938
    if (result != NULL) {
939
        string sresulti = string(result);
940
        if (sresulti.compare("Hello from me") == 0) {
941
            cout << "Output:" << result << endl;
942
            sresult->success++;
943
        } else {
944
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
945
            sresult->failure++;
946
            sresult->failureList.push_back("exampleParam-2");
947
        }
948
    } else {
949
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
950
        sresult->failure++;
951
        sresult->failureList.push_back("exampleParam-2");
952
    }
953

    
954
//proc->clearParameters();
955
//unset($result);
956
//echo 'again with a no parameter value<br/>';
957

    
958
    executable->setProperty("!indent", "no");
959
    executable->setProperty("!indent", "yes");
960
    const char *result2 = executable->applyTemplatesReturningString();
961

    
962
    executable->clearProperties();
963

    
964
    if(result2 != NULL) {
965
        cout<<result2<<endl;
966
        sresult->success++;
967

    
968
    }
969

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

    
975
    const char *result3 = executable->applyTemplatesReturningString();
976
    if(result3 != NULL) {
977
        cout<<"Output ="<<result3<<endl;
978

    
979
        sresult->success++;
980
    } else {
981
        cout<<"Error in result ===== FAIL ======="<<endl;
982
        sresult->failure++;
983
        sresult->failureList.push_back("exampleParam");
984
    }
985

    
986
}
987

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

    
992
    proc->setJustInTimeCompilation(true);
993

    
994
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
995
    if (result != NULL) {
996
        cout << "XdmNode returned" << endl;
997
        sresult->success++;
998
        delete result;
999
    } else {
1000
        printf("result is null \nCheck For errors:");
1001
        sresult->failure++;
1002
        sresult->failureList.push_back("xmarkTest1");
1003
        if (proc->exceptionOccurred()) {
1004
            cerr << proc->getErrorMessage() << endl;
1005
        }
1006
    }
1007

    
1008

    
1009
}
1010

    
1011

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

    
1016

    
1017
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1018
    if (result != NULL) {
1019
        cout << "XdmNode returned" << endl;
1020
        sresult->success++;
1021
        delete result;
1022

    
1023
    } else {
1024
        printf("result is null \nCheck For errors:");
1025
        sresult->failure++;
1026
        sresult->failureList.push_back("xmarkTest2");
1027
        if (proc->exceptionOccurred() > 0) {
1028
            cout << proc->getErrorMessage() << endl;
1029
        }
1030
    }
1031

    
1032

    
1033
}
1034

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

    
1039
    proc->setJustInTimeCompilation(true);
1040

    
1041
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1042
    if (result != NULL) {
1043
        cout << "XdmNode returned" << endl;
1044
        sresult->success++;
1045
        delete result;
1046
    } else {
1047
        printf("Result is null \nCheck For errors:");
1048
        if (proc->exceptionOccurred() > 0) {
1049
            cout << proc->getErrorMessage() << endl;
1050
        }
1051
        sresult->failure++;
1052
        sresult->failureList.push_back("exampleSimple_xmark");
1053
        proc->exceptionClear();
1054
    }
1055
    proc->clearParameters();
1056

    
1057
}
1058

    
1059
/*
1060
* Test saving nd loading a Xslt package
1061
*/
1062
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1063

    
1064
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1065
    trans->clearParameters();
1066

    
1067

    
1068
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1069
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1070

    
1071
    if (output== nullptr) {
1072
        printf("result is null \n");
1073
        const char *message = trans->getErrorMessage();
1074
        if (message != NULL) {
1075
            cout << "Error message =" << message << endl;
1076
        }
1077
        sresult->failure++;
1078
        sresult->failureList.push_back("testPackage1");
1079

    
1080
    } else {
1081
        printf("%s", output);
1082
        printf("result is OK \n");
1083
        sresult->success++;
1084
    }
1085
    fflush(stdout);
1086
    delete output;
1087
}
1088

    
1089

    
1090
/*
1091
* Test saving and loading a Xslt package
1092
*/
1093
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1094

    
1095
    cout << endl << "Test: testPackage1a" << endl;
1096
    trans->clearParameters();
1097

    
1098
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1099

    
1100
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1101
    executable->setInitialMatchSelectionAsFile("cat.xml");
1102
    const char * output = executable->applyTemplatesReturningString();
1103
    if (output== nullptr) {
1104
        printf("result is null \n");
1105
        const char *message = trans->getErrorMessage();
1106
        if (message != NULL) {
1107
            cout << "Error message =" << message << endl;
1108
        }
1109
        sresult->failure++;
1110
        sresult->failureList.push_back("testPackage1a");
1111

    
1112
    } else {
1113
        printf("%s", output);
1114
        printf("result is OK \n");
1115
        sresult->success++;
1116
    }
1117
    fflush(stdout);
1118
    delete output;
1119
}
1120

    
1121

    
1122
/*
1123
* Test saving and loading a Xslt package
1124
*/
1125
void testPackage2_Error(Xslt30Processor *trans, sResultCount *sresult) {
1126

    
1127
    cout << endl << "Test: testPackage2_Error:" << endl;
1128

    
1129
    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>";
1130

    
1131
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1132

    
1133
    if(trans->exceptionOccurred()) {
1134
        const char *message = trans->getErrorMessage();
1135
        if (message != NULL) {
1136
            cout << "Error message =" << message << endl;
1137
        }
1138
        sresult->success++;
1139
        trans->exceptionClear();
1140
        return;
1141

    
1142
    }
1143

    
1144
    sresult->failure++;
1145
    sresult->failureList.push_back("testPackage2_Error");
1146

    
1147
}
1148

    
1149
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1150

    
1151
    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>";
1152
    cout << endl << "Test: testCallFunction:" << endl;
1153
    XsltExecutable * executable = trans->compileFromString(source);
1154

    
1155
    if(executable == nullptr) {
1156
        if(trans->exceptionOccurred()) {
1157
            cerr<<"Error: "<<trans->getErrorMessage()<<endl;
1158

    
1159
        }
1160
        sresult->failure++;
1161
        sresult->failureList.push_back("testCallFunction");
1162
        return;
1163

    
1164

    
1165
    }
1166

    
1167
    XdmValue **valueArray = new XdmValue *[2];
1168

    
1169
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1170
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1171
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1172

    
1173
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1174
        sresult->success++;
1175
    } else {
1176
        if (v != NULL && !(v->getHead())->isAtomic()) {
1177
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1178
        }
1179
        cout << "testCallFunction ======= FAIL ======" << endl;
1180
        if(executable->exceptionOccurred()) {
1181
            SaxonApiException * exception = executable->getException();
1182
            cerr<<"Error: " << exception->getMessage() <<endl;
1183
            delete exception;
1184
        }
1185
        sresult->failure++;
1186
        sresult->failureList.push_back("testCallFunction");
1187
    }
1188
    delete valueArray[0];
1189
    delete valueArray[1];
1190
    delete [] valueArray;
1191
}
1192

    
1193
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1194

    
1195

    
1196
    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>";
1197
    cout << endl << "Test:testInitialTemplate" << endl;
1198
    XsltExecutable * executable = trans->compileFromString(source);
1199
    if(executable == nullptr) {
1200
        if(trans->exceptionOccurred()) {
1201
            cout << "Error: "<< trans->getErrorMessage() << endl;
1202
        }
1203
        return;
1204
    }
1205
    XdmNode *node = proc->parseXmlFromString("<e/>");
1206

    
1207
    executable->setResultAsRawValue(false);
1208
    std::map<std::string, XdmValue *> parameterValues;
1209

    
1210
    XdmAtomicValue * a1 = proc->makeIntegerValue(12);
1211
    XdmAtomicValue * a2 = proc->makeIntegerValue(5);;
1212
    parameterValues["a"] = a1;
1213
    parameterValues["b"] = a2;
1214
    executable->setInitialTemplateParameters(parameterValues, false);
1215
    executable->setInitialMatchSelection(node);
1216
    XdmValue *result = executable->applyTemplatesReturningValue();
1217
    if (result != NULL) {
1218
        sresult->success++;
1219
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1220
        delete result;
1221
    } else {
1222
        sresult->failure++;
1223
    }
1224

    
1225
    delete executable;
1226
    delete a1;
1227
    delete a2;
1228
}
1229

    
1230
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1231
    cout << endl << "Test: testResolveUri:" << endl;
1232

    
1233
    XsltExecutable * executable = trans->compileFromString(
1234
            "<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>");
1235

    
1236
    if(executable == nullptr) {
1237
        sresult->failure++;
1238
        sresult->failureList.push_back("testResolveUri");
1239
        if(trans->exceptionOccurred()) {
1240
            cout << "Error: "<< trans->getErrorMessage() << endl;
1241
        }
1242
        return;
1243
    }
1244
    XdmValue *value = executable->callTemplateReturningValue("go");
1245

    
1246

    
1247
    if (value== nullptr) {
1248

    
1249
        sresult->failure++;
1250
        sresult->failureList.push_back("testResolveUri");
1251
    } else {
1252

    
1253
        const char *svalue = value->itemAt(0)->getStringValue();
1254
        cout << "testResolveUri = " << svalue << endl;
1255
        sresult->success++;
1256
    }
1257

    
1258

    
1259
}
1260

    
1261
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1262
    cout << endl << "Test: testContextNotRoot" << endl;
1263

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

    
1266
    XsltExecutable * executable = trans->compileFromString(
1267
            "<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>");
1268

    
1269
    if(executable == nullptr) {
1270
        sresult->failure++;
1271
        sresult->failureList.push_back("testContextNotRoot");
1272
        if(trans->exceptionOccurred()) {
1273
            cout << "Error: "<< trans->getErrorMessage() << endl;
1274
        }
1275
        return;
1276
    }
1277

    
1278
    executable->setGlobalContextItem(node);
1279
    if (node->getChildCount() > 0) {
1280
        XdmNode *eNode = node->getChildren()[0]->getChildren()[0];
1281
        cout << "Node content = " << eNode->toString() << endl;
1282
        executable->setInitialMatchSelection(eNode);
1283
        const char *result = executable->applyTemplatesReturningString();
1284

    
1285
        if (result== nullptr) {
1286

    
1287
            cout << "testCallFunction ======= FAIL ======" << endl;
1288
            if(executable->exceptionOccurred()) {
1289
                SaxonApiException * exception = executable->getException();
1290
                cerr<<"Error: " << exception->getMessage() <<endl;
1291
                delete exception;
1292
            }
1293
            sresult->failure++;
1294
            sresult->failureList.push_back("testContextNotRoot");
1295
        } else {
1296

    
1297
            cout << "testContextNotRoot = " << result << endl;
1298
            sresult->success++;
1299
        }
1300
    }
1301
}
1302

    
1303

    
1304
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1305
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1306

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

    
1309
    XsltExecutable * executable = trans->compileFromString(
1310
            "<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>");
1311

    
1312
    if(executable == nullptr) {
1313
        sresult->failure++;
1314
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1315
        if(trans->exceptionOccurred()) {
1316
            cout << "Error: "<< trans->getErrorMessage() << endl;
1317
        }
1318
        return;
1319
    }
1320

    
1321
    executable->setGlobalContextItem(node);
1322
    const char *result = executable->callTemplateReturningString("main");
1323

    
1324
    if (result== nullptr) {
1325

    
1326
        cout << "testCallFunction ======= FAIL ======" << endl;
1327
        if(executable->exceptionOccurred()) {
1328
            SaxonApiException * exception = executable->getException();
1329
            cerr<<"Error: " << exception->getMessage() <<endl;
1330
            delete exception;
1331
        }
1332
        sresult->failure++;
1333
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1334
    } else {
1335

    
1336
        cout << "testContextNotRoot = " << result << endl;
1337
        sresult->success++;
1338
    }
1339

    
1340
}
1341

    
1342

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

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

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

    
1351
    if(executable == nullptr) {
1352
        sresult->failure++;
1353
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1354
        if(trans->exceptionOccurred()) {
1355
            cout << "Error: "<< trans->getErrorMessage() << endl;
1356
        }
1357
        return;
1358
    }
1359

    
1360
    executable->setGlobalContextItem(node);
1361
    XdmValue *result = executable->callTemplateReturningValue("main");
1362

    
1363
    if (result== nullptr) {
1364

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

    
1375
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1376
        sresult->success++;
1377
    }
1378

    
1379
}
1380

    
1381
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1382

    
1383
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1384
                                                            1);
1385
    if (fi== nullptr) {
1386
        sresult->failure++;
1387
        sresult->failureList.push_back("testCallSystemFunction");
1388
        return;
1389
    }
1390

    
1391
    XdmValue ** xdmValue = new XdmValue*[1];
1392
    xdmValue[0] = new XdmValue();
1393
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1394
    XdmValue *result = fi->call(xdmValue, 1);
1395

    
1396
    std::cerr << "Result = " << result->toString() << endl;
1397
    if(result->size() == 3) {
1398

    
1399
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1400
        sresult->success++;
1401

    
1402
    } else {
1403
        sresult->failure++;
1404
        sresult->failureList.push_back("testCallSystemFunction");
1405

    
1406
    }
1407

    
1408
}
1409

    
1410

    
1411
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1412
    cout << endl << "Test: testPipeline" << endl;
1413

    
1414
    Xslt30Processor * trans = proc->newXslt30Processor();
1415
    XsltExecutable * stage1 = trans->compileFromString(
1416
            "<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>");
1417

    
1418
    if(stage1 == nullptr) {
1419
        sresult->failure++;
1420
        sresult->failureList.push_back("testPipeline");
1421
        if(trans->exceptionOccurred()) {
1422
            cout << "stage 1 Error: "<< trans->getErrorMessage() << endl;
1423
        }
1424
        return;
1425
    }
1426

    
1427
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1428

    
1429
    XsltExecutable *stage2 = trans->compileFromString(
1430
            "<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>");
1431

    
1432
    if(stage2 == nullptr) {
1433
        sresult->failure++;
1434
        sresult->failureList.push_back("testPipeline");
1435
        if(trans->exceptionOccurred()) {
1436
            cout << "stage 2 Error: "<< trans->getErrorMessage() << endl;
1437
        }
1438
        return;
1439
    }
1440
    XsltExecutable *stage3 = trans->compileFromString(
1441
            "<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>");
1442

    
1443
    if(stage3 == nullptr) {
1444
        sresult->failure++;
1445
        sresult->failureList.push_back("testPipeline");
1446
        if(trans->exceptionOccurred()) {
1447
            cout << "stage 3 Error: "<< trans->getErrorMessage() << endl;
1448
        }
1449
        return;
1450
    }
1451
    XsltExecutable *stage4 = trans->compileFromString(
1452
            "<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>");
1453

    
1454
    if(stage3 == nullptr) {
1455
        sresult->failure++;
1456
        sresult->failureList.push_back("testPipeline");
1457
        if(trans->exceptionOccurred()) {
1458
            cout << "stage 4 Error: "<< trans->getErrorMessage() << endl;
1459
        }
1460
        return;
1461
    }
1462

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

    
1466
    if(stage5 == nullptr) {
1467
        sresult->failure++;
1468
        sresult->failureList.push_back("testPipeline");
1469
        if(trans->exceptionOccurred()) {
1470
            cout << "stage 5 Error: "<< trans->getErrorMessage() << endl;
1471
        }
1472
        return;
1473
    }
1474

    
1475
    stage1->setProperty("!indent", "no");
1476
    stage1->setInitialMatchSelection(inn);
1477

    
1478
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1479
    if (stage1->exceptionOccurred()) {
1480
        sresult->failure++;
1481
        sresult->failureList.push_back("testPipeline");
1482
        if(stage1->exceptionOccurred()) {
1483
            SaxonApiException * exception = stage1->getException();
1484
            cerr<<"Error: " << exception->getMessage() <<endl;
1485
            delete exception;
1486
        }
1487
        return;
1488
    }
1489

    
1490
    if (d1 == nullptr) {
1491
        sresult->failure++;
1492
        sresult->failureList.push_back("testPipeline-0");
1493
        return;
1494
    }
1495
    XdmItem *d11 = d1->getHead();
1496
    if (d11== nullptr) {
1497

    
1498
        cout << "d11 is NULL\n" << endl;
1499
    }
1500
    const char *data = d1->getHead()->toString();
1501

    
1502
    if (data != NULL) {
1503
        cout << "d1 result=" << data << endl;
1504
    } else {
1505
        sresult->failure++;
1506
        sresult->failureList.push_back("testPipeline-1");
1507

    
1508
        return;
1509
    }
1510
    stage2->setProperty("!indent", "no");
1511
    stage2->setInitialMatchSelection(d1->getHead());
1512
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1513
    if (d2== nullptr) {
1514
        cout << "ERROR-11\n" << endl;
1515
        if(stage2->exceptionOccurred()) {
1516
            SaxonApiException * exception = stage2->getException();
1517
            cerr<<"Error: " << exception->getMessage() <<endl;
1518
            delete exception;
1519
        }
1520
        sresult->failure++;
1521
        sresult->failureList.push_back("testPipeline-2");
1522
        return;
1523
    }
1524
    stage3->setProperty("!indent", "no");
1525
    stage3->setInitialMatchSelection(d2);
1526
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1527
    if(d3 == nullptr){
1528
        sresult->failure++;
1529
        sresult->failureList.push_back("testPipeline-3");
1530
        if(stage3->exceptionOccurred()) {
1531
            SaxonApiException * exception = stage3->getException();
1532
            cerr<<"Error: " << exception->getMessage() <<endl;
1533
            delete exception;
1534
        }
1535
      }
1536
    stage4->setProperty("!indent", "no");
1537
    stage4->setInitialMatchSelection(d3);
1538
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1539
    if(d3== nullptr){
1540
        sresult->failure++;
1541
        sresult->failureList.push_back("testPipeline-1");
1542
        if(stage4->exceptionOccurred()) {
1543
            SaxonApiException * exception = stage4->getException();
1544
            cerr<<"Error: " << exception->getMessage() <<endl;
1545
            delete exception;
1546
        }
1547
        return;
1548
      }
1549
    stage5->setProperty("!indent", "no");
1550
    stage5->setInitialMatchSelection(d4);
1551
    const char * sw = stage5->applyTemplatesReturningString();
1552
    if(sw==NULL){
1553
        sresult->failure++;
1554
        sresult->failureList.push_back("testPipeline-1");
1555
        if(stage5->exceptionOccurred()) {
1556
            SaxonApiException * exception = stage5->getException();
1557
            cerr<<"Error: " << exception->getMessage() <<endl;
1558
            delete exception;
1559
        }
1560
        return;
1561
      }
1562
    cout<<sw<<endl;
1563
    cout << "testPipeline = " << sw << endl;
1564
    sresult->success++;
1565

    
1566

    
1567
}
1568

    
1569

    
1570
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1571

    
1572

    
1573
    cout << endl << "Test: testCatalog" << endl;
1574
    bool trace = false;
1575
    proc->setcwd(cwd);
1576
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1577

    
1578
    Xslt30Processor * trans = proc->newXslt30Processor();
1579

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

    
1582
    if(executable == nullptr) {
1583

    
1584
        if (trans->exceptionOccurred()) {
1585

    
1586
            if(trans->getErrorMessage() != nullptr) {
1587
                const char *message = trans->getErrorMessage();
1588
                cerr << "exception=" << message << endl;
1589
            } else {
1590
                const char *message = proc->getErrorMessage();
1591
                if(message != nullptr) {
1592
                    cerr << "exception-proc=" << message << endl;
1593
                }
1594
            }
1595
        }
1596
        sresult->failure++;
1597
        sresult->failureList.push_back("testCatalog");
1598
        trans->exceptionClear();
1599
        return;
1600
    }
1601

    
1602
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1603

    
1604
    const char *result = executable->applyTemplatesReturningString();
1605

    
1606
    if(result != NULL) {
1607
        std::cerr << "testCatalog result= " << result << std::endl;
1608
    }
1609
    sresult->success++; // TODO - check the results more carefully
1610

    
1611
    delete executable;
1612

    
1613
}
1614

    
1615

    
1616
/*
1617
 *
1618
 *void *RunThread(void *args) {
1619

1620
    struct arg_struct *argsi = (struct arg_struct *)args;
1621
    int threadid = argsi->id;
1622
    Xslt30Processor * trans = argsi->trans;
1623
    long tid;
1624
    tid = (long)threadid;
1625

1626
    trans->attachThread();
1627

1628
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1629

1630
    const char *result = trans->applyTemplatesReturningString();
1631
    cout<<" Result from THREAD ID: "<< tid << ", " << result<<endl;
1632
    delete result;
1633
    //pthread_exit(NULL);
1634
    trans->detachThread();
1635
}
1636

1637
void testThreads (SaxonProcessor * processor) {
1638
    pthread_t threads[NUM_THREADS];
1639
    int rc;
1640
    int i;
1641

1642
    Xslt30Processor *  trans = processor->newXslt30Processor();
1643

1644
    trans->compileFromFile("../php/xsl/foo.xsl");
1645
    struct arg_struct args;
1646
    args.trans = trans;
1647

1648
    for( i = 0; i < NUM_THREADS; i++ ) {
1649
        cout << "main() : creating thread, " << i << endl;
1650
        args.id = i;
1651
        rc = pthread_create(&threads[i], NULL, RunThread, (void *)&args);
1652

1653
        if (rc) {
1654
            cout << "Error:unable to create thread," << rc << endl;
1655
            exit(-1);
1656
        }
1657
        (void) pthread_join(threads[i], NULL);
1658
    }
1659
  //  pthread_exit(NULL);
1660
}
1661

1662
 *
1663
 */
1664

    
1665

    
1666
int main(int argc, char* argv[]) {
1667

    
1668

    
1669
    const char * cwd = nullptr;
1670
    if(argc > 0) {
1671
        cwd = argv[1];
1672
    }
1673

    
1674
    SaxonProcessor *processor = new SaxonProcessor(true);
1675
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
1676
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
1677
    
1678
    char buff[FILENAME_MAX]; //create string buffer to hold path
1679
    GetCurrentDir( buff, FILENAME_MAX );
1680
    cout<<"CWD = "<< buff<<endl;
1681
    if(cwd != nullptr) {
1682
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
1683
    }
1684
    if (processor->isSchemaAwareProcessor()) {
1685

    
1686
        std::cerr << "Processor is Schema Aware" << std::endl;
1687
    } else {
1688
        std::cerr << "Processor is not Schema Aware" << std::endl;
1689
    }
1690

    
1691
    sResultCount *sresult = new sResultCount();
1692
    Xslt30Processor *trans = processor->newXslt30Processor();
1693

    
1694
    if( trans == nullptr) {
1695
        cout<< "Error creating Xslt30Processor"<<endl;
1696
        if(processor->exceptionOccurred()) {
1697
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
1698

    
1699
        }
1700
        return -1;
1701
    }
1702
    //testValidation(trans,sresult);
1703

    
1704
    testInitialTemplate(processor, trans, sresult);
1705

    
1706

    
1707

    
1708
    exampleSimple1Err(trans, sresult);
1709

    
1710
    exampleSimple1(trans, sresult);
1711

    
1712
    exampleSimple_xmark(trans, sresult);
1713

    
1714
    exampleSimple2(trans, sresult);
1715

    
1716
    exampleSimple3(processor, trans, sresult);
1717

    
1718
    exampleSimple3aError(processor, trans, sresult);
1719

    
1720
    testApplyTemplatesString1(trans, sresult);
1721

    
1722
    testApplyTemplatesString2(processor, trans, sresult);
1723

    
1724
    testApplyTemplates2a_Error(processor, trans, sresult);
1725

    
1726
    testTransformToString4(processor, trans, sresult);
1727

    
1728
    testCatalog(cwd, processor,  sresult);
1729

    
1730
    testTransformToString2b(processor, trans, sresult);
1731

    
1732
    testTransformToString3(processor, trans, sresult);
1733
        
1734
    testTransformFromstring(processor, trans, sresult);
1735

    
1736
    testTransformFromstring2Err(processor, trans, sresult);
1737

    
1738
    testTrackingOfValueReference(processor, trans, sresult);
1739

    
1740
    //testTrackingOfValueReferenceError(processor, trans, sresult);
1741

    
1742
    testPackage1(trans, sresult);
1743

    
1744
    testPackage1a(trans, sresult);
1745

    
1746
    testPackage2_Error(trans, sresult);
1747

    
1748
    testXdmNodeOutput(trans, sresult);
1749

    
1750
    exampleParam(processor, trans, sresult);
1751

    
1752
    //SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1753

    
1754

    
1755
    xmarkTest1(trans, sresult);
1756

    
1757
    xmarkTest2(trans, sresult);
1758

    
1759
    processor->release();
1760
    delete processor;
1761

    
1762
    return 0;
1763

    
1764
    testCallFunction(processor, trans, sresult);
1765

    
1766

    
1767

    
1768
    testResolveUri(processor, trans, sresult);
1769
    testContextNotRoot(processor, trans, sresult);
1770
    testContextNotRootNamedTemplate(processor, trans, sresult);
1771
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1772
    testPipeline(processor, sresult);
1773

    
1774
    //Available in PE and EE
1775
    //testTransformToStringExtensionFunc(processor, trans);
1776

    
1777
    delete trans;
1778
    delete processor;
1779
    //processor->release();
1780

    
1781

    
1782
    SaxonProcessor *processor2 = new SaxonProcessor(true);
1783

    
1784
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1785
    testApplyTemplatesString1(trans2, sresult);
1786
    delete trans2;
1787
    processor2->release();
1788

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

    
1792
    std::list<std::string>::iterator it;
1793
    std::cout << "Failed tests:" << std::endl;
1794
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
1795
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
1796
        //Print the contents
1797
        std::cout << it->c_str() << std::endl;
1798

    
1799
    }
1800

    
1801

    
1802
    return 0;
1803
}
(27-27/28)