Project

Profile

Help

Revision 5d25f10d

Added by O'Neil Delpratt 5 months ago

Saxon/C development - XPath tests

View differences:

src/main/c/samples/cppTests/testXPath.cpp
12 12
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
13 13
void testXPathSingle(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
14 14

  
15
       cout<<endl<<"Test testXPathSingle:"<<endl; 
16
      XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
15
    cout<<endl<<"Test testXPathSingle:"<<endl;
16
    XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
17 17

  
18 18
	xpath->setContextItem((XdmItem *)input);
19 19
	XdmItem * result = xpath->evaluateSingle("//person[1]");
20 20
	
21
	if(result == NULL) {
21
	if(result == nullptr) {
22 22
		 printf("result is null \n");
23
        SaxonApiException * exception = xpath->getException();
24
        if(exception != nullptr) {
25
            const char *message = xpath->getErrorMessage();
26
            cout << "Error Message = " << message << endl;
27
            xpath->exceptionClear();
28
        }
23 29
        sresult->failure++;
24 30
        sresult->failureList.push_back("testXPathSingle");
25 31

  
26 32
	} else {
27 33
		cout<<"Number of items="<<result->size()<<endl;
28 34
		cout<<"String Value of result="<<result->getStringValue()<<endl;
35
		sresult->success++;
29 36
	}
30 37
	delete result;
31 38

  
......
33 40

  
34 41
}
35 42

  
36
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
43
// Test case on the evaluate method in XPathProcessor. Here we test that we have more than one XdmItem.
37 44
void testXPathValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
38 45
     cout<<endl<<"Test testXPathValues:"<<endl; 
39
  	xpath->clearParameters(true);
46
  	xpath->clearParameters();
40 47
  	xpath->clearProperties();
41 48
	 XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person1>text3</person1></out>");
42
	if(input == NULL) {return;}
49
	if(input == nullptr) {
50
        SaxonApiException * exception = xpath->getException();
51
        if(exception != nullptr) {
52
            const char *message = xpath->getErrorMessage();
53
            cout << "Error Message = " << message << endl;
54
            xpath->exceptionClear();
55
        }
56
        sresult->failure++;
57
        sresult->failureList.push_back("testXPathValues");
58

  
59
	    return;
60

  
61
	}
62

  
43 63
	XdmNode ** children = input->getChildren();
44 64
	XdmNode ** children0 = children[0]->getChildren();
45 65
	int num = children[0]->getChildCount();
46
	delete []children;
66

  
47 67
	cout<<"number of children: "<<num<<endl;
48 68
	for(int i=0; i<num;i++) {
49 69
		cout<<"node name:"<<children0[i]->getNodeName()<<endl;
50 70
	}
71

  
72
    delete []children;
51 73
	delete [] children0;
52 74

  
53 75
	xpath->setContextItem((XdmItem *)input);
54 76
	XdmValue * resultValues = xpath->evaluate("//person");
55 77
	
56
	if(resultValues == NULL) {
78
	if(resultValues == nullptr) {
57 79
		 printf("result is null \n");
80
        SaxonApiException * exception = xpath->getException();
81
        if(exception != nullptr) {
82
            const char *message = xpath->getErrorMessage();
83
            cout << "Error Message = " << message << endl;
84
            xpath->exceptionClear();
85
        }
86
        sresult->failure++;
87
        sresult->failureList.push_back("testXPathValues");
88

  
58 89
	} else {
59 90
		cout<<"Number of items="<<resultValues->size()<<endl;
91
		bool nullFound = false;
60 92
		for(int i =0; i< resultValues->size();i++){
61 93
			XdmItem * itemi = resultValues->itemAt(i);
62
			if(itemi == NULL) {
94
			if(itemi == nullptr) {
63 95
				cout<<"Item at position "<<i<<" should not be null"<<endl;
96
				nullFound = true;
64 97
				break;
65 98
			}
66 99
			cout<<"Item at "<<i<<" ="<<itemi->getStringValue()<<endl;		
67 100
		}
101
        delete resultValues;
102
		if(nullFound) {
103
            sresult->success++;
104
        } else {
105
		    sresult->failure++;
106
		    sresult->failureList.push_back("testXPathValues");
107
		}
68 108
	}
69
	delete resultValues;
109
    delete input;
110
    xpath->clearParameters();
111
    xpath->clearProperties();
70 112
	
71 113

  
72 114
}
......
74 116
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
75 117
void testXPathAttrValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
76 118
     cout<<endl<<"Test testXPathValues1:"<<endl; 
77
  	xpath->clearParameters(true);
119
  	xpath->clearParameters();
78 120
  	xpath->clearProperties();
79 121
	 XdmNode * input = processor->parseXmlFromString("<out attr='valuex'><person attr1='value1' attr2='value2'>text1</person><person>text2</person><person1>text3</person1></out>");
80
	if(input == NULL) {return;}
122
	if(input == nullptr) {
123
        SaxonApiException * exception = xpath->getException();
124
        if(exception != nullptr) {
125
            const char *message = xpath->getErrorMessage();
126
            cout << "Error Message = " << message << endl;
127
            xpath->exceptionClear();
128
        }
129
        sresult->failure++;
130
        sresult->failureList.push_back("testXPathAttrValues");
131

  
132
	    return;
133

  
134
	}
81 135

  
82 136
	//cout<<"Name of attr1= "<<input->getAttributeValue("attr")<<endl;
83 137

  
......
85 139
	xpath->setContextItem((XdmItem *)input);
86 140
	XdmItem * result = xpath->evaluateSingle("(//person)[1]");
87 141
	
88
	if(result == NULL) {
142
	if(result == nullptr) {
89 143
		 printf("result is null \n");
90 144
	} else {
91 145
		XdmNode *nodeValue = (XdmNode*)result;
92 146
		cout<<"Attribute Count= "<<nodeValue->getAttributeCount()<<endl;
93 147
		const char *attrVal = nodeValue->getAttributeValue("attr1");
94
		if(attrVal != NULL) {
148
		if(attrVal != nullptr) {
95 149
			cout<<"Attribute value= "<<attrVal<<endl;
96 150
		}
97 151
		
98 152
		XdmNode ** attrNodes = nodeValue->getAttributeNodes();
99
		if(attrNodes == NULL) { return;}
153
		if(attrNodes == nullptr) { return;}
100 154
	
101 155
		const char *name1 = attrNodes[0]->getNodeName();
102
		if(name1 != NULL) {
156
		if(name1 != nullptr) {
103 157
			cout<<"Name of attr1= "<<name1<<endl;
104 158
		}
105 159
		XdmNode * parent = attrNodes[0]->getParent();
106
		if(parent != NULL) {
160
		if(parent != nullptr) {
107 161
			cout<<"Name of parent= "<<parent->getNodeName()<<endl;
108 162
		}
109 163
		XdmNode * parent1 = parent->getParent();
110
		if(parent1 != NULL) {
164
		if(parent1 != nullptr) {
111 165
			cout<<"Name of parent= "<<parent1->getNodeName()<<endl;
112 166
		}
167
		sresult->success++;
113 168
	//TODO test if attr value is not there
114 169
	}
115 170
	
......
126 181

  
127 182
	XdmValue * resultValues = xpath->evaluate("//person");
128 183
	
129
	if(resultValues == NULL) {
184
	if(resultValues == nullptr) {
130 185
		 printf("result is null \n");
186
        SaxonApiException * exception = xpath->getException();
187
        if(exception != nullptr) {
188
            const char *message = xpath->getErrorMessage();
189
            cout << "Error Message = " << message << endl;
190
            xpath->exceptionClear();
191
        }
192
        sresult->failure++;
193
        sresult->failureList.push_back("testXPathOnFile");
131 194
	} else {
132 195
		cout<<"Number of items="<<resultValues->size()<<endl;
133 196
		for(int i =0; i< resultValues->size();i++){
134 197
			XdmItem * itemi = resultValues->itemAt(i);
135
			if(itemi == NULL) {
198
			if(itemi == nullptr) {
136 199
				cout<<"Item at position "<<i<<" should not be null"<<endl;
137 200
				break;
138 201
			}
......
147 210
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
148 211
void testXPathOnFile2(XPathProcessor * xpath, sResultCount *sresult){
149 212
    	 cout<<endl<<"Test testXPath with file source:"<<endl;
150
  	xpath->clearParameters(true);
213
  	xpath->clearParameters();
151 214
  	xpath->clearProperties(); 
152 215
	xpath->setContextFile("cat.xml");
153 216

  
154 217
	XdmItem * result = xpath->evaluateSingle("//person[1]");
155 218
	
156
	if(result == NULL) {
219
	if(result == nullptr) {
157 220
		 printf("result is null \n");
221
        SaxonApiException * exception = xpath->getException();
222
        if(exception != nullptr) {
223
            const char *message = xpath->getErrorMessage();
224
            cout << "Error Message = " << message << endl;
225
            xpath->exceptionClear();
226
        }
227
        sresult->failure++;
228
        sresult->failureList.push_back("testXPathOnFile2");
229
        return;
158 230
	} else {
159 231
		cout<<"Number of items="<<result->size()<<endl;
160 232
		if(result->isAtomic()) {
......
170 242
			cout<<"Node is of kind:"<<node->getNodeKind()<<endl;
171 243
		}
172 244
		const char * baseURI = node->getBaseUri();
173
		if(baseURI != NULL) {
245
		if(baseURI != nullptr) {
174 246
			cout<<"baseURI of node: "<<baseURI<<endl;
175 247
		}
248
		sresult->success++;
176 249
	}
177 250
	delete result;
178
	
179

  
180 251
}
181 252

  
182 253
int main()
......
205 276

  
206 277
    }
207 278

  
279
    delete xpathProc;
280
    delete processor;
281

  
208 282
     processor->release();
283

  
284

  
285
#ifdef MEM_DEBUG
286
    SaxonProcessor::getInfo();
287
#endif
209 288
    return 0;
210 289
}

Also available in: Unified diff