Project

Profile

Help

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

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

1
#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
#include "../../Saxon.C.API/XdmFunctionItem.h"
7
#include "../../Saxon.C.API/XdmMap.h"
8
#include "../../Saxon.C.API/XdmArray.h"
9
#include "CppTestUtils.h"
10
#include <string>
11

    
12
using namespace std;
13

    
14
#ifdef MEM_DEBUG
15
#define new new(__FILE__, __LINE__)
16
#endif
17

    
18

    
19
/*
20
* Test1: 
21
*/
22
void testxQuery1(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
23
    cout<<endl<<"Test testXQuery1:"<<endl;
24
    queryProc->clearParameters();
25
    queryProc->clearProperties();
26
   queryProc->setProperty("s", "cat.xml");
27

    
28
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
29

    
30
    const char * result = queryProc->runQueryToString();
31
    if(result != nullptr){
32
            cout<<"Result :"<<result<<endl;
33
            sresult->success++;
34
            delete result;
35
    } else {
36
        sresult->failure++;
37
        sresult->failureList.push_back("testXQuery1");
38
        if(queryProc->exceptionOccurred()) {
39
            SaxonApiException *exception = queryProc->getException();
40
            if(exception != nullptr) {
41
                cerr << "Error: " << exception->getMessage() << endl;
42
                delete exception;
43
                exception = nullptr;
44
            }
45
            queryProc->exceptionClear();
46
        }
47

    
48
        
49
    }
50

    
51
    queryProc->executeQueryToFile(nullptr, "catOutput.xml", nullptr);
52

    
53
    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

    
76
}
77

    
78
void testxQueryError(XQueryProcessor * queryProc, sResultCount *sresult){
79
 cout<<endl<<"Test testXQueryError-Test:"<<endl;
80
    queryProc->clearParameters();
81
    queryProc->clearProperties();
82
    //queryProc->setProperty("s", "cat.xml");
83

    
84
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
85

    
86
     queryProc->executeQueryToFile(nullptr, "catOutput.xml", nullptr);
87

    
88
                if (queryProc->exceptionOccurred()) {
89
            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

    
102
                }
103
    queryProc->clearParameters();
104
    queryProc->clearProperties();
105
}
106

    
107
void testXQueryError2(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
108
 cout<<endl<<"Test testXQueryError-test2:"<<endl;
109
        queryProc->clearProperties();
110
        queryProc->clearParameters();
111
   queryProc->setProperty("s", "cat.xml");
112

    
113
    queryProc->setProperty("qs", "<out>{count(/out/person)}<out>");
114

    
115
    const char * result = queryProc->runQueryToString();
116
   if(result != nullptr){
117
            cout<<"Result :"<<result<<endl;
118
            delete result;
119
       sresult->failure++;
120
       sresult->failureList.push_back("testXQueryError2");
121
    } else {
122

    
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
    }
143
    queryProc->clearProperties();
144
    queryProc->clearParameters();
145

    
146
}
147

    
148

    
149
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult) {
150
 cout<<endl<<"Test testDefaultNamespace:"<<endl;
151
        queryProc->clearProperties();
152
        queryProc->clearParameters();
153
        queryProc->declareNamespace("", "http://one.uri/");
154
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar/></foo>");
155

    
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
        queryProc->setContextItem((XdmItem *)input);
168
        queryProc->setQueryContent("/foo");
169

    
170
        XdmValue * value = queryProc->runQueryToValue();
171

    
172
        if(value != nullptr && value->size() == 1) {
173
            sresult->success++;
174
                cout<<"Test1: Result is ok size is "<<value->size()<<endl;
175
        delete value;
176
        } else {
177
        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
        }
189
    queryProc->clearProperties();
190
    queryProc->clearParameters();
191
        delete input;
192

    
193
}
194

    
195
// Test that the XQuery compiler can compile two queries without interference
196
void testReusability(SaxonProcessor * processor, sResultCount *sresult){
197
        cout<<endl<<"Test test XQuery reusability:"<<endl;
198
        XQueryProcessor * queryProc2 = processor->newXQueryProcessor();
199

    
200
        queryProc2->clearProperties();
201
        queryProc2->clearParameters();
202

    
203
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar xmlns='http://two.uri'>12</bar></foo>");
204

    
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

    
219
        queryProc2->declareNamespace("", "http://two.uri");
220
        queryProc2->setQueryContent("declare variable $p as xs:integer external; /*/bar + $p");
221

    
222
        queryProc2->setContextItem((XdmItem *)input);
223

    
224
        XdmAtomicValue * value1 = processor->makeBooleanValue(true);
225
          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
        }
