Project

Profile

Help

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

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

1

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

    
13
using namespace std;
14

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

    
19

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

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

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

    
49
        
50
    }
51

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

    
54
    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

    
75
}
76

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

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

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

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

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

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

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

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

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

    
145
}
146

    
147

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

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

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

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

    
192
}
193

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

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

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

    
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

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

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

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

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

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

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

    
264

    
265
        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
        }
292

    
293
    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
        
308
}
309

    
310

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

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

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

    
343
}
344

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

    
352

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

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

    
374
                                queryProc->exceptionClear();
375

    
376
                
377
                }
378
        
379
    }
380
    delete queryProc;
381

    
382
}
383

    
384
int main(int argc, char* argv[])
385
{
386

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

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

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

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

    
419
    cout<<endl<<"============================================================="<<endl<<endl;
420
    testDefaultNamespace(processor, query,sresult);
421

    
422
    cout<<endl<<"============================================================="<<endl<<endl;
423
    testReusability(processor, sresult);
424
    cout<<endl<<"============================================================="<<endl<<endl;
425
    testXQueryLineNumberError(query,sresult);
426
    cout<<endl<<"============================================================="<<endl<<endl;
427
    testXQueryLineNumber(processor, sresult);
428

    
429
    delete query;
430

    
431
    delete processor;
432
    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

    
452

    
453
#ifdef MEM_DEBUG
454
    SaxonProcessor::getInfo();
455
#endif
456
    return 0;
457
}
(25-25/28)