1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(2);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type"
});
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_0_0,
"initialize returns instance of default versioned protocol")
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
version: "1.1.0"
});
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_1_0,
"initialize returns instance of custom versioned protocol")
}
function test_setGeometryName(t) {
t.plan(4);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
geometryName: "geom"
});
t.eq(protocol.geometryName, "geom", "geometryName set correctly by constructor");
t.eq(protocol.format.geometryName, "geom", "geometryName correctly set on format by constructor");
// change the geometryName on the fly
protocol.setGeometryName("SHAPE");
t.eq(protocol.geometryName, "SHAPE", "geometryName changed correctly by setGeometryName");
t.eq(protocol.format.geometryName, "SHAPE", "geometryName correctly changed on format by setGeometryName");
protocol.destroy();
}
function test_setFeatureType(t) {
t.plan(4);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type"
});
t.eq(protocol.featureType, "type", "featureType set correctly by constructor");
t.eq(protocol.format.featureType, "type", "featureType correctly set on format by constructor");
// change the feature type on the fly
protocol.setFeatureType("foo");
t.eq(protocol.featureType, "foo", "featureType changed correctly by setFeatureType");
t.eq(protocol.format.featureType, "foo", "featureType correctly changed on format by setFeatureType");
protocol.destroy();
}
function test_read(t) {
t.plan(7);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
parseResponse: function(request, options) {
t.eq(request.responseText, "foo", "parseResponse called properly");
t.eq(options, {foo: "bar"}, "parseResponse receives readOptions");
return "foo";
}
});
var _POST = OpenLayers.Request.POST;
var expected, status;
OpenLayers.Request.POST = function(obj) {
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct");
obj.status = status;
obj.responseText = "foo";
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
expected = readXML("GetFeature_1");
status = 200;
var response = protocol.read({readOptions: {foo: "bar"}, callback: function(response) {
t.eq(response.features, "foo", "user callback properly called with features");
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly");
}});
options = {
maxFeatures: 10,
featureType: 'type2',
srsName: 'EPSG:900913',
featureNS: 'htttp://alternative.namespace.org',
callback: function(response) {
t.eq(response.code, OpenLayers.Protocol.Response.FAILURE, "failure reported properly to user callback");
}
};
expected = readXML("GetFeature_2");
status = 400;
var response = protocol.read(options);
OpenLayers.Request.POST = _POST;
}
function test_parseResponse_poorconfig(t) {
t.plan(2);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featurePrefix: "topp",
featureType: "tasmania_roads",
geometryName: null
});
protocol.parseResponse({responseText: document.getElementById("query_response").firstChild.nodeValue});
t.eq(protocol.geometryName, "geom", "geometryName configured correctly");
t.eq(protocol.featureNS, "http://www.openplans.org/topp", "featureNS configured correctly");
}
function test_exception(t) {
t.plan(8);
var url = "http://some.url.org";
var protocol = new OpenLayers.Protocol.WFS({
url: url,
version: "1.1.0",
featureNS: "http://namespace.org",
featureType: "type"
});
// mock up a response
var response = {
priv: {
status: 200,
responseText: '<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport language="en" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/ows http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows"><ows:Exception locator="foo" exceptionCode="InvalidParameterValue"><ows:ExceptionText>Update error: Error occurred updating features</ows:ExceptionText><ows:ExceptionText>Second exception line</ows:ExceptionText></ows:Exception></ows:ExceptionReport>'
}
};
var log, entry, expected;
// test GetFeature
log = [];
protocol.handleRead(OpenLayers.Util.extend({}, response), {
callback: function(resp) {
log.push(resp);
}
});
expected = {
exceptionReport: {
version: "1.0.0",
language: "en",
exceptions: [{
code: "InvalidParameterValue",
locator: "foo",
texts: [
"Update error: Error occurred updating features",
"Second exception line"
]
}]
},
success: false
};
t.eq(log.length, 1, "GetFeature handled");
entry = log[0];
t.eq(entry.code, OpenLayers.Protocol.Response.FAILURE, "GetFeature failure reported");
t.ok(!!entry.error, "GetFeature got error");
t.eq(entry.error, expected, "GetFeature error matches expected");
// test a commit
log = [];
protocol.handleCommit(response, {
callback: function(resp) {
log.push(resp);
}
});
t.eq(log.length, 1, "commit handled");
entry = log[0];
t.eq(entry.code, OpenLayers.Protocol.Response.FAILURE, "commit failure reported");
t.ok(!!entry.error, "commit got error");
t.eq(entry.error, expected, "GetFeature error matches expected");
}
function test_commit(t){
t.plan(5);
var url = "http://some.url.org";
var protocol = new OpenLayers.Protocol.WFS({
url: url,
featureNS: "http://namespace.org",
featureType: "type"
});
protocol.format.read = function(data) {
t.eq(data, "foo", "callback called with correct argument");
return {
insertIds: new Array(3),
success: true
}
};
var _POST = OpenLayers.Request.POST;
var expected;
OpenLayers.Request.POST = function(obj) {
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "Transaction XML with Insert, Update and Delete created correctly");
t.eq(obj.headers, {foo: 'bar'}, "HTTP headers passed from commit to Request.POST");
obj.responseText = "foo";
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
var featureDelete = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"});
featureDelete.fid = "fid.37";
featureDelete.state = OpenLayers.State.DELETE;
featureDelete.layer = {
projection: {
getCode : function(){
return "EPSG:4326";
}
}
}
var featureInsert = featureDelete.clone();
featureInsert.state = OpenLayers.State.INSERT;
var featureModify = featureDelete.clone();
featureModify.fid = "fid.37";
featureModify.state = OpenLayers.State.UPDATE;
options = {
featureNS: "http://some.namespace.org",
featureType: "type",
headers: {foo: 'bar'},
callback: function(response) {
t.eq(response.insertIds.length, 3, "correct response passed to user callback");
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success properly reported to user callback");
}
}
expected = readXML("commit");
var response = protocol.commit([featureInsert, featureModify, featureDelete], options);
OpenLayers.Request.POST = _POST;
}
function test_filterDelete(t) {
t.plan(2)
var url = "http://some.url.org";
var protocol = new OpenLayers.Protocol.WFS({
url: url,
featureNS: "http://namespace.org",
featureType: "type"
});
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: new OpenLayers.Bounds(-5, -5, 5, 5)
});
var _POST = OpenLayers.Request.POST;
var expected = readXML("filter_delete");
OpenLayers.Request.POST = function(obj) {
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "request data correct");
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
var response = protocol.filterDelete(filter, {
callback: function() {
t.ok("user callback function called");
}
});
OpenLayers.Request.POST = _POST;
}
function test_abort(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://example.com",
featureNS: "http://example.com#namespace",
featureType: "type"
});
var response = {
priv: {
abort: function() {
aborted = true;
}
}
};
// call abort with mocked response
var aborted = false;
protocol.abort(response);
t.eq(aborted, true, "abort called on response.priv");
}
function test_fromWMSLayer(t) {
t.plan(9);
var map = new OpenLayers.Map("map", {
projection: "CRS:84"
});
var layer = new OpenLayers.Layer.WMS("foo", "htttp://foo/ows",
{layers: "topp:states"}
);
map.addLayer(layer);
var protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
t.eq(protocol.url, "htttp://foo/ows", "url taken from wms layer");
t.eq(protocol.featurePrefix, "topp", "feature prefix correctly extracted");
t.eq(protocol.featureType, "states", "typeName correctly extracted");
t.eq(protocol.srsName, "CRS:84", "srsName set correctly");
t.eq(protocol.version, "1.1.0", "version set correctly");
t.eq(protocol.format.geometryName, null, "format's geometryName set to null");
layer.params["LAYERS"] = ["topp:street_centerline", "topp:states"];
layer.projection = new OpenLayers.Projection("EPSG:900913");
protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
t.eq(protocol.featurePrefix, "topp", "featurePrefix from layer param array");
t.eq(protocol.featureType, "street_centerline", "first layer from layer param array as featureType");
t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred");
}
function test_readFormat(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
formatOptions: {outputFormat: 'json'},
readFormat: new OpenLayers.Format.GeoJSON()
});
var request = {};
request.responseText = '{"type":"FeatureCollection","features":[{"type":"Feature","id":"V_HECTOPUNTEN.108411","geometry":{"type":"MultiPoint","coordinates":[[190659.467,349576.19]]},"geometry_name":"ORA_GEOMETRY","properties":{"WEGNUMMER":"002","HECTOMTRNG_ORG":2200,"HECTOMTRNG":"220.00","bbox":[190659.467,349576.19,190659.467,349576.19]}}]}';
var features = protocol.parseResponse(request);
t.eq(features.length, 1, "the right format is used to read the request (GeoJSON)");
}
function test_outputFormat(t) {
t.plan(2);
var protocol = new OpenLayers.Protocol.WFS({
version: "1.1.0",
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
outputFormat: 'json'
});
t.ok(protocol.readFormat instanceof OpenLayers.Format.GeoJSON, "the correct readFormat is used for outputFormat JSON");
protocol = new OpenLayers.Protocol.WFS({
version: "1.1.0",
url: "http://some.url.org",
featureNS: "http://namespace.org",
featureType: "type",
outputFormat: 'GML2'
});
t.ok(protocol.readFormat instanceof OpenLayers.Format.GML.v2, "the correct readFormat is used for outputFormat GML2");
}
function test_readOptions(t) {
t.plan(1);
var protocol = new OpenLayers.Protocol.WFS({
url: "http://some.url.org",
version: "1.1.0",
featureNS: "http://namespace.org",
featureType: "type",
readOptions: {'output': 'object'},
parseResponse: function(request, options) {
t.eq(options.output, "object", "Options object correctly set to pass on to Format's read");
}
});
var _POST = OpenLayers.Request.POST;
OpenLayers.Request.POST = function(obj) {
obj.status = 200;
obj.responseText = "foo";
t.delay_call(0.1, function() {obj.callback.call(this)});
return obj;
};
protocol.read({
callback: function() {}
});
OpenLayers.Request.POST = _POST;
}
function readXML(id) {
var xml = document.getElementById(id).firstChild.nodeValue;
return new OpenLayers.Format.XML().read(xml).documentElement;
}
</script>
</head>
<body>
<div id="map" style="width:512px; height:256px"> </div>
<div id="GetFeature_1"><!--
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Query typeName="feature:type" xmlns:feature="http://namespace.org"/>
</wfs:GetFeature>
--></div>
<div id="GetFeature_2"><!--
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" maxFeatures="10" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Query typeName="feature:type2" xmlns:feature="htttp://alternative.namespace.org"/>
</wfs:GetFeature>
--></div>
<div id="commit"><!--
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfs:Insert>
<feature:type xmlns:feature="http://namespace.org">
<feature:the_geom>
<gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates>
</gml:Point>
</feature:the_geom>
<feature:has>cheeseburger</feature:has>
</feature:type>
</wfs:Insert>
<wfs:Update typeName="feature:type" xmlns:feature="http://namespace.org">
<wfs:Property>
<wfs:Name>the_geom</wfs:Name>
<wfs:Value>
<gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates>
</gml:Point>
</wfs:Value>
</wfs:Property>
<wfs:Property>
<wfs:Name>has</wfs:Name>
<wfs:Value>cheeseburger</wfs:Value>
</wfs:Property>
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:FeatureId fid="fid.37"/>
</ogc:Filter>
</wfs:Update>
<wfs:Delete typeName="feature:type" xmlns:feature="http://namespace.org">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:FeatureId fid="fid.37"/>
</ogc:Filter>
</wfs:Delete>
</wfs:Transaction>
--></div>
<div id="filter_delete"><!--
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0">
<wfs:Delete typeName="feature:type" xmlns:feature="http://namespace.org">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:BBOX>
<gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:coordinates decimal="." cs="," ts=" ">-5,-5 5,5</gml:coordinates>
</gml:Box>
</ogc:BBOX>
</ogc:Filter>
</wfs:Delete>
</wfs:Transaction>
--></div>
<div id="query_response"><!--
<?xml version="1.0" encoding="UTF-8"?>
<wfs:FeatureCollection xmlns:ogc="http://www.opengis.net/ogc" xmlns:wfs="http://www.opengis.net/wfs" xmlns:topp="http://www.openplans.org/topp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink"><gml:boundedBy><gml:Envelope srsDimension="2" srsName="urn:x-ogc:def:crs:EPSG:4326"><gml:lowerCorner>5450000.0 500000.0</gml:lowerCorner><gml:upperCorner>5450000.0 540000.0</gml:upperCorner></gml:Envelope></gml:boundedBy><gml:featureMembers><topp:tasmania_roads gml:id="tasmania_roads.1"><gml:boundedBy><gml:Envelope srsDimension="2" srsName="urn:x-ogc:def:crs:EPSG:4326"><gml:lowerCorner>5450000.0 500000.0</gml:lowerCorner><gml:upperCorner>5450000.0 540000.0</gml:upperCorner></gml:Envelope></gml:boundedBy><topp:geom><gml:MultiLineString srsDimension="2" srsName="urn:x-ogc:def:crs:EPSG:4326"><gml:lineStringMember><gml:LineString><gml:posList>5450000.0 500000.0 5450000.0 540000.0</gml:posList></gml:LineString></gml:lineStringMember></gml:MultiLineString></topp:geom><topp:TYPE>street</topp:TYPE></topp:tasmania_roads></gml:featureMembers></wfs:FeatureCollection>
--></div>
</body>
</html>
|