256

    
257
        queryProc2->setContextItem((XdmItem *)input);
258

    
259
        XdmAtomicValue * value2 = processor->makeLongValue(6);
260
    cout<<"PrimitiveTypeName of  value2="<<value2->getPrimitiveTypeName()<<endl;
261
        queryProc2->setParameter("p",(XdmValue *)value2);
262

    
263
        XdmValue * val2 = queryProc2->runQueryToValue();
264

    
265

    
266
        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
        }
293

    
294
    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
        
309
}
310

    
311

    
312
//Test requirement of license file - Test should fail
313
void testXQueryLineNumberError(XQueryProcessor * queryProc, sResultCount *sresult){
314
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
315
        queryProc->clearProperties();
316
        queryProc->clearParameters();
317
   queryProc->setProperty("s", "cat.xml");
318

    
319
    queryProc->setProperty("qs", "saxon:line-number((//person)[1])");
320

    
321
    const char * result = queryProc->runQueryToString();
322
    if(result != nullptr){
323
        sresult->success++;
324
            cout<<"Result :"<<result<<endl;
325
            delete result;
326
    } else {
327
        if (queryProc->exceptionOccurred()) {
328
        sresult->failure++;
329
        sresult->failureList.push_back("testXQueryLineNumberError");
330
                    cout<<"Exception found. "<<endl;
331
        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
                
338
                }
339
        
340
    }
341
    queryProc->clearProperties();
342
    queryProc->clearParameters();
343

    
344
}
345

    
346
//Test requirement of license file - Test should succeed
347
void testXQueryLineNumber(SaxonProcessor * processor, sResultCount *sresult){
348
  //SaxonProcessor * processor = new SaxonProcessor(true);
349
  processor->setConfigurationProperty("l", "on");
350
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
351
 cout<<endl<<"testXQueryLineNumber:"<<endl;
352

    
353

    
354
    //queryProc->setQueryBaseURI(processor->getcwd());
355

    
356
    queryProc->setProperty("s", "cat.xml");
357
   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
    if(result != nullptr){
363
            cout<<"Result :"<<result<<endl;
364
            sresult->success++;
365
    } else {
366
        if (queryProc->exceptionOccurred()) {
367
        sresult->failure++;
368
        sresult->failureList.push_back("testXQueryLineNumber");
369
                    cout<<"Exception found."<<endl;
370
                                const char * message = queryProc->getErrorMessage();
371
                                if(message != nullptr) {
372
                                        cout<<"Error Message = "<<message<<endl;                
373
                                }
374

    
375
                                queryProc->exceptionClear();
376

    
377
                
378
                }
379
        
380
    }
381
        delete processor;
382
    delete queryProc;
383

    
384
}
385

    
386
int main(int argc, char* argv[])
387
{
388

    
389
    const char * cwd = nullptr;
390
    if(argc > 0) {
391
        cwd = argv[1];
392
    }
393

    
394
    SaxonProcessor * processor = new SaxonProcessor(false);
395
    cout<<"Test: XQueryProcessor with Saxon version="<<processor->version()<<endl<<endl;
396

    
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
    XQueryProcessor * query = processor->newXQueryProcessor();
408

    
409
    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
    testXQueryError2(processor, query,sresult);
420

    
421

    
422
#ifdef MEM_DEBUG
423
    SaxonProcessor::getInfo();
424
#endif
425
    cout<<endl<<"============================================================="<<endl<<endl;
426
    /*testDefaultNamespace(processor, query,sresult);
427

428
    cout<<endl<<"============================================================="<<endl<<endl;
429
    testReusability(processor, sresult);
430
    cout<<endl<<"============================================================="<<endl<<endl;
431
    testXQueryLineNumberError(query,sresult);
432
    cout<<endl<<"============================================================="<<endl<<endl;
433
    testXQueryLineNumber(processor, sresult);*/
434
cerr<<"testXquery cp0"<<endl;
435
    delete query;
436
    cerr<<"testXquery cp0"<<endl;
437

    
438
    delete processor;
439
    cerr<<"testXquery cp0"<<endl;
440
    processor->release();
441

    
442
#ifdef MEM_DEBUG
443
    SaxonProcessor::getInfo();
444
#endif
445

    
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

    
463

    
464
#ifdef MEM_DEBUG
465
    SaxonProcessor::getInfo();
466
#endif
467
    return 0;
468
}
(25-25/28)