Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXQuery.cpp @ af4371f2

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