Project

Profile

Help

Download (59.7 KB) Statistics
| Branch: | Revision:

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

1 72bf04c6 Norman Walsh
#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 501beb26 O'Neil Delpratt
#include "../../Saxon.C.API/XdmFunctionItem.h"
8 e05bbe34 O'Neil Delpratt
#include "cppExtensionFunction.h"
9
#include "CppTestUtils.h"
10 72bf04c6 Norman Walsh
#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 501beb26 O'Neil Delpratt
        {
21
                {
22
                        fname,
23
                        funcParameters,
24 e05bbe34 O'Neil Delpratt
                        (void *) &cppExtensionFunction::cppNativeCall
25 501beb26 O'Neil Delpratt
                }
26
        };
27 72bf04c6 Norman Walsh
28
29
/*
30
* Test transform to String. Source and stylesheet supplied as arguments
31
*/
32 501beb26 O'Neil Delpratt
void testApplyTemplatesString1(Xslt30Processor *trans, sResultCount *sresult) {
33
34 4ee4355d O'Neil Delpratt
    cout << "Test: testApplyTemplatesString1:" << endl;
35 9458d238 O'Neil Delpratt
//    trans->setupXslMessage(false);
36
37 6b6a9706 O'Neil Delpratt
    XsltExecutable *executable = trans->compileFromFile("test.xsl");
38 9458d238 O'Neil Delpratt
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 6b6a9706 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("cat.xml");
49 501beb26 O'Neil Delpratt
50
    const char *output = executable->applyTemplatesReturningString();
51 9458d238 O'Neil Delpratt
    if (output == nullptr) {
52 501beb26 O'Neil Delpratt
        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 9458d238 O'Neil Delpratt
        printf("%s", output);
58 501beb26 O'Neil Delpratt
        printf("result is OK \n");
59
        sresult->success++;
60 a69dd173 O'Neil Delpratt
        delete output;
61 72bf04c6 Norman Walsh
    } else {
62 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ====== \n");
63
        sresult->failure++;
64
        sresult->failureList.push_back("testApplyTemplatesString1-1");
65 a69dd173 O'Neil Delpratt
            std::cout<<"output="<<output<<std::endl;
66
        delete output;
67 501beb26 O'Neil Delpratt
    }
68
    fflush(stdout);
69 9458d238 O'Neil Delpratt
    delete executable;
70 72bf04c6 Norman Walsh
71
}
72
73
/*
74
* Test transform to String. Source and stylesheet supplied as arguments
75
*/
76 501beb26 O'Neil Delpratt
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 9458d238 O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    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 9458d238 O'Neil Delpratt
        if (output == nullptr) {
101 501beb26 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->ExceptionDescribe();
102
            printf("result is null ====== FAIL ======  \n");
103 4ee4355d O'Neil Delpratt
            sresult->failure++;
104 501beb26 O'Neil Delpratt
            sresult->failureList.push_back("testTransformToStringExnteionFunc");
105
        } else {
106 a69dd173 O'Neil Delpratt
            sresult->success++;
107 501beb26 O'Neil Delpratt
            printf("result is OK \n");
108
        }
109
        fflush(stdout);
110
        delete output;
111
    } else {
112
        printf("native Class not foun ====== FAIL ====== ");
113 4ee4355d O'Neil Delpratt
        sresult->failure++;
114 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToStringExtensionFunc");
115 72bf04c6 Norman Walsh
    }
116 9458d238 O'Neil Delpratt
    delete executable;
117 72bf04c6 Norman Walsh
}
118
119
120
/*
121
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
122
*/
123 501beb26 O'Neil Delpratt
void testApplyTemplatesString2(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
124 72bf04c6 Norman Walsh
125 4ee4355d O'Neil Delpratt
    cout << "Test: testApplyTemplatesString2:" << endl;
126
127 19d839c4 O'Neil Delpratt
128 501beb26 O'Neil Delpratt
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
129 72bf04c6 Norman Walsh
130 9458d238 O'Neil Delpratt
    if (input== nullptr) {
131 501beb26 O'Neil Delpratt
        cout << "Source document is null." << endl;
132 4ee4355d O'Neil Delpratt
        if(processor->exceptionOccurred()) {
133
            cerr<<processor->getErrorMessage()<<endl;
134
        }
135 a69dd173 O'Neil Delpratt
        sresult->failure++;
136
        sresult->failureList.push_back("testApplyTemplatesString2");
137
        return;
138 72bf04c6 Norman Walsh
139
    }
140 79d12c83 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
141 a69dd173 O'Neil Delpratt
    if(executable == nullptr) {
142 9458d238 O'Neil Delpratt
        sresult->failure++;
143
        sresult->failureList.push_back("testApplyTemplatesString2");
144
        if (trans->exceptionOccurred()) {
145
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
146
        }
147 a69dd173 O'Neil Delpratt
        delete input;
148 9458d238 O'Neil Delpratt
        return;
149
    }
150 a69dd173 O'Neil Delpratt
151 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmValue *) input);
152 4ee4355d O'Neil Delpratt
153 501beb26 O'Neil Delpratt
    const char *output = executable->applyTemplatesReturningString();
154 9458d238 O'Neil Delpratt
    if (output== nullptr) {
155 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ======  \n");
156
        sresult->failureList.push_back("testApplyTemplatesString2");
157 4ee4355d O'Neil Delpratt
        sresult->failure++;
158 501beb26 O'Neil Delpratt
    } else {
159 a69dd173 O'Neil Delpratt
        sresult->success++;
160 501beb26 O'Neil Delpratt
        printf("%s", output);
161
        printf("result is OK \n");
162 a69dd173 O'Neil Delpratt
        delete output;
163 72bf04c6 Norman Walsh
    }
164 501beb26 O'Neil Delpratt
    fflush(stdout);
165 4ee4355d O'Neil Delpratt
    delete input;
166 501beb26 O'Neil Delpratt
    delete executable;
