Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXQuery.cpp @ 01d6fdb6

1 72bf04c6 Norman Walsh
#include "../../Saxon.C.API/SaxonProcessor.h"
2
#include "../../Saxon.C.API/XdmValue.h"
3
#include "../../Saxon.C.API/XdmItem.h"
4
#include "../../Saxon.C.API/XdmNode.h"
5
#include "../../Saxon.C.API/XdmAtomicValue.h"
6 55b80284 O'Neil Delpratt
#include "../../Saxon.C.API/XdmFunctionItem.h"
7
#include "../../Saxon.C.API/XdmMap.h"
8
#include "../../Saxon.C.API/XdmArray.h"
9 e05bbe34 O'Neil Delpratt
#include "CppTestUtils.h"
10 72bf04c6 Norman Walsh
#include <string>
11
12
using namespace std;
13
14 55b80284 O'Neil Delpratt
#ifdef MEM_DEBUG
15
#define new new(__FILE__, __LINE__)
16
#endif
17 72bf04c6 Norman Walsh
18
19
/*
20
* Test1: 
21
*/
22 55b80284 O'Neil Delpratt
void testxQuery1(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
23
    cout<<endl<<"Test testXQuery1:"<<endl;
24 01d6fdb6 O'Neil Delpratt
    queryProc->clearParameters();
25
    queryProc->clearProperties();
26 72bf04c6 Norman Walsh
   queryProc->setProperty("s", "cat.xml");
27
28
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
29
30
    const char * result = queryProc->runQueryToString();
31 01d6fdb6 O'Neil Delpratt
    if(result != nullptr){
32 72bf04c6 Norman Walsh
            cout<<"Result :"<<result<<endl;
33 01d6fdb6 O'Neil Delpratt
            sresult->success++;
34
            delete result;
35 72bf04c6 Norman Walsh
    } else {
36 01d6fdb6 O'Neil Delpratt
        sresult->failure++;
37
        sresult->failureList.push_back("testXQuery1");
38 55b80284 O'Neil Delpratt
        if(queryProc->exceptionOccurred()) {
39
            SaxonApiException *exception = queryProc->getException();
40 01d6fdb6 O'Neil Delpratt
            if(exception != nullptr) {
41
                cerr << "Error: " << exception->getMessage() << endl;
42
                delete exception;
43
                exception = nullptr;
44
            }
45
            queryProc->exceptionClear();
46 55b80284 O'Neil Delpratt
        }
47 72bf04c6 Norman Walsh
48
        
49
    }
50
51 01d6fdb6 O'Neil Delpratt
    queryProc->executeQueryToFile(nullptr, "catOutput.xml", nullptr);
52 55b80284 O'Neil Delpratt
53 01d6fdb6 O'Neil Delpratt
    if(queryProc->exceptionOccurred()) {
54
        SaxonApiException *exception = queryProc->getException();
55
        if(exception != nullptr) {
56
            cerr << "Error: " << exception->getMessage() << endl;
57
        }
58
        queryProc->exceptionClear();
59
    }
60
61
    if (CppTestUtils::exists("catOutput.xml")) {
62
        cout<<"The file $filename exists"<<endl;
63
        sresult->success++;
64
        remove("catOutput.xml");
65
    } else {
66
        sresult->failure++;
67
        sresult->failureList.push_back("testXQuery1");
68
        cout<<"The file $filename does not exist"<<endl;
69
70
    }
71
    cerr<<"testXQuery1 cp0"<<endl;
72
    queryProc->clearParameters();
73
    cerr<<"testXQuery1 cp0"<<endl;
74
    queryProc->clearProperties();
75 72bf04c6 Norman Walsh
76
}
77
78 55b80284 O'Neil Delpratt
void testxQueryError(XQueryProcessor * queryProc, sResultCount *sresult){
79 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQueryError-Test:"<<endl;
80 01d6fdb6 O'Neil Delpratt
    queryProc->clearParameters();
81
    queryProc->clearProperties();
82
    //queryProc->setProperty("s", "cat.xml");
83 72bf04c6 Norman Walsh
84
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
85 01d6fdb6 O'Neil Delpratt
86
     queryProc->executeQueryToFile(nullptr, "catOutput.xml", nullptr);
87 72bf04c6 Norman Walsh
88
                if (queryProc->exceptionOccurred()) {
89 01d6fdb6 O'Neil Delpratt
            cout << "Exception found. " << endl;
90
            SaxonApiException * exception = queryProc->getException();
91
            if(exception != nullptr) {
92
                sresult->success++;
93
                const char *message = exception->getMessage();
94
                cout << "Error Message = " << message << endl;
95
            }
96
            queryProc->exceptionClear();
97
                } else {
98
99
            sresult->failure++;
100
            sresult->failureList.push_back("testXQueryError");
101 79d12c83 O'Neil Delpratt
102 72bf04c6 Norman Walsh
                }
103 01d6fdb6 O'Neil Delpratt
    queryProc->clearParameters();
104
    queryProc->clearProperties();
105 72bf04c6 Norman Walsh
}
106
107 01d6fdb6 O'Neil Delpratt
void testXQueryError2(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
108 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQueryError-test2:"<<endl;
109
        queryProc->clearProperties();
110 01d6fdb6 O'Neil Delpratt
        queryProc->clearParameters();
111 72bf04c6 Norman Walsh
   queryProc->setProperty("s", "cat.xml");
112
113
    queryProc->setProperty("qs", "<out>{count(/out/person)}<out>");
114
115
    const char * result = queryProc->runQueryToString();
116 01d6fdb6 O'Neil Delpratt
   if(result != nullptr){
117 72bf04c6 Norman Walsh
            cout<<"Result :"<<result<<endl;
118 01d6fdb6 O'Neil Delpratt
            delete result;
119
       sresult->failure++;
120
       sresult->failureList.push_back("testXQueryError2");
121 72bf04c6 Norman Walsh
    } else {
122 01d6fdb6 O'Neil Delpratt
123
       if (queryProc->exceptionOccurred()) {
124
           SaxonApiException * exception = queryProc->getException();
125
           if(exception != nullptr) {
126
               sresult->success++;
127
               cout << "Exception found. " << endl;
128
               const char *message = queryProc->getErrorMessage();
129
               cout << "Error Message = " << message << endl;
130
131
           } else {
132
               cout << "Exception obj is NULL !!!!. " << endl;
133
134
           }
135
           queryProc->exceptionClear();
136
       } else {
137
138
           sresult->failure++;
139
           sresult->failureList.push_back("testXQueryError2");
140
       }
141
142 72bf04c6 Norman Walsh
    }
143 01d6fdb6 O'Neil Delpratt
    queryProc->clearProperties();
144
    queryProc->clearParameters();
145 72bf04c6 Norman Walsh
146
}
147
148
149 55b80284 O'Neil Delpratt
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult) {
150 01d6fdb6 O'Neil Delpratt
 cout<<endl<<"Test testDefaultNamespace:"<<endl;
151 72bf04c6 Norman Walsh
        queryProc->clearProperties();
152 01d6fdb6 O'Neil Delpratt
        queryProc->clearParameters();
153 72bf04c6 Norman Walsh
        queryProc->declareNamespace("", "http://one.uri/");
154
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar/></foo>");
155 01d6fdb6 O'Neil Delpratt
156
    if(input == nullptr) {
157
        cout << "Source document is null." << endl;
158
        if(processor->exceptionOccurred()) {
159
            cerr<<processor->getErrorMessage()<<endl;
160
        }
161
        sresult->failure++;
162
        sresult->failureList.push_back("testDefaultNamespace");
163
        return;
164
165
    }
166
167 72bf04c6 Norman Walsh
        queryProc->setContextItem((XdmItem *)input);
168
        queryProc->setQueryContent("/foo");
169
170
        XdmValue * value = queryProc->runQueryToValue();
171
172 01d6fdb6 O'Neil Delpratt
        if(value != nullptr && value->size() == 1) {
173
            sresult->success++;
174 72bf04c6 Norman Walsh
                cout<<"Test1: Result is ok size is "<<value->size()<<endl;
175 01d6fdb6 O'Neil Delpratt
        delete value;
176 72bf04c6 Norman Walsh
        } else {
177 01d6fdb6 O'Neil Delpratt
        if (queryProc->exceptionOccurred()) {
178
            sresult->failure++;
179
            sresult->failureList.push_back("testDefaultNamespace");
180
            SaxonApiException * exception = queryProc->getException();
181
            if(exception != nullptr) {
182
                cout << "Exception found. " << endl;
183
                const char *message = queryProc->getErrorMessage();
184
                cout << "Error Message = " << message << endl;
185
                queryProc->exceptionClear();
186
            }
187
        }
188 72bf04c6 Norman Walsh
        }
189 01d6fdb6 O'Neil Delpratt
    queryProc->clearProperties();
190
    queryProc->clearParameters();
191
        delete input;
192 72bf04c6 Norman Walsh
193
}
194
195
// Test that the XQuery compiler can compile two queries without interference
196 01d6fdb6 O'Neil Delpratt
void testReusability(SaxonProcessor * processor, sResultCount *sresult){
197 72bf04c6 Norman Walsh
        cout<<endl<<"Test test XQuery reusability:"<<endl;
198
        XQueryProcessor * queryProc2 = processor->newXQueryProcessor();
199
200 01d6fdb6 O'Neil Delpratt
        queryProc2->clearProperties();
201
        queryProc2->clearParameters();
202 72bf04c6 Norman Walsh
203
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar xmlns='http://two.uri'>12</bar></foo>");
204 01d6fdb6 O'Neil Delpratt
205
         if(input == nullptr) {
206
        cout << "Source document is null." << endl;
207
        if(processor->exceptionOccurred()) {
208
            cerr<<processor->getErrorMessage()<<endl;
209
        }
210
             sresult->failure++;
211
             sresult->failureList.push_back("testReusability");
212
             return;
213
214
         }
215
216
        queryProc2->declareNamespace("", "http://one.uri/");
217
        queryProc2->setQueryContent("declare variable $p as xs:boolean external; exists(/foo) = $p");
218 72bf04c6 Norman Walsh
219
        queryProc2->declareNamespace("", "http://two.uri");
220
        queryProc2->setQueryContent("declare variable $p as xs:integer external; /*/bar + $p");
221
222 01d6fdb6 O'Neil Delpratt
        queryProc2->setContextItem((XdmItem *)input);
223 72bf04c6 Norman Walsh
224
        XdmAtomicValue * value1 = processor->makeBooleanValue(true);
225 01d6fdb6 O'Neil Delpratt
          queryProc2->setParameter("p",(XdmValue *)value1);
226
        XdmValue * val = queryProc2->runQueryToValue();
227
228
        if(val != nullptr){
229
            if( ((XdmItem*)val->itemAt(0))->isAtomic()) {
230
                sresult->success++;
231
            cout << "Test1: Result is atomic" << endl;
232
            XdmAtomicValue *atomic = (XdmAtomicValue *) val->itemAt(0);
233
            bool result1 = atomic->getBooleanValue();
234
            cout << "Test2: Result value=" << (result1 == true ? "true" : "false") << endl;
235
            cout << "PrimitiveTypeName of  atomic=" << atomic->getPrimitiveTypeName() << endl;
236
        } else {
237
            sresult->failure++;
238
            sresult->failureList.push_back("testReusability");
239
            }
240
            delete val;
241
        } else {
242
        if (queryProc2->exceptionOccurred()) {
243
            sresult->failure++;
244
            sresult->failureList.push_back("testReusability");
245
            SaxonApiException * exception = queryProc2->getException();
246
            if(exception != nullptr) {
247
                cout << "Exception found. " << endl;
248
                const char *message = queryProc2->getErrorMessage();
249
                cout << "Error Message = " << message << endl;
250
                queryProc2->exceptionClear();
251
            }
252
        }
253
254
        return;
255 72bf04c6 Norman Walsh
        }
256
257
        queryProc2->setContextItem((XdmItem *)input);
258
259
        XdmAtomicValue * value2 = processor->makeLongValue(6);
260 01d6fdb6 O'Neil Delpratt
    cout<<"PrimitiveTypeName of  value2="<<value2->getPrimitiveTypeName()<<endl;
261 72bf04c6 Norman Walsh
        queryProc2->setParameter("p",(XdmValue *)value2);
262
263
        XdmValue * val2 = queryProc2->runQueryToValue();
264
265
266 01d6fdb6 O'Neil Delpratt
        if(val2 != nullptr){
267
        cout<<"XdmValue size="<<val2->size()<<", "<<(val2->itemAt(0))->getStringValue()<<endl;
268
            if(((XdmItem*)val2->itemAt(0))->isAtomic()) {
269
            sresult->success++;
270
            cout << "Test3: Result is atomic" << endl;
271
            XdmAtomicValue *atomic2 = (XdmAtomicValue *) (val2->itemAt(0));
272
            long result2 = atomic2->getLongValue();
273
            cout << "Result value=" << result2 << endl;
274
            cout << "PrimitiveTypeName of  atomic2=" << atomic2->getPrimitiveTypeName() << endl;
275
        }
276
            delete val2;
277
        } else {
278
        if (queryProc2->exceptionOccurred()) {
279
            sresult->failure++;
280
            sresult->failureList.push_back("testReusability-2");
281
            SaxonApiException * exception = queryProc2->getException();
282
            if(exception != nullptr) {
283
                cout << "Exception found. " << endl;
284
                const char *message = queryProc2->getErrorMessage();
285
                cout << "Error Message = " << message << endl;
286
                queryProc2->exceptionClear();
287
            }
288
        }
289
            delete value2;
290
            delete queryProc2;
291
            return;
292 72bf04c6 Norman Walsh
        }
293
294 01d6fdb6 O'Neil Delpratt
    if (queryProc2->exceptionOccurred()) {
295
        sresult->failure++;
296
        sresult->failureList.push_back("testReusability-3");
297
        SaxonApiException * exception = queryProc2->getException();
298
        if(exception != nullptr) {
299
            cout << "Exception found. " << endl;
300
            const char *message = queryProc2->getErrorMessage();
301
            cout << "Error Message = " << message << endl;
302
            queryProc2->exceptionClear();
303
        }
304
    }
305
    delete value2;
306
    delete input;
307
    delete queryProc2;
308 72bf04c6 Norman Walsh
        
309
}
310
311
312
//Test requirement of license file - Test should fail
313 55b80284 O'Neil Delpratt
void testXQueryLineNumberError(XQueryProcessor * queryProc, sResultCount *sresult){
314 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
315
        queryProc->clearProperties();
316 01d6fdb6 O'Neil Delpratt
        queryProc->clearParameters();
317 72bf04c6 Norman Walsh
   queryProc->setProperty("s", "cat.xml");
318
319
    queryProc->setProperty("qs", "saxon:line-number((//person)[1])");
320
321
    const char * result = queryProc->runQueryToString();
322 01d6fdb6 O'Neil Delpratt
    if(result != nullptr){
323
        sresult->success++;
324 72bf04c6 Norman Walsh
            cout<<"Result :"<<result<<endl;
325 01d6fdb6 O'Neil Delpratt
            delete result;
326 72bf04c6 Norman Walsh
    } else {
327
        if (queryProc->exceptionOccurred()) {
328 01d6fdb6 O'Neil Delpratt
        sresult->failure++;
329
        sresult->failureList.push_back("testXQueryLineNumberError");
330 79d12c83 O'Neil Delpratt
                    cout<<"Exception found. "<<endl;
331 01d6fdb6 O'Neil Delpratt
        SaxonApiException * exception = queryProc->getException();
332
        if(exception != nullptr) {
333
            const char *message = queryProc->getErrorMessage();
334
            cout << "Error Message = " << message << endl;
335
            queryProc->exceptionClear();
336
        }
337 72bf04c6 Norman Walsh
                
338
                }
339
        
340
    }
341 01d6fdb6 O'Neil Delpratt
    queryProc->clearProperties();
342
    queryProc->clearParameters();
343 72bf04c6 Norman Walsh
344
}
345
346
//Test requirement of license file - Test should succeed
347 01d6fdb6 O'Neil Delpratt
void testXQueryLineNumber(SaxonProcessor * processor, sResultCount *sresult){
348
  //SaxonProcessor * processor = new SaxonProcessor(true);
349 72bf04c6 Norman Walsh
  processor->setConfigurationProperty("l", "on");
350
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
351
 cout<<endl<<"testXQueryLineNumber:"<<endl;
352 01d6fdb6 O'Neil Delpratt
353
354
    //queryProc->setQueryBaseURI(processor->getcwd());
355
356
    queryProc->setProperty("s", "cat.xml");
357 72bf04c6 Norman Walsh
   queryProc->declareNamespace("saxon","http://saxon.sf.net/");
358
359
    queryProc->setProperty("qs", "saxon:line-number(doc('cat.xml')/out/person[1])"); ///out/person[1]
360
361
    const char * result = queryProc->runQueryToString();
362 01d6fdb6 O'Neil Delpratt
    if(result != nullptr){
363 72bf04c6 Norman Walsh
            cout<<"Result :"<<result<<endl;
364 01d6fdb6 O'Neil Delpratt
            sresult->success++;
365 72bf04c6 Norman Walsh
    } else {
366
        if (queryProc->exceptionOccurred()) {
367 01d6fdb6 O'Neil Delpratt
        sresult->failure++;
368
        sresult->failureList.push_back("testXQueryLineNumber");
369 79d12c83 O'Neil Delpratt
                    cout<<"Exception found."<<endl;
370
                                const char * message = queryProc->getErrorMessage();
371 01d6fdb6 O'Neil Delpratt
                                if(message != nullptr) {
372 72bf04c6 Norman Walsh
                                        cout<<"Error Message = "<<message<<endl;                
373 01d6fdb6 O'Neil Delpratt
                                }
374
375
                                queryProc->exceptionClear();
376 79d12c83 O'Neil Delpratt
377 72bf04c6 Norman Walsh
                
378
                }
379
        
380
    }
381
        delete processor;
382 01d6fdb6 O'Neil Delpratt
    delete queryProc;
383 72bf04c6 Norman Walsh
384
}
385
386 01d6fdb6 O'Neil Delpratt
int main(int argc, char* argv[])
387 72bf04c6 Norman Walsh
{
388
389 01d6fdb6 O'Neil Delpratt
    const char * cwd = nullptr;
390
    if(argc > 0) {
391
        cwd = argv[1];
392
    }
393
394 72bf04c6 Norman Walsh
    SaxonProcessor * processor = new SaxonProcessor(false);
395
    cout<<"Test: XQueryProcessor with Saxon version="<<processor->version()<<endl<<endl;
396 01d6fdb6 O'Neil Delpratt
397
    char buff[FILENAME_MAX]; //create string buffer to hold path
398
    GetCurrentDir( buff, FILENAME_MAX );
399
    cout<<"CWD = "<< buff<<endl;
400
    if(cwd != nullptr) {
401
        processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
402
    } else {
403
        processor->setcwd(buff);
404
    }
405
406
407 72bf04c6 Norman Walsh
    XQueryProcessor * query = processor->newXQueryProcessor();
408
409 55b80284 O'Neil Delpratt
    sResultCount *sresult = new sResultCount();
410
411
    testxQuery1(processor, query, sresult);
412
413
    cout<<endl<<"============================================================="<<endl<<endl;
414
415
    testxQueryError(query,sresult);
416
417
    cout<<endl<<"============================================================="<<endl<<endl;
418
419 01d6fdb6 O'Neil Delpratt
    testXQueryError2(processor, query,sresult);
420 55b80284 O'Neil Delpratt
421 01d6fdb6 O'Neil Delpratt
422
#ifdef MEM_DEBUG
423
    SaxonProcessor::getInfo();
424
#endif
425 55b80284 O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
426 01d6fdb6 O'Neil Delpratt
    /*testDefaultNamespace(processor, query,sresult);
427 55b80284 O'Neil Delpratt

428
    cout<<endl<<"============================================================="<<endl<<endl;
429 01d6fdb6 O'Neil Delpratt
    testReusability(processor, sresult);
430 55b80284 O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
431
    testXQueryLineNumberError(query,sresult);
432
    cout<<endl<<"============================================================="<<endl<<endl;
433 01d6fdb6 O'Neil Delpratt
    testXQueryLineNumber(processor, sresult);*/
434
cerr<<"testXquery cp0"<<endl;
435 72bf04c6 Norman Walsh
    delete query;
436 01d6fdb6 O'Neil Delpratt
    cerr<<"testXquery cp0"<<endl;
437
438
    delete processor;
439
    cerr<<"testXquery cp0"<<endl;
440 55b80284 O'Neil Delpratt
    processor->release();
441
442 01d6fdb6 O'Neil Delpratt
#ifdef MEM_DEBUG
443
    SaxonProcessor::getInfo();
444
#endif
445 55b80284 O'Neil Delpratt
446
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
447
448
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
449
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
450
451
    std::list<std::string>::iterator it;
452
    std::cout << "Failed tests:" << std::endl;
453
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
454
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
455
        //Print the contents
456
        std::cout << it->c_str() << std::endl;
457
458
    }
459
460
    delete sresult;
461
462 01d6fdb6 O'Neil Delpratt
463
464 55b80284 O'Neil Delpratt
#ifdef MEM_DEBUG
465
    SaxonProcessor::getInfo();
466
#endif
467 72bf04c6 Norman Walsh
    return 0;
468
}