Project

Profile

Help

Revision 01d6fdb6

Added by O'Neil Delpratt 5 months ago

Saxon/C development

View differences:

src/main/c/samples/cppTests/testXQuery.cpp
21 21
*/
22 22
void testxQuery1(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
23 23
    cout<<endl<<"Test testXQuery1:"<<endl;
24
	queryProc->clearProperties();
25
	queryProc->clearParameters(true);
24
    queryProc->clearParameters();
25
    queryProc->clearProperties();
26 26
   queryProc->setProperty("s", "cat.xml");
27 27

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

  
30 30
    const char * result = queryProc->runQueryToString();
31
    if(result != NULL){
31
    if(result != nullptr){
32 32
    	cout<<"Result :"<<result<<endl;
33
    	sresult->success++;
34
    	delete result;
33 35
    } else {
36
        sresult->failure++;
37
        sresult->failureList.push_back("testXQuery1");
34 38
        if(queryProc->exceptionOccurred()) {
35

  
36 39
            SaxonApiException *exception = queryProc->getException();
37
            cerr<< "Error: "<<exception->getMessage()<<endl;
38
            delete exception;
40
            if(exception != nullptr) {
41
                cerr << "Error: " << exception->getMessage() << endl;
42
                delete exception;
43
                exception = nullptr;
44
            }
45
            queryProc->exceptionClear();
39 46
        }
40 47

  
41 48
	
42 49
    }
43 50

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

  
45
	queryProc->setcwd(".");
46
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
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();
47 75

  
48
		if (CppTestUtils::exists("catOutput.xml")) {
49
		    cout<<"The file $filename exists"<<endl;;
50
		} else {
51
		    cout<<"The file $filename does not exist"<<endl;
52
		    processor->exceptionClear();
53
		}
54 76
}
55 77

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

  
62 84
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
63
queryProc->setcwd(".");
64
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
85

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

  
66 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");
67 101

  
68
		    cout<<"Exception found. "<<endl;
69
				const char * message = queryProc->getErrorMessage();
70
				if(message != NULL) {
71
					cout<<"Error Message = "<<message<<endl;		
72
				}
73
		
74 102
		}
103
    queryProc->clearParameters();
104
    queryProc->clearProperties();
75 105
}
76 106

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

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

  
85 115
    const char * result = queryProc->runQueryToString();
86
   if(result != NULL){
116
   if(result != nullptr){
87 117
    	cout<<"Result :"<<result<<endl;
118
    	delete result;
119
       sresult->failure++;
120
       sresult->failureList.push_back("testXQueryError2");
88 121
    } else {
89
	const char * message = queryProc->getErrorMessage();
90
	if(message != NULL) {
91
		cout<<"Error Message="<<message<<" Line number= "<<queryProc->getException()->getLineNumber()<<endl;
92
	} else {
93
		cout<<"Error Message - NULL check"<<endl;
94
	}
95
	
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

  
96 142
    }
143
    queryProc->clearProperties();
144
    queryProc->clearParameters();
97 145

  
98 146
}
99 147

  
100 148

  
101 149
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult) {
102
 cout<<endl<<"Test testXQuery2:"<<endl;
150
 cout<<endl<<"Test testDefaultNamespace:"<<endl;
103 151
	queryProc->clearProperties();
104
	queryProc->clearParameters(true);	
152
	queryProc->clearParameters();
105 153
	queryProc->declareNamespace("", "http://one.uri/");
106 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

  
107 167
	queryProc->setContextItem((XdmItem *)input);
108 168
	queryProc->setQueryContent("/foo");
109 169

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

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

  
118 193
}
119 194

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

  
125
	queryProc->clearProperties();
126
	queryProc->clearParameters(true);
200
	queryProc2->clearProperties();
201
	queryProc2->clearParameters();
127 202

  
128 203
 	XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar xmlns='http://two.uri'>12</bar></foo>");
129
	queryProc->declareNamespace("", "http://one.uri/");
130
	queryProc->setQueryContent("declare variable $p as xs:boolean external; exists(/foo) = $p");
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");
131 218

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

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

  
137 224
	XdmAtomicValue * value1 = processor->makeBooleanValue(true);
138
  	queryProc->setParameter("p",(XdmValue *)value1);
139
	XdmValue * val = queryProc->runQueryToValue();
140

  
141
	if(val != NULL && ((XdmItem*)val->itemAt(0))->isAtomic()){
142
		cout<<"Test1: Result is atomic"<<endl;
143
		XdmAtomicValue* atomic = (XdmAtomicValue *)val->itemAt(0);
144
		bool result1 = atomic->getBooleanValue();
145
		cout<<"Test2: Result value="<<(result1 == true ? "true" : "false")<<endl;
146
cout<<"Test5: PrimitiveTypeName of  atomic="<<atomic->getPrimitiveTypeName()<<endl;
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;
147 255
	}
