Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXQuery.cpp @ 55b80284

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 72bf04c6 Norman Walsh
        queryProc->clearProperties();
25
        queryProc->clearParameters(true);
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 != NULL){
32
            cout<<"Result :"<<result<<endl;
33
    } else {
34 55b80284 O'Neil Delpratt
        if(queryProc->exceptionOccurred()) {
35
36
            SaxonApiException *exception = queryProc->getException();
37
            cerr<< "Error: "<<exception->getMessage()<<endl;
38
            delete exception;
39
        }
40 72bf04c6 Norman Walsh
41
        
42
    }
43
44 55b80284 O'Neil Delpratt
45 72bf04c6 Norman Walsh
        queryProc->setcwd(".");
46
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
47
48 e05bbe34 O'Neil Delpratt
                if (CppTestUtils::exists("catOutput.xml")) {
49 72bf04c6 Norman Walsh
                    cout<<"The file $filename exists"<<endl;;
50
                } else {
51
                    cout<<"The file $filename does not exist"<<endl;
52
                    processor->exceptionClear();
53
                }
54
}
55
56 55b80284 O'Neil Delpratt
void testxQueryError(XQueryProcessor * queryProc, sResultCount *sresult){
57 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQueryError-Test:"<<endl;
58
        queryProc->clearProperties();
59
        queryProc->clearParameters(true);
60
   queryProc->setProperty("s", "cat.xml");
61
62
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
63
queryProc->setcwd(".");
64
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
65
66
                if (queryProc->exceptionOccurred()) {
67 79d12c83 O'Neil Delpratt
68
                    cout<<"Exception found. "<<endl;
69
                                const char * message = queryProc->getErrorMessage();
70 72bf04c6 Norman Walsh
                                if(message != NULL) {
71
                                        cout<<"Error Message = "<<message<<endl;                
72 79d12c83 O'Neil Delpratt
                                }
73 72bf04c6 Norman Walsh
                
74
                }
75
}
76
77 55b80284 O'Neil Delpratt
void testxQueryError2(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
78 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQueryError-test2:"<<endl;
79
        queryProc->clearProperties();
80
        queryProc->clearParameters(true);
81
   queryProc->setProperty("s", "cat.xml");
82
83
    queryProc->setProperty("qs", "<out>{count(/out/person)}<out>");
84
85
    const char * result = queryProc->runQueryToString();
86
   if(result != NULL){
87
            cout<<"Result :"<<result<<endl;
88
    } else {
89 79d12c83 O'Neil Delpratt
        const char * message = queryProc->getErrorMessage();
90 72bf04c6 Norman Walsh
        if(message != NULL) {
91 79d12c83 O'Neil Delpratt
                cout<<"Error Message="<<message<<" Line number= "<<queryProc->getException()->getLineNumber()<<endl;
92 72bf04c6 Norman Walsh
        } else {
93
                cout<<"Error Message - NULL check"<<endl;
94
        }
95
        
96
    }
97
98
}
99
100
101 55b80284 O'Neil Delpratt
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult) {
102 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQuery2:"<<endl;
103
        queryProc->clearProperties();
104
        queryProc->clearParameters(true);        
105
        queryProc->declareNamespace("", "http://one.uri/");
106
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar/></foo>");
107
        queryProc->setContextItem((XdmItem *)input);
108
        queryProc->setQueryContent("/foo");
109
110
        XdmValue * value = queryProc->runQueryToValue();
111
112
        if(value->size() == 1) {
113
                cout<<"Test1: Result is ok size is "<<value->size()<<endl;
114
        } else {
115
                cout<<"Test 1: Result failure"<<endl;
116
        }
117
118
}
119
120
// Test that the XQuery compiler can compile two queries without interference
121 55b80284 O'Neil Delpratt
void testReusability(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
122 72bf04c6 Norman Walsh
        cout<<endl<<"Test test XQuery reusability:"<<endl;
123
        XQueryProcessor * queryProc2 = processor->newXQueryProcessor();
124
125
        queryProc->clearProperties();
126
        queryProc->clearParameters(true);
127
128
         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");
131
132
        queryProc2->declareNamespace("", "http://two.uri");
133
        queryProc2->setQueryContent("declare variable $p as xs:integer external; /*/bar + $p");
134
135
        queryProc->setContextItem((XdmItem *)input);
136
137
        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;
147
        }
148
149
        queryProc2->setContextItem((XdmItem *)input);
150
151
        XdmAtomicValue * value2 = processor->makeLongValue(6);
152
cout<<"Test5: PrimitiveTypeName of  value2="<<value2->getPrimitiveTypeName()<<endl;
153
        queryProc2->setParameter("p",(XdmValue *)value2);
154
155
        XdmValue * val2 = queryProc2->runQueryToValue();
156
157
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;
167
        }