167 72bf04c6 Norman Walsh
}
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 4ee4355d O'Neil Delpratt
void testApplyTemplates2a_Error(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
174
175
    cout << "Test: TransformToString2a_Error:" << endl;
176 72bf04c6 Norman Walsh
177 501beb26 O'Neil Delpratt
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
178
179 a69dd173 O'Neil Delpratt
    if (input == nullptr) {
180 501beb26 O'Neil Delpratt
        cout << "Source document is null. ====== FAIL ======" << endl;
181 4ee4355d O'Neil Delpratt
        if(processor->exceptionOccurred()) {
182
            cerr<<processor->getErrorMessage()<<endl;
183
        }
184 501beb26 O'Neil Delpratt
        sresult->failure++;
185 4ee4355d O'Neil Delpratt
        sresult->failureList.push_back("testApplyTemplates2a_Error");
186
        trans->exceptionClear();
187 501beb26 O'Neil Delpratt
        return;
188
    }
189
    XsltExecutable * executable = trans->compileFromFile("test-error.xsl");
190 9458d238 O'Neil Delpratt
191
    if(executable== nullptr) {
192 4ee4355d O'Neil Delpratt
        printf("Expected result is null \n");
193 a69dd173 O'Neil Delpratt
        sresult->success++;
194
195 9458d238 O'Neil Delpratt
        if (trans->exceptionOccurred()) {
196 4ee4355d O'Neil Delpratt
            cerr <<"testApplyTemplates2a_Error error: "<< trans->getErrorMessage() << endl;
197 9458d238 O'Neil Delpratt
        }
198 a69dd173 O'Neil Delpratt
        delete input;
199 4ee4355d O'Neil Delpratt
        trans->exceptionClear();
200 9458d238 O'Neil Delpratt
        return;
201
    }
202
203 a69dd173 O'Neil Delpratt
    sresult->failure++;
204 4ee4355d O'Neil Delpratt
    sresult->failureList.push_back("testApplyTemplates2a_Error");
205 501beb26 O'Neil Delpratt
    fflush(stdout);
206 4ee4355d O'Neil Delpratt
207 9458d238 O'Neil Delpratt
    delete executable;
208 501beb26 O'Neil Delpratt
209 72bf04c6 Norman Walsh
}
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 501beb26 O'Neil Delpratt
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
216 72bf04c6 Norman Walsh
217 501beb26 O'Neil Delpratt
    cout << endl << "Test: TransformToString2b:" << endl;
218 72bf04c6 Norman Walsh
219 a69dd173 O'Neil Delpratt
    const char * result  = trans->transformFileToString("cat-error.xml", "test-error.xsl");
220 72bf04c6 Norman Walsh
221 a69dd173 O'Neil Delpratt
    if(result== nullptr) {
222 4ee4355d O'Neil Delpratt
        if(trans->exceptionOccurred()) {
223
            cerr<<"Expected failure = "<<trans->getErrorMessage()<<endl;
224
        }
225 a69dd173 O'Neil Delpratt
        sresult->success++;
226 4ee4355d O'Neil Delpratt
        trans->exceptionClear();
227 501beb26 O'Neil Delpratt
        return;
228 a69dd173 O'Neil Delpratt
    }else {
229
        delete result;
230 9458d238 O'Neil Delpratt
        sresult->failure++;
231
        sresult->failureList.push_back("testTransformToString2b");
232
        cerr << "testTransformToString2b NULL found" << endl;
233
        if (trans->exceptionOccurred()) {
234 4ee4355d O'Neil Delpratt
            cerr <<"Error: "<< trans->getErrorMessage() << endl;
235 9458d238 O'Neil Delpratt
        }
236 a69dd173 O'Neil Delpratt
        trans->exceptionClear();
237 9458d238 O'Neil Delpratt
        return;
238
    }
239
240 72bf04c6 Norman Walsh
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 501beb26 O'Neil Delpratt
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
249
250 4ee4355d O'Neil Delpratt
    cout << endl << "Test: TransformToString3" << endl;
251 72bf04c6 Norman Walsh
252 501beb26 O'Neil Delpratt
    XdmNode *inputi = processor->parseXmlFromString(
253
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
254 72bf04c6 Norman Walsh
255 9458d238 O'Neil Delpratt
    if (inputi== nullptr) {
256 4ee4355d O'Neil Delpratt
        cout << "Source document inputi is null." << endl;
257
        if (trans->exceptionOccurred()) {
258
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
259
        }
260
        sresult->failure++;
261 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToString3");
262 146cfacb O'Neil Delpratt
263 501beb26 O'Neil Delpratt
        return;
264 72bf04c6 Norman Walsh
    }
265
266 146cfacb O'Neil Delpratt
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
267
268 4ee4355d O'Neil Delpratt
    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 146cfacb O'Neil Delpratt
279 501beb26 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
280 9458d238 O'Neil Delpratt
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 a69dd173 O'Neil Delpratt
        delete value1;
289 9458d238 O'Neil Delpratt
        return;
290
    }
291
292 146cfacb O'Neil Delpratt
293 4ee4355d O'Neil Delpratt
    executable->setParameter("numParam", (XdmValue *) value1);
294 146cfacb O'Neil Delpratt
295 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmNode *) inputi);
296
    const char *output = executable->applyTemplatesReturningString();
297 a69dd173 O'Neil Delpratt
    if (output == nullptr) {
298 501beb26 O'Neil Delpratt
        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 a69dd173 O'Neil Delpratt
        delete output;
304 72bf04c6 Norman Walsh
    }
305 501beb26 O'Neil Delpratt
    fflush(stdout);
306 a69dd173 O'Neil Delpratt
307 9458d238 O'Neil Delpratt
    delete value1;
308 4ee4355d O'Neil Delpratt
    delete inputi;
309 9458d238 O'Neil Delpratt
    delete executable;
310 72bf04c6 Norman Walsh
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 501beb26 O'Neil Delpratt
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
318
319 4ee4355d O'Neil Delpratt
    cout <<  "Test: testTransformToString4:" << endl;
320 501beb26 O'Neil Delpratt
321
    XdmNode *input = processor->parseXmlFromString(
322
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
323
324 a69dd173 O'Neil Delpratt
    if (input== nullptr) {
325
        cout << "Source document is null. ====== FAIL ====== " << endl;
326 4ee4355d O'Neil Delpratt
        sresult->failure++;
327 a69dd173 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToString4");
328
    }
329
330
331 d791e94d O'Neil Delpratt
    XdmValue *values = new XdmValue();
332 4ee4355d O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
341 a69dd173 O'Neil Delpratt
342 4ee4355d O'Neil Delpratt
    XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
343 501beb26 O'Neil Delpratt
344
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
345 9458d238 O'Neil Delpratt
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 a69dd173 O'Neil Delpratt
        delete values;
354 9458d238 O'Neil Delpratt
        return;
355
    }
356
357
    executable->setParameter("values", (XdmValue *) values);
358 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmNode *) input);
359 a69dd173 O'Neil Delpratt
    const char *output = executable->applyTemplatesReturningString();
360 9458d238 O'Neil Delpratt
    if (output== nullptr) {
361 501beb26 O'Neil Delpratt
        printf("result is null \n");
362 4ee4355d O'Neil Delpratt
        sresult->failure++;
363 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToString4");
364
    } else {
365
        printf("%s", output);
366
        printf("result is OK \n");
367 a69dd173 O'Neil Delpratt
        delete output;
368 72bf04c6 Norman Walsh
    }
369 501beb26 O'Neil Delpratt
    fflush(stdout);