148 256

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

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

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

  
157 265

  
158

  
159
cout<<"XdmValue size="<<val2->size()<<", "<<(val2->itemAt(0))->getStringValue()<<endl;
160

  
161
	if(val2 != NULL && ((XdmItem*)val2->itemAt(0))->isAtomic()){
162
		cout<<"Test3: Result is atomic"<<endl;
163
		XdmAtomicValue* atomic2 = (XdmAtomicValue *)(val2->itemAt(0));
164
		long result2 = atomic2->getLongValue();
165
		cout<<"Test4: Result value="<<result2<<endl;
166
		cout<<"Test5: PrimitiveTypeName of  atomic2="<<atomic2->getPrimitiveTypeName()<<endl;
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;
167 292
	}
168 293

  
169
if (queryProc->exceptionOccurred()) {
170
				const char * message = queryProc->getErrorMessage();
171
				if(message != NULL) {
172
					cout<<"Error Message = "<<message<<endl;		
173
				}		
174

  
175
		
176
		}
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;
177 308
	
178 309
}
179 310

  
......
182 313
void testXQueryLineNumberError(XQueryProcessor * queryProc, sResultCount *sresult){
183 314
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
184 315
	queryProc->clearProperties();
185
	queryProc->clearParameters(true);
316
	queryProc->clearParameters();
186 317
   queryProc->setProperty("s", "cat.xml");
187 318

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

  
190 321
    const char * result = queryProc->runQueryToString();
191
    if(result != NULL){
322
    if(result != nullptr){
323
        sresult->success++;
192 324
    	cout<<"Result :"<<result<<endl;
325
    	delete result;
193 326
    } else {
194
	cout<<"Some faiure"<<endl;
195 327
	if (queryProc->exceptionOccurred()) {
328
        sresult->failure++;
329
        sresult->failureList.push_back("testXQueryLineNumberError");
196 330
		    cout<<"Exception found. "<<endl;
197
		    const char * message = queryProc->getException()->getMessage();
198
				if(message != NULL) {
199
					cout<<"Error Message = "<<message<<endl;		
200
				}		
201

  
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
        }
202 337
		
203 338
		}
204 339
	
205 340
    }
341
    queryProc->clearProperties();
342
    queryProc->clearParameters();
206 343

  
207 344
}
208 345

  
209 346
//Test requirement of license file - Test should succeed
210
void testXQueryLineNumber(sResultCount *sresult){
211
  SaxonProcessor * processor = new SaxonProcessor(true);
347
void testXQueryLineNumber(SaxonProcessor * processor, sResultCount *sresult){
348
  //SaxonProcessor * processor = new SaxonProcessor(true);
212 349
  processor->setConfigurationProperty("l", "on");
213 350
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
214 351
 cout<<endl<<"testXQueryLineNumber:"<<endl;
215
	
216
   queryProc->setProperty("s", "cat.xml");
352

  
353

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

  
356
    queryProc->setProperty("s", "cat.xml");
217 357
   queryProc->declareNamespace("saxon","http://saxon.sf.net/");
218 358

  
219 359
    queryProc->setProperty("qs", "saxon:line-number(doc('cat.xml')/out/person[1])"); ///out/person[1]
220 360

  
221 361
    const char * result = queryProc->runQueryToString();
222
    if(result != NULL){
362
    if(result != nullptr){
223 363
    	cout<<"Result :"<<result<<endl;
364
    	sresult->success++;
224 365
    } else {
225
	cout<<"Some faiure"<<endl;
226 366
	if (queryProc->exceptionOccurred()) {
367
        sresult->failure++;
368
        sresult->failureList.push_back("testXQueryLineNumber");
227 369
		    cout<<"Exception found."<<endl;
228 370
				const char * message = queryProc->getErrorMessage();
229
				if(message != NULL) {
371
				if(message != nullptr) {
230 372
					cout<<"Error Message = "<<message<<endl;		
231
				}		
373
				}
374

  
375
				queryProc->exceptionClear();
232 376

  
233 377
		
234 378
		}
235 379
	
236 380
    }
237 381
	delete processor;
382
    delete queryProc;
238 383

  
239 384
}
240 385

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

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

  
244 394
    SaxonProcessor * processor = new SaxonProcessor(false);
245 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

  
246 407
    XQueryProcessor * query = processor->newXQueryProcessor();
247 408

  
248 409
    sResultCount *sresult = new sResultCount();
......
255 416

  
256 417
    cout<<endl<<"============================================================="<<endl<<endl;
257 418

  
258
    testxQueryError2(processor, query,sresult);
419
    testXQueryError2(processor, query,sresult);
259 420

  
421

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

  
263 428
    cout<<endl<<"============================================================="<<endl<<endl;
264
    testReusability(processor, query,sresult);
429
    testReusability(processor, sresult);
265 430
    cout<<endl<<"============================================================="<<endl<<endl;
266 431
    testXQueryLineNumberError(query,sresult);
267 432
    cout<<endl<<"============================================================="<<endl<<endl;
268
    testXQueryLineNumber(sresult);
269

  
270

  
433
    testXQueryLineNumber(processor, sresult);*/
434
cerr<<"testXquery cp0"<<endl;
271 435
    delete query;
436
    cerr<<"testXquery cp0"<<endl;
437

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

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

  
275 446
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
276 447

  
......
288 459

  
289 460
    delete sresult;
290 461

  
462

  
463

  
291 464
#ifdef MEM_DEBUG
292 465
    SaxonProcessor::getInfo();
293 466
#endif

Also available in: Unified diff