168
169
if (queryProc->exceptionOccurred()) {
170 79d12c83 O'Neil Delpratt
                                const char * message = queryProc->getErrorMessage();
171 72bf04c6 Norman Walsh
                                if(message != NULL) {
172
                                        cout<<"Error Message = "<<message<<endl;                
173
                                }                
174 79d12c83 O'Neil Delpratt
175 72bf04c6 Norman Walsh
                
176
                }
177
        
178
}
179
180
181
//Test requirement of license file - Test should fail
182 55b80284 O'Neil Delpratt
void testXQueryLineNumberError(XQueryProcessor * queryProc, sResultCount *sresult){
183 72bf04c6 Norman Walsh
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
184
        queryProc->clearProperties();
185
        queryProc->clearParameters(true);
186
   queryProc->setProperty("s", "cat.xml");
187
188
    queryProc->setProperty("qs", "saxon:line-number((//person)[1])");
189
190
    const char * result = queryProc->runQueryToString();
191
    if(result != NULL){
192
            cout<<"Result :"<<result<<endl;
193
    } else {
194
        cout<<"Some faiure"<<endl;
195
        if (queryProc->exceptionOccurred()) {
196 79d12c83 O'Neil Delpratt
                    cout<<"Exception found. "<<endl;
197
                    const char * message = queryProc->getException()->getMessage();
198 72bf04c6 Norman Walsh
                                if(message != NULL) {
199
                                        cout<<"Error Message = "<<message<<endl;                
200
                                }                
201 79d12c83 O'Neil Delpratt
202 72bf04c6 Norman Walsh
                
203
                }
204
        
205
    }
206
207
}
208
209
//Test requirement of license file - Test should succeed
210 55b80284 O'Neil Delpratt
void testXQueryLineNumber(sResultCount *sresult){
211 72bf04c6 Norman Walsh
  SaxonProcessor * processor = new SaxonProcessor(true);
212
  processor->setConfigurationProperty("l", "on");
213
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
214
 cout<<endl<<"testXQueryLineNumber:"<<endl;
215
        
216
   queryProc->setProperty("s", "cat.xml");
217
   queryProc->declareNamespace("saxon","http://saxon.sf.net/");
218
219
    queryProc->setProperty("qs", "saxon:line-number(doc('cat.xml')/out/person[1])"); ///out/person[1]
220
221
    const char * result = queryProc->runQueryToString();
222
    if(result != NULL){
223
            cout<<"Result :"<<result<<endl;
224
    } else {
225
        cout<<"Some faiure"<<endl;
226
        if (queryProc->exceptionOccurred()) {
227 79d12c83 O'Neil Delpratt
                    cout<<"Exception found."<<endl;
228
                                const char * message = queryProc->getErrorMessage();
229 72bf04c6 Norman Walsh
                                if(message != NULL) {
230
                                        cout<<"Error Message = "<<message<<endl;                
231
                                }                
232 79d12c83 O'Neil Delpratt
233 72bf04c6 Norman Walsh
                
234
                }
235
        
236
    }
237
        delete processor;
238
239
}
240
241 5d48327d O'Neil Delpratt
int main()
242 72bf04c6 Norman Walsh
{
243
244
    SaxonProcessor * processor = new SaxonProcessor(false);
245
    cout<<"Test: XQueryProcessor with Saxon version="<<processor->version()<<endl<<endl;
246
    XQueryProcessor * query = processor->newXQueryProcessor();
247
248 55b80284 O'Neil Delpratt
    sResultCount *sresult = new sResultCount();
249
250
    testxQuery1(processor, query, sresult);
251
252
    cout<<endl<<"============================================================="<<endl<<endl;
253
254
    testxQueryError(query,sresult);
255
256
    cout<<endl<<"============================================================="<<endl<<endl;
257
258
    testxQueryError2(processor, query,sresult);
259
260
    cout<<endl<<"============================================================="<<endl<<endl;
261
    testDefaultNamespace(processor, query,sresult);
262
263
    cout<<endl<<"============================================================="<<endl<<endl;
264
    testReusability(processor, query,sresult);
265
    cout<<endl<<"============================================================="<<endl<<endl;
266
    testXQueryLineNumberError(query,sresult);
267
    cout<<endl<<"============================================================="<<endl<<endl;
268
    testXQueryLineNumber(sresult);
269
270 72bf04c6 Norman Walsh
271
    delete query;
272 55b80284 O'Neil Delpratt
    processor->release();
273
274
275
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
276
277
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
278
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
279
280
    std::list<std::string>::iterator it;
281
    std::cout << "Failed tests:" << std::endl;
282
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
283
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
284
        //Print the contents
285
        std::cout << it->c_str() << std::endl;
286
287
    }
288
289
    delete sresult;
290
291
#ifdef MEM_DEBUG
292
    SaxonProcessor::getInfo();
293
#endif
294 72bf04c6 Norman Walsh
    return 0;
295
}