01: package org.geoserver.wfs.response;
02:
03: import java.io.BufferedReader;
04: import java.io.InputStream;
05: import java.io.InputStreamReader;
06:
07: import junit.textui.TestRunner;
08: import net.sf.json.JSONArray;
09: import net.sf.json.JSONObject;
10:
11: import org.geoserver.wfs.WFSTestSupport;
12:
13: public class GeoJSONTest extends WFSTestSupport {
14:
15: public void testGet() throws Exception {
16: InputStream is = get("wfs?request=GetFeature&version=1.0.0&typename=sf:PrimitiveGeoFeature&maxfeatures=1&outputformat=json");
17: BufferedReader in = new BufferedReader(
18: new InputStreamReader(is));
19: StringBuffer buffer = new StringBuffer();
20: String line;
21: while ((line = in.readLine()) != null) {
22: buffer.append(line);
23: }
24: String out = buffer.toString();
25:
26: JSONObject rootObject = JSONObject.fromObject(out);
27: assertEquals(rootObject.get("type"), "FeatureCollection");
28: JSONArray featureCol = rootObject.getJSONArray("features");
29: JSONObject aFeature = featureCol.getJSONObject(0);
30: assertEquals(aFeature.getString("geometry_name"),
31: "surfaceProperty");
32: }
33:
34: public void testPost() throws Exception {
35: String xml = "<wfs:GetFeature " + "service=\"WFS\" "
36: + "outputFormat=\"json\" " + "version=\"1.0.0\" "
37: + "xmlns:cdf=\"http://www.opengis.net/cite/data\" "
38: + "xmlns:ogc=\"http://www.opengis.net/ogc\" "
39: + "xmlns:wfs=\"http://www.opengis.net/wfs\" " + "> "
40: + "<wfs:Query typeName=\"sf:PrimitiveGeoFeature\"> "
41: + "</wfs:Query> " + "</wfs:GetFeature>";
42:
43: InputStream is = post("wfs", xml);
44: BufferedReader in = new BufferedReader(
45: new InputStreamReader(is));
46: StringBuffer buffer = new StringBuffer();
47: String line;
48: while ((line = in.readLine()) != null) {
49: buffer.append(line);
50: }
51: String out = buffer.toString();
52:
53: JSONObject rootObject = JSONObject.fromObject(out);
54: assertEquals(rootObject.get("type"), "FeatureCollection");
55: JSONArray featureCol = rootObject.getJSONArray("features");
56: JSONObject aFeature = featureCol.getJSONObject(0);
57: assertEquals(aFeature.getString("geometry_name"),
58: "surfaceProperty");
59: }
60:
61: public void testGeometryCollection() throws Exception {
62: InputStream is = get("wfs?request=GetFeature&version=1.0.0&typename=sf:AggregateGeoFeature&maxfeatures=3&outputformat=json");
63: BufferedReader in = new BufferedReader(
64: new InputStreamReader(is));
65: StringBuffer buffer = new StringBuffer();
66: String line;
67: while ((line = in.readLine()) != null) {
68: buffer.append(line);
69: }
70:
71: String out = buffer.toString();
72:
73: JSONObject rootObject = JSONObject.fromObject(out);
74: assertEquals(rootObject.get("type"), "FeatureCollection");
75: JSONArray featureCol = rootObject.getJSONArray("features");
76: JSONObject aFeature = featureCol.getJSONObject(1);
77: JSONObject aGeometry = aFeature.getJSONObject("geometry");
78: assertEquals(aGeometry.getString("type"), "MultiLineString");
79: JSONArray geomArray = aGeometry.getJSONArray("coordinates");
80: geomArray = geomArray.getJSONArray(0);
81: geomArray = geomArray.getJSONArray(0);
82: assertEquals(geomArray.getString(0), "55.174");
83: }
84:
85: public static void main(String[] args) {
86: TestRunner runner = new TestRunner();
87: runner.run(GeoJSONTest.class);
88: }
89: }
|