370 a69dd173 O'Neil Delpratt
    delete sheet;
371 4ee4355d O'Neil Delpratt
    delete executable;
372 9458d238 O'Neil Delpratt
    delete input;
373
    delete values;
374 72bf04c6 Norman Walsh
375
}
376
377 501beb26 O'Neil Delpratt
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
378 4ee4355d O'Neil Delpratt
    cout << endl << "Test: testTransfromFromstring: "<< endl;
379
380 a69dd173 O'Neil Delpratt
    if(processor == nullptr) {
381
        cout<<" processor is null"<<endl;
382
        return;
383 72bf04c6 Norman Walsh
384 a69dd173 O'Neil Delpratt
    }
385 501beb26 O'Neil Delpratt
    XdmNode *input = processor->parseXmlFromString(
386
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
387 72bf04c6 Norman Walsh
388 146cfacb O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    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 146cfacb O'Neil Delpratt
    if(executable== nullptr) {
402 9458d238 O'Neil Delpratt
        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 146cfacb O'Neil Delpratt
        delete input;
409 9458d238 O'Neil Delpratt
        return;
410
    }
411
412 501beb26 O'Neil Delpratt
    const char *output = executable->transformToString((XdmNode *) input);
413 9458d238 O'Neil Delpratt
    if (output== nullptr) {
414 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ====== \n");
415 4ee4355d O'Neil Delpratt
        sresult->failure++;
416 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformFromString");
417 a69dd173 O'Neil Delpratt
        if (trans->exceptionOccurred()) {
418
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
419
        }
420 501beb26 O'Neil Delpratt
    } else {
421
        printf("%s", output);
422
        printf("result is OK \n");
423 4ee4355d O'Neil Delpratt
        sresult->success++;
424 a69dd173 O'Neil Delpratt
        delete output;
425 72bf04c6 Norman Walsh
    }
426 501beb26 O'Neil Delpratt
    fflush(stdout);
427 9458d238 O'Neil Delpratt
    delete input;
428
    delete executable;
429 72bf04c6 Norman Walsh
430
431
}
432
433
//Test case has error in the stylesheet
434 501beb26 O'Neil Delpratt
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 9458d238 O'Neil Delpratt
440
    if(executable== nullptr) {
441 4ee4355d O'Neil Delpratt
        sresult->success++;
442
443 9458d238 O'Neil Delpratt
        if (trans->exceptionOccurred()) {
444 4ee4355d O'Neil Delpratt
            cerr <<"Error expected: "<< trans->getErrorMessage() << endl;
445 9458d238 O'Neil Delpratt
        }
446
        return;
447
    }
448
449 72bf04c6 Norman Walsh
450 4ee4355d O'Neil Delpratt
    sresult->failure++;
451
    sresult->failureList.push_back("testTransfromFromstring2-Error");
452 9458d238 O'Neil Delpratt
    delete executable;
453 72bf04c6 Norman Walsh
454
}
455
456 501beb26 O'Neil Delpratt
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 4ee4355d O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    }
482
483
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
484
    if (parMap.size() > 0) {
485 fc47eaa6 O'Neil Delpratt
        //cout << "Parameter size: " << parMap.size() << endl;
486
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
487 501beb26 O'Neil Delpratt
        ostringstream name1;
488
        for (int i = 0; i < 10; i++) {
489 4ee4355d O'Neil Delpratt
            name1 << "sparam:value" << i;
490 501beb26 O'Neil Delpratt
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
491
            XdmValue *valuei = parMap[name1.str()];
492 4ee4355d O'Neil Delpratt
            if (valuei != nullptr) {
493 501beb26 O'Neil Delpratt
                cout << name1.str();
494 4ee4355d O'Neil Delpratt
                if (valuei->itemAt(0) != nullptr)
495 501beb26 O'Neil Delpratt
                    cout << "= " << valuei->itemAt(0)->getStringValue();
496
                cout << endl;
497 4ee4355d O'Neil Delpratt
498 501beb26 O'Neil Delpratt
            } else {
499
                sresult->failure++;
500
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
501
                sresult->failureList.push_back("testTrackingOfValueReference");
502 d0e6f8be O'Neil Delpratt
                return;
503 501beb26 O'Neil Delpratt
            }
504
            name1.str("");
505
        }
506
    }
507 4ee4355d O'Neil Delpratt
    if(parMap.size() > 0 ) {
508
        cerr<<"Deleting map"<<endl;
509
        trans->clearParameters(true);
510
    }
511 501beb26 O'Neil Delpratt
    sresult->success++;
512 72bf04c6 Norman Walsh
}
513
514
/*Test case should be error.*/
515 501beb26 O'Neil Delpratt
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
516 146cfacb O'Neil Delpratt
    trans->clearParameters();
517 501beb26 O'Neil Delpratt
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 4ee4355d O'Neil Delpratt
    for (int i = 0; i < 2; i++) {
524 501beb26 O'Neil Delpratt
        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 4ee4355d O'Neil Delpratt
        if(values == nullptr) {
531
            cerr<<"values NULL ====== FAIL ======="<<endl;
532
        } else {
533
            trans->setParameter(name.str().c_str(), values);
534
        }
535 501beb26 O'Neil Delpratt
        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 4ee4355d O'Neil Delpratt
    for (int i = 0; i < 2; i++) {
545
        name1 << "sparam:value" << i;
546 501beb26 O'Neil Delpratt
        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 146cfacb O'Neil Delpratt
    trans->clearParameters();
573 501beb26 O'Neil Delpratt
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 9458d238 O'Neil Delpratt
    if (rootValue== nullptr) {
583 501beb26 O'Neil Delpratt
        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 9458d238 O'Neil Delpratt
    delete executable;
593 72bf04c6 Norman Walsh
}
594
595
596 501beb26 O'Neil Delpratt
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
597 72bf04c6 Norman Walsh
598 501beb26 O'Neil Delpratt
    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 9458d238 O'Neil Delpratt
    if (rootValue== nullptr) {
603 501beb26 O'Neil Delpratt
        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 9458d238 O'Neil Delpratt
    if (rootItem== nullptr) {
610 72bf04c6 Norman Walsh
611 501beb26 O'Neil Delpratt
        cout << "Result is null ====== FAIL ====== " << endl;
612
        sresult->failure++;
613
        sresult->failureList.push_back("testXdmNodeOutput-0");
614 4ee4355d O'Neil Delpratt
        delete rootValue;
615
        delete executable;
616 501beb26 O'Neil Delpratt
        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 4ee4355d O'Neil Delpratt
        delete result;
628 501beb26 O'Neil Delpratt
    } else {
629
        //TODO - this test case prints the XML declaration. Check if this correct
630
        sresult->failure++;
631
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
632 9458d238 O'Neil Delpratt
        sresult->failureList.push_back("testXdmNodeOutput");
633 501beb26 O'Neil Delpratt
    }
634 4ee4355d O'Neil Delpratt
    delete rootValue;
635 9458d238 O'Neil Delpratt
    delete executable;
636 72bf04c6 Norman Walsh
637
}
638
639 501beb26 O'Neil Delpratt
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
640
    cout << "ExampleSimple1 taken from PHP:" << endl;
641
642
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
643 9458d238 O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
653
    const char *result = executable->applyTemplatesReturningString();
654
    if (result != NULL) {
655
        cout << result << endl;
656
        sresult->success++;
657 a69dd173 O'Neil Delpratt
        delete result;
658 501beb26 O'Neil Delpratt
    } else {
659
        cout << "Result is null ====== FAIL ====== " << endl;
660
        sresult->failure++;
661
    }
662 146cfacb O'Neil Delpratt
    proc->clearParameters();
663 9458d238 O'Neil Delpratt
    delete executable;
664 72bf04c6 Norman Walsh
}
665
666 501beb26 O'Neil Delpratt
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
667 e05bbe34 O'Neil Delpratt
    cout << "ExampleSimple1Err taken from PHP:" << endl;
668 72bf04c6 Norman Walsh
669 501beb26 O'Neil Delpratt
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
670 a69dd173 O'Neil Delpratt
    if(executable == nullptr || proc->exceptionOccurred()) {
671
        if( proc->exceptionOccurred()) {
672
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
673
        }
674 e05bbe34 O'Neil Delpratt
        cout << "Result expected as null " << endl;
675 4ee4355d O'Neil Delpratt
        proc->exceptionClear();
676 e05bbe34 O'Neil Delpratt
        sresult->success++;
677 a69dd173 O'Neil Delpratt
        return;
678 e05bbe34 O'Neil Delpratt
    } else {
679 501beb26 O'Neil Delpratt
        sresult->failure++;
680
        sresult->failureList.push_back("exampleSimple1Err");
681 e05bbe34 O'Neil Delpratt
682 a69dd173 O'Neil Delpratt
        delete executable;
683 501beb26 O'Neil Delpratt
    }
684 a69dd173 O'Neil Delpratt
685 501beb26 O'Neil Delpratt
}
686
687 72bf04c6 Norman Walsh
688 501beb26 O'Neil Delpratt
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
689
    cout << "<b>exampleSimple2:</b><br/>" << endl;
690 9458d238 O'Neil Delpratt
    proc->setcwd("/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests");
691 501beb26 O'Neil Delpratt
692
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
693 9458d238 O'Neil Delpratt
    if(executable== nullptr) {
694 fc47eaa6 O'Neil Delpratt
        sresult->failure++;
695
        sresult->failureList.push_back("exampleSimple2");
696
        cerr << "exampleSimple2 NULL found" << endl;
697 9458d238 O'Neil Delpratt
        if (proc->exceptionOccurred()) {
698 fc47eaa6 O'Neil Delpratt
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
699
        }
700
        return;
701
    }
702 a69dd173 O'Neil Delpratt
703 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
704
    const char *filename = "output1.xml";
705
    executable->setOutputFile(filename);
706
    executable->applyTemplatesReturningFile("output1.xml");
707
708 e05bbe34 O'Neil Delpratt
    if (CppTestUtils::exists("output1.xml")) {
709 501beb26 O'Neil Delpratt
        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 4ee4355d O'Neil Delpratt
721 9458d238 O'Neil Delpratt
    delete executable;
722 72bf04c6 Norman Walsh
723
}
724
725 501beb26 O'Neil Delpratt
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
726
    cout << "<b>exampleSimple3:</b><br/>" << endl;
727 146cfacb O'Neil Delpratt
    proc->clearParameters();
728 501beb26 O'Neil Delpratt
729 4ee4355d O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
741
742 146cfacb O'Neil Delpratt
   if(executable == nullptr) {
743 e9b06a56 O'Neil Delpratt
        cout << "executable is NULL" <<endl;
744
        if(proc->exceptionOccurred()) {
745
                cout << proc->getErrorMessage() <<endl;
746
                
747
        }
748 4ee4355d O'Neil Delpratt
        sresult->failure++;
749
        sresult->failureList.push_back("exampleSimple3");
750 e9b06a56 O'Neil Delpratt
        return;
751
   }
752
753 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
754
    sresult->success++;
755 4ee4355d O'Neil Delpratt
    delete xdmNode;
756 a69dd173 O'Neil Delpratt
    delete executable;
757 501beb26 O'Neil Delpratt
}
758
759 4ee4355d O'Neil Delpratt
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
760
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
761 501beb26 O'Neil Delpratt
762
    XsltExecutable * executable = proc->compileFromFile(nullptr);
763
764
765
    if (executable == nullptr) {
766
767
768 4ee4355d O'Neil Delpratt
        cout << "Expected failure of test exampleSimple3aError:" << endl;
769
        if(proc->exceptionOccurred()) {
770
            cout<<proc->getErrorMessage()<<endl;
771
        }
772 501beb26 O'Neil Delpratt
773
        sresult->success++;
774 4ee4355d O'Neil Delpratt
        proc->exceptionClear();
775 501beb26 O'Neil Delpratt
        return;
776
    }
777
778
    cout << "Error: executable is not nullptr'" << endl;
779
    sresult->failure++;
780 4ee4355d O'Neil Delpratt
    sresult->failureList.push_back("exampleSimple3aError");
781 a69dd173 O'Neil Delpratt
    delete executable;
782 501beb26 O'Neil Delpratt
    return;
783
784
785
}
786
787
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
788 4ee4355d O'Neil Delpratt
    cout << "Test: ExampleParam" << endl;
789 501beb26 O'Neil Delpratt
790
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
791
792 4ee4355d O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
806
807
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
808 4ee4355d O'Neil Delpratt
    if (xdmvalue != nullptr) {
809 501beb26 O'Neil Delpratt
810
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
811
812
    } else {
813 4ee4355d O'Neil Delpratt
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
814 501beb26 O'Neil Delpratt
        sresult->failure++;
815 4ee4355d O'Neil Delpratt
        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 501beb26 O'Neil Delpratt
    }
824
    const char *result = executable->applyTemplatesReturningString();
825 4ee4355d O'Neil Delpratt
    if (result != nullptr) {
826 501beb26 O'Neil Delpratt
        cout << "Output:" << result << endl;
827
        sresult->success++;
828 4ee4355d O'Neil Delpratt
        delete result;
829 501beb26 O'Neil Delpratt
    } else {
830
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
831
        sresult->failure++;
832 4ee4355d O'Neil Delpratt
        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 501beb26 O'Neil Delpratt
    }
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 4ee4355d O'Neil Delpratt
    if (result2 != nullptr) {
851
        cout << "Result2 output= " << result2 << endl;
852 501beb26 O'Neil Delpratt
        sresult->success++;
853 4ee4355d O'Neil Delpratt
        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 501beb26 O'Neil Delpratt
    }
867
868
    //  unset($result);
869
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
870 4ee4355d O'Neil Delpratt
    //delete xdmvalue;
871
    //executable->clearParameters();
872
873 501beb26 O'Neil Delpratt
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
874 4ee4355d O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
890 4ee4355d O'Neil Delpratt
    delete xdmvalue;
891
892 501beb26 O'Neil Delpratt
893
    const char *result3 = executable->applyTemplatesReturningString();
894 4ee4355d O'Neil Delpratt
    if (result3 != nullptr) {
895 501beb26 O'Neil Delpratt
        cout << "Output =" << result3 << endl;
896 4ee4355d O'Neil Delpratt
897 501beb26 O'Neil Delpratt
        sresult->success++;
898 4ee4355d O'Neil Delpratt
        delete result3;
899
900 501beb26 O'Neil Delpratt
    } else {
901
        cout << "Error in result ===== FAIL =======" << endl;
902
        sresult->failure++;
903
        sresult->failureList.push_back("exampleParam");
904 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
905
            SaxonApiException * exception = executable->getException();
906
            cerr<<"Error: " << exception->getMessage() <<endl;
907
            delete exception;
908
        }
909
910 501beb26 O'Neil Delpratt
    }
911 4ee4355d O'Neil Delpratt
    delete xdmValue2;
912
    delete executable;
913
914 501beb26 O'Neil Delpratt
915
916 72bf04c6 Norman Walsh
}
917
918
// test parameter and properties maps where we update key, value pair.
919 501beb26 O'Neil Delpratt
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 4ee4355d O'Neil Delpratt
    cout << "Test: xmarkTest1 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
991 501beb26 O'Neil Delpratt
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 4ee4355d O'Neil Delpratt
        delete result;
999 501beb26 O'Neil Delpratt
    } else {
1000
        printf("result is null \nCheck For errors:");
1001
        sresult->failure++;
1002
        sresult->failureList.push_back("xmarkTest1");
1003 4ee4355d O'Neil Delpratt
        if (proc->exceptionOccurred()) {
1004
            cerr << proc->getErrorMessage() << endl;
1005 501beb26 O'Neil Delpratt
        }
1006
    }
1007
1008 72bf04c6 Norman Walsh
1009
}
1010
1011 501beb26 O'Neil Delpratt
1012 72bf04c6 Norman Walsh
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1013 501beb26 O'Neil Delpratt
void xmarkTest2(Xslt30Processor *proc, sResultCount *sresult) {
1014 4ee4355d O'Neil Delpratt
    cout << "Test: xmarkTest2 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1015 501beb26 O'Neil Delpratt
1016
1017
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1018
    if (result != NULL) {
1019
        cout << "XdmNode returned" << endl;
1020 4ee4355d O'Neil Delpratt
        sresult->success++;
1021
        delete result;
1022
1023 501beb26 O'Neil Delpratt
    } 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 72bf04c6 Norman Walsh
1035
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1036 501beb26 O'Neil Delpratt
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
1037 4ee4355d O'Neil Delpratt
    cout << "exampleSimple_xmark test - test q12.xsl:" << endl;
1038 72bf04c6 Norman Walsh
1039 501beb26 O'Neil Delpratt
    proc->setJustInTimeCompilation(true);
1040 72bf04c6 Norman Walsh
1041 501beb26 O'Neil Delpratt
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1042
    if (result != NULL) {
1043
        cout << "XdmNode returned" << endl;
1044 4ee4355d O'Neil Delpratt
        sresult->success++;
1045 a69dd173 O'Neil Delpratt
        delete result;
1046 501beb26 O'Neil Delpratt
    } else {
1047 4ee4355d O'Neil Delpratt
        printf("Result is null \nCheck For errors:");
1048 501beb26 O'Neil Delpratt
        if (proc->exceptionOccurred() > 0) {
1049
            cout << proc->getErrorMessage() << endl;
1050
        }
1051 4ee4355d O'Neil Delpratt
        sresult->failure++;
1052
        sresult->failureList.push_back("exampleSimple_xmark");
1053
        proc->exceptionClear();
1054 501beb26 O'Neil Delpratt
    }
1055 146cfacb O'Neil Delpratt
    proc->clearParameters();
1056 72bf04c6 Norman Walsh
1057 501beb26 O'Neil Delpratt
}
1058 72bf04c6 Norman Walsh
1059
/*
1060
* Test saving nd loading a Xslt package
1061
*/
1062 501beb26 O'Neil Delpratt
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1063
1064 4ee4355d O'Neil Delpratt
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1065 146cfacb O'Neil Delpratt
    trans->clearParameters();
1066 72bf04c6 Norman Walsh
1067
1068
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1069 501beb26 O'Neil Delpratt
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1070 4ee4355d O'Neil Delpratt
1071 9458d238 O'Neil Delpratt
    if (output== nullptr) {
1072 501beb26 O'Neil Delpratt
        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 72bf04c6 Norman Walsh
    }
1085 501beb26 O'Neil Delpratt
    fflush(stdout);
1086 72bf04c6 Norman Walsh
    delete output;
1087
}
1088
1089
1090
/*
1091
* Test saving and loading a Xslt package
1092
*/
1093 4ee4355d O'Neil Delpratt
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1094 501beb26 O'Neil Delpratt
1095 4ee4355d O'Neil Delpratt
    cout << endl << "Test: testPackage1a" << endl;
1096 146cfacb O'Neil Delpratt
    trans->clearParameters();
1097 72bf04c6 Norman Walsh
1098 4ee4355d O'Neil Delpratt
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1099 72bf04c6 Norman Walsh
1100 4ee4355d O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1101
    executable->setInitialMatchSelectionAsFile("cat.xml");
1102
    const char * output = executable->applyTemplatesReturningString();
1103 9458d238 O'Neil Delpratt
    if (output== nullptr) {
1104 4ee4355d O'Neil Delpratt
        printf("result is null \n");
1105 501beb26 O'Neil Delpratt
        const char *message = trans->getErrorMessage();
1106
        if (message != NULL) {
1107
            cout << "Error message =" << message << endl;
1108
        }
1109 4ee4355d O'Neil Delpratt
        sresult->failure++;
1110
        sresult->failureList.push_back("testPackage1a");
1111
1112 501beb26 O'Neil Delpratt
    } else {
1113
        printf("%s", output);
1114
        printf("result is OK \n");
1115 4ee4355d O'Neil Delpratt
        sresult->success++;
1116 72bf04c6 Norman Walsh
    }
1117 501beb26 O'Neil Delpratt
    fflush(stdout);
1118 72bf04c6 Norman Walsh
    delete output;
1119
}
1120
1121 4ee4355d O'Neil Delpratt
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 501beb26 O'Neil Delpratt
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 4ee4355d O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    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 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
1181
            SaxonApiException * exception = executable->getException();
1182
            cerr<<"Error: " << exception->getMessage() <<endl;
1183
            delete exception;
1184 501beb26 O'Neil Delpratt
        }
1185
        sresult->failure++;
1186
        sresult->failureList.push_back("testCallFunction");
1187
    }
1188
    delete valueArray[0];
1189
    delete valueArray[1];
1190 901b6eca O'Neil Delpratt
    delete [] valueArray;
1191 501beb26 O'Neil Delpratt
}
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 901b6eca O'Neil Delpratt
    if(executable == nullptr) {
1200
        if(trans->exceptionOccurred()) {
1201
            cout << "Error: "<< trans->getErrorMessage() << endl;
1202
        }
1203
        return;
1204
    }
1205 501beb26 O'Neil Delpratt
    XdmNode *node = proc->parseXmlFromString("<e/>");
1206
1207
    executable->setResultAsRawValue(false);
1208
    std::map<std::string, XdmValue *> parameterValues;
1209 4ee4355d O'Neil Delpratt
1210
    XdmAtomicValue * a1 = proc->makeIntegerValue(12);
1211
    XdmAtomicValue * a2 = proc->makeIntegerValue(5);;
1212
    parameterValues["a"] = a1;
1213
    parameterValues["b"] = a2;
1214 501beb26 O'Neil Delpratt
    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 4ee4355d O'Neil Delpratt
        delete result;
1221 501beb26 O'Neil Delpratt
    } else {
1222
        sresult->failure++;
1223 72bf04c6 Norman Walsh
    }
1224
1225 a69dd173 O'Neil Delpratt
    delete executable;
1226 4ee4355d O'Neil Delpratt
    delete a1;
1227
    delete a2;
1228 72bf04c6 Norman Walsh
}
1229
1230 501beb26 O'Neil Delpratt
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 901b6eca O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    XdmValue *value = executable->callTemplateReturningValue("go");
1245
1246
1247 9458d238 O'Neil Delpratt
    if (value== nullptr) {
1248 501beb26 O'Neil Delpratt
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 72bf04c6 Norman Walsh
}
1260
1261 501beb26 O'Neil Delpratt
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1262
    cout << endl << "Test: testContextNotRoot" << endl;
1263 72bf04c6 Norman Walsh
1264 501beb26 O'Neil Delpratt
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1265 72bf04c6 Norman Walsh
1266 501beb26 O'Neil Delpratt
    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 901b6eca O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    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 72bf04c6 Norman Walsh
1285 9458d238 O'Neil Delpratt
        if (result== nullptr) {
1286 72bf04c6 Norman Walsh
1287 501beb26 O'Neil Delpratt
            cout << "testCallFunction ======= FAIL ======" << endl;
1288 4ee4355d O'Neil Delpratt
            if(executable->exceptionOccurred()) {
1289
                SaxonApiException * exception = executable->getException();
1290
                cerr<<"Error: " << exception->getMessage() <<endl;
1291
                delete exception;
1292 501beb26 O'Neil Delpratt
            }
1293
            sresult->failure++;
1294
            sresult->failureList.push_back("testContextNotRoot");
1295
        } else {
1296 72bf04c6 Norman Walsh
1297 501beb26 O'Neil Delpratt
            cout << "testContextNotRoot = " << result << endl;
1298
            sresult->success++;
1299
        }
1300
    }
1301
}
1302 72bf04c6 Norman Walsh
1303 501beb26 O'Neil Delpratt
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 901b6eca O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    executable->setGlobalContextItem(node);
1322
    const char *result = executable->callTemplateReturningString("main");
1323
1324 9458d238 O'Neil Delpratt
    if (result== nullptr) {
1325 501beb26 O'Neil Delpratt
1326
        cout << "testCallFunction ======= FAIL ======" << endl;
1327 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
1328
            SaxonApiException * exception = executable->getException();
1329
            cerr<<"Error: " << exception->getMessage() <<endl;
1330
            delete exception;
1331 501beb26 O'Neil Delpratt
        }
1332
        sresult->failure++;
1333
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1334
    } else {
1335
1336
        cout << "testContextNotRoot = " << result << endl;
1337
        sresult->success++;
1338
    }
1339 72bf04c6 Norman Walsh
1340
}
1341
1342 501beb26 O'Neil Delpratt
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 901b6eca O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    executable->setGlobalContextItem(node);
1361
    XdmValue *result = executable->callTemplateReturningValue("main");
1362
1363 9458d238 O'Neil Delpratt
    if (result== nullptr) {
1364 501beb26 O'Neil Delpratt
1365
        cout << "testCallFunction ======= FAIL ======" << endl;
1366 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
1367
            SaxonApiException * exception = executable->getException();
1368
            cerr<<"Error: " << exception->getMessage() <<endl;
1369
            delete exception;
1370 501beb26 O'Neil Delpratt
        }
1371
        sresult->failure++;
1372
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1373
    } else {
1374
1375
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1376
        sresult->success++;
1377 72bf04c6 Norman Walsh
    }
1378
1379 501beb26 O'Neil Delpratt
}
1380 72bf04c6 Norman Walsh
1381 501beb26 O'Neil Delpratt
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1382 72bf04c6 Norman Walsh
1383 501beb26 O'Neil Delpratt
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1384
                                                            1);
1385 9458d238 O'Neil Delpratt
    if (fi== nullptr) {
1386 501beb26 O'Neil Delpratt
        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 72bf04c6 Norman Walsh
1406
    }
1407
1408 501beb26 O'Neil Delpratt
}
1409
1410
1411
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1412
    cout << endl << "Test: testPipeline" << endl;
1413 72bf04c6 Norman Walsh
1414 501beb26 O'Neil Delpratt
    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 901b6eca O'Neil Delpratt
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 501beb26 O'Neil Delpratt
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1428 72bf04c6 Norman Walsh
1429 501beb26 O'Neil Delpratt
    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 901b6eca O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    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 901b6eca O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    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 901b6eca O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    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 901b6eca O'Neil Delpratt
    if(stage5 == nullptr) {
1467 501beb26 O'Neil Delpratt
        sresult->failure++;
1468
        sresult->failureList.push_back("testPipeline");
1469 901b6eca O'Neil Delpratt
        if(trans->exceptionOccurred()) {
1470
            cout << "stage 5 Error: "<< trans->getErrorMessage() << endl;
1471
        }
1472 501beb26 O'Neil Delpratt
        return;
1473 72bf04c6 Norman Walsh
    }
1474
1475 501beb26 O'Neil Delpratt
    stage1->setProperty("!indent", "no");
1476
    stage1->setInitialMatchSelection(inn);
1477 72bf04c6 Norman Walsh
1478 501beb26 O'Neil Delpratt
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1479
    if (stage1->exceptionOccurred()) {
1480 901b6eca O'Neil Delpratt
        sresult->failure++;
1481
        sresult->failureList.push_back("testPipeline");
1482 4ee4355d O'Neil Delpratt
        if(stage1->exceptionOccurred()) {
1483
            SaxonApiException * exception = stage1->getException();
1484
            cerr<<"Error: " << exception->getMessage() <<endl;
1485
            delete exception;
1486
        }
1487 501beb26 O'Neil Delpratt
        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 9458d238 O'Neil Delpratt
    if (d11== nullptr) {
1497 72bf04c6 Norman Walsh
1498 501beb26 O'Neil Delpratt
        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 72bf04c6 Norman Walsh
1508 501beb26 O'Neil Delpratt
        return;
1509
    }
1510
    stage2->setProperty("!indent", "no");
1511
    stage2->setInitialMatchSelection(d1->getHead());
1512
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1513 9458d238 O'Neil Delpratt
    if (d2== nullptr) {
1514 501beb26 O'Neil Delpratt
        cout << "ERROR-11\n" << endl;
1515 4ee4355d O'Neil Delpratt
        if(stage2->exceptionOccurred()) {
1516
            SaxonApiException * exception = stage2->getException();
1517
            cerr<<"Error: " << exception->getMessage() <<endl;
1518
            delete exception;
1519 72bf04c6 Norman Walsh
        }
1520 501beb26 O'Neil Delpratt
        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 4ee4355d O'Neil Delpratt
        if(stage3->exceptionOccurred()) {
1531
            SaxonApiException * exception = stage3->getException();
1532
            cerr<<"Error: " << exception->getMessage() <<endl;
1533
            delete exception;
1534 901b6eca O'Neil Delpratt
        }
1535 501beb26 O'Neil Delpratt
      }
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 4ee4355d O'Neil Delpratt
        if(stage4->exceptionOccurred()) {
1543
            SaxonApiException * exception = stage4->getException();
1544
            cerr<<"Error: " << exception->getMessage() <<endl;
1545
            delete exception;
1546 901b6eca O'Neil Delpratt
        }
1547 501beb26 O'Neil Delpratt
        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 4ee4355d O'Neil Delpratt
        if(stage5->exceptionOccurred()) {
1556
            SaxonApiException * exception = stage5->getException();
1557
            cerr<<"Error: " << exception->getMessage() <<endl;
1558
            delete exception;
1559 901b6eca O'Neil Delpratt
        }
1560 501beb26 O'Neil Delpratt
        return;
1561
      }
1562
    cout<<sw<<endl;
1563
    cout << "testPipeline = " << sw << endl;
1564
    sresult->success++;
1565
1566
1567
}
1568
1569 72bf04c6 Norman Walsh
1570 9458d238 O'Neil Delpratt
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1571 72bf04c6 Norman Walsh
1572 00082f6f O'Neil Delpratt
1573 d0e6f8be O'Neil Delpratt
    cout << endl << "Test: testCatalog" << endl;
1574 9458d238 O'Neil Delpratt
    bool trace = false;
1575
    proc->setcwd(cwd);
1576 501beb26 O'Neil Delpratt
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1577
1578 9458d238 O'Neil Delpratt
    Xslt30Processor * trans = proc->newXslt30Processor();
1579
1580 501beb26 O'Neil Delpratt
    XsltExecutable  * executable = trans->compileFromFile("../php/catalog-test/test1.xsl");
1581 901b6eca O'Neil Delpratt
1582
    if(executable == nullptr) {
1583 00082f6f O'Neil Delpratt
1584 901b6eca O'Neil Delpratt
        if (trans->exceptionOccurred()) {
1585 00082f6f O'Neil Delpratt
1586
            if(trans->getErrorMessage() != nullptr) {
1587
                const char *message = trans->getErrorMessage();
1588 4ee4355d O'Neil Delpratt
                cerr << "exception=" << message << endl;
1589 00082f6f O'Neil Delpratt
            } else {
1590
                const char *message = proc->getErrorMessage();
1591
                if(message != nullptr) {
1592 4ee4355d O'Neil Delpratt
                    cerr << "exception-proc=" << message << endl;
1593 00082f6f O'Neil Delpratt
                }
1594
            }
1595 4ee4355d O'Neil Delpratt
        }
1596
        sresult->failure++;
1597
        sresult->failureList.push_back("testCatalog");
1598
        trans->exceptionClear();
1599 d0e6f8be O'Neil Delpratt
        return;
1600 901b6eca O'Neil Delpratt
    }
1601 a69dd173 O'Neil Delpratt
1602 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1603 4ee4355d O'Neil Delpratt
1604 501beb26 O'Neil Delpratt
    const char *result = executable->applyTemplatesReturningString();
1605
1606
    if(result != NULL) {
1607
        std::cerr << "testCatalog result= " << result << std::endl;
1608
    }
1609 4ee4355d O'Neil Delpratt
    sresult->success++; // TODO - check the results more carefully
1610
1611
    delete executable;
1612 501beb26 O'Neil Delpratt
1613
}
1614 72bf04c6 Norman Walsh
1615
1616
/*
1617 501beb26 O'Neil Delpratt
 *
1618
 *void *RunThread(void *args) {
1619 72bf04c6 Norman Walsh

1620 501beb26 O'Neil Delpratt
    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 72bf04c6 Norman Walsh

1626 501beb26 O'Neil Delpratt
    trans->attachThread();
1627

1628
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1629 72bf04c6 Norman Walsh

1630 501beb26 O'Neil Delpratt
    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 72bf04c6 Norman Walsh
}
1636

1637 501beb26 O'Neil Delpratt
void testThreads (SaxonProcessor * processor) {
1638
    pthread_t threads[NUM_THREADS];
1639
    int rc;
1640
    int i;
1641 72bf04c6 Norman Walsh

1642 501beb26 O'Neil Delpratt
    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 e9b06a56 O'Neil Delpratt
int main(int argc, char* argv[]) {
1667
1668
1669
    const char * cwd = nullptr;
1670
    if(argc > 0) {
1671 4bed9808 O'Neil Delpratt
        cwd = argv[1];
1672 e9b06a56 O'Neil Delpratt
    }
1673 501beb26 O'Neil Delpratt
1674
    SaxonProcessor *processor = new SaxonProcessor(true);
1675 fc47eaa6 O'Neil Delpratt
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
1676 501beb26 O'Neil Delpratt
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
1677 6b6a9706 O'Neil Delpratt
    
1678 e05bbe34 O'Neil Delpratt
    char buff[FILENAME_MAX]; //create string buffer to hold path
1679
    GetCurrentDir( buff, FILENAME_MAX );
1680 9458d238 O'Neil Delpratt
    cout<<"CWD = "<< buff<<endl;
1681 e9b06a56 O'Neil Delpratt
    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 501beb26 O'Neil Delpratt
    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 e05bbe34 O'Neil Delpratt
1691 501beb26 O'Neil Delpratt
    sResultCount *sresult = new sResultCount();
1692
    Xslt30Processor *trans = processor->newXslt30Processor();
1693 8d5b64f3 O'Neil Delpratt
1694
    if( trans == nullptr) {
1695 dfaaba21 O'Neil Delpratt
        cout<< "Error creating Xslt30Processor"<<endl;
1696 8d5b64f3 O'Neil Delpratt
        if(processor->exceptionOccurred()) {
1697 dfaaba21 O'Neil Delpratt
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
1698 8d5b64f3 O'Neil Delpratt
1699
        }
1700 dfaaba21 O'Neil Delpratt
        return -1;
1701 8d5b64f3 O'Neil Delpratt
    }
1702 501beb26 O'Neil Delpratt
    //testValidation(trans,sresult);
1703 a69dd173 O'Neil Delpratt
1704 501beb26 O'Neil Delpratt
    testInitialTemplate(processor, trans, sresult);
1705 a69dd173 O'Neil Delpratt
1706 4ee4355d O'Neil Delpratt
1707
1708 501beb26 O'Neil Delpratt
    exampleSimple1Err(trans, sresult);
1709 a69dd173 O'Neil Delpratt
1710 72bf04c6 Norman Walsh
    exampleSimple1(trans, sresult);
1711 e05bbe34 O'Neil Delpratt
1712 72bf04c6 Norman Walsh
    exampleSimple_xmark(trans, sresult);
1713 a69dd173 O'Neil Delpratt
1714 72bf04c6 Norman Walsh
    exampleSimple2(trans, sresult);
1715 a69dd173 O'Neil Delpratt
1716 72bf04c6 Norman Walsh
    exampleSimple3(processor, trans, sresult);
1717 a69dd173 O'Neil Delpratt
1718 4ee4355d O'Neil Delpratt
    exampleSimple3aError(processor, trans, sresult);
1719 a69dd173 O'Neil Delpratt
1720 72bf04c6 Norman Walsh
    testApplyTemplatesString1(trans, sresult);
1721 a69dd173 O'Neil Delpratt
1722 72bf04c6 Norman Walsh
    testApplyTemplatesString2(processor, trans, sresult);
1723
1724 4ee4355d O'Neil Delpratt
    testApplyTemplates2a_Error(processor, trans, sresult);
1725 72bf04c6 Norman Walsh
1726
    testTransformToString4(processor, trans, sresult);
1727 00082f6f O'Neil Delpratt
1728 9458d238 O'Neil Delpratt
    testCatalog(cwd, processor,  sresult);
1729 146cfacb O'Neil Delpratt
1730 a69dd173 O'Neil Delpratt
    testTransformToString2b(processor, trans, sresult);
1731 72bf04c6 Norman Walsh
1732
    testTransformToString3(processor, trans, sresult);
1733
        
1734
    testTransformFromstring(processor, trans, sresult);
1735
1736 a69dd173 O'Neil Delpratt
    testTransformFromstring2Err(processor, trans, sresult);
1737 72bf04c6 Norman Walsh
1738 501beb26 O'Neil Delpratt
    testTrackingOfValueReference(processor, trans, sresult);
1739 72bf04c6 Norman Walsh
1740 4ee4355d O'Neil Delpratt
    //testTrackingOfValueReferenceError(processor, trans, sresult);
1741 72bf04c6 Norman Walsh
1742
    testPackage1(trans, sresult);
1743
1744 4ee4355d O'Neil Delpratt
    testPackage1a(trans, sresult);
1745
1746
    testPackage2_Error(trans, sresult);
1747 72bf04c6 Norman Walsh
1748
    testXdmNodeOutput(trans, sresult);
1749
1750
    exampleParam(processor, trans, sresult);
1751
1752 4ee4355d O'Neil Delpratt
    //SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1753
1754
1755 72bf04c6 Norman Walsh
    xmarkTest1(trans, sresult);
1756
1757
    xmarkTest2(trans, sresult);
1758 4ee4355d O'Neil Delpratt
1759
    processor->release();
1760
    delete processor;
1761
1762
    return 0;
1763
1764 501beb26 O'Neil Delpratt
    testCallFunction(processor, trans, sresult);
1765 4ee4355d O'Neil Delpratt
1766
1767
1768 501beb26 O'Neil Delpratt
    testResolveUri(processor, trans, sresult);
1769 72bf04c6 Norman Walsh
    testContextNotRoot(processor, trans, sresult);
1770 501beb26 O'Neil Delpratt
    testContextNotRootNamedTemplate(processor, trans, sresult);
1771
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1772
    testPipeline(processor, sresult);
1773 72bf04c6 Norman Walsh
1774 501beb26 O'Neil Delpratt
    //Available in PE and EE
1775
    //testTransformToStringExtensionFunc(processor, trans);
1776 72bf04c6 Norman Walsh
1777
    delete trans;
1778 501beb26 O'Neil Delpratt
    delete processor;
1779 a69dd173 O'Neil Delpratt
    //processor->release();
1780 72bf04c6 Norman Walsh
1781
1782 501beb26 O'Neil Delpratt
    SaxonProcessor *processor2 = new SaxonProcessor(true);
1783 72bf04c6 Norman Walsh
1784 501beb26 O'Neil Delpratt
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1785
    testApplyTemplatesString1(trans2, sresult);
1786
    delete trans2;
1787
    processor2->release();
1788 72bf04c6 Norman Walsh
1789 501beb26 O'Neil Delpratt
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
1790
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
1791 72bf04c6 Norman Walsh
1792 501beb26 O'Neil Delpratt
    std::list<std::string>::iterator it;
1793
    std::cout << "Failed tests:" << std::endl;
1794 72bf04c6 Norman Walsh
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
1795 501beb26 O'Neil Delpratt
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
1796
        //Print the contents
1797
        std::cout << it->c_str() << std::endl;
1798 72bf04c6 Norman Walsh
1799 501beb26 O'Neil Delpratt
    }
1800 72bf04c6 Norman Walsh
1801
1802
    return 0;
1803
}