001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.wfs;
017:
018: import java.io.IOException;
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.NoSuchElementException;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import junit.framework.TestCase;
027:
028: import org.geotools.data.DefaultQuery;
029: import org.geotools.data.FeatureReader;
030: import org.geotools.data.Query;
031: import org.geotools.data.Transaction;
032: import org.geotools.feature.Feature;
033: import org.geotools.feature.FeatureType;
034: import org.geotools.feature.IllegalAttributeException;
035: import org.geotools.filter.AttributeExpression;
036: import org.geotools.filter.BBoxExpression;
037: import org.geotools.filter.FilterFactory;
038: import org.geotools.filter.FilterFactoryFinder;
039: import org.geotools.filter.FilterType;
040: import org.geotools.filter.GeometryFilter;
041: import org.geotools.filter.IllegalFilterException;
042: import org.xml.sax.SAXException;
043:
044: import com.vividsolutions.jts.geom.Envelope;
045:
046: /**
047: * <p>
048: * DOCUMENT ME!
049: * </p>
050: * @author dzwiers
051: *
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wfs/src/test/java/org/geotools/data/wfs/WFSDataStoreReadTest.java $
053: */
054: public class WFSDataStoreReadTest extends TestCase {
055:
056: public WFSDataStoreReadTest() {
057: Logger.global.setLevel(Level.SEVERE);
058: }
059:
060: public void testEmpty() {/**/
061: }
062:
063: public static WFSDataStore getDataStore(URL server)
064: throws IOException {
065: try {
066: Map m = new HashMap();
067: m.put(WFSDataStoreFactory.URL.key, server);
068: m.put(WFSDataStoreFactory.TIMEOUT.key, new Integer(10000)); // not debug
069: m
070: .put(WFSDataStoreFactory.TIMEOUT.key, new Integer(
071: 1000000)); //for debug
072: return (WFSDataStore) (new WFSDataStoreFactory())
073: .createNewDataStore(m);
074:
075: } catch (java.net.SocketException se) {
076: se.printStackTrace();
077: return null;
078: }
079: }
080:
081: public static void doFeatureType(URL url, boolean get,
082: boolean post, int i) throws IOException, SAXException {
083: if (url == null)
084: return;
085: try {
086: WFSDataStore wfs = getDataStore(url);
087: System.out.println("FeatureTypeTest + " + url);
088: assertNotNull("No featureTypes", wfs.getTypeNames());
089: assertNotNull("Null featureType in [" + i + "]", wfs
090: .getTypeNames()[i]);
091: System.out.println("FT name = " + wfs.getTypeNames()[i]);
092: if (get) {
093: // get
094: FeatureType ft = wfs
095: .getSchemaGet(wfs.getTypeNames()[i]);
096: assertNotNull("FeatureType was null", ft);
097: assertTrue(
098: wfs.getTypeNames()[i]
099: + " must have 1 geom and atleast 1 other attribute -- fair assumption",
100: ft.getDefaultGeometry() != null
101: && ft.getAttributeTypes() != null
102: && ft.getAttributeCount() > 0);
103: }
104: if (post) {
105: // post
106: FeatureType ft = wfs
107: .getSchemaPost(wfs.getTypeNames()[i]);
108: assertNotNull("FeatureType was null", ft);
109: assertTrue(
110: "must have 1 geom and atleast 1 other attribute -- fair assumption",
111: ft.getDefaultGeometry() != null
112: && ft.getAttributeTypes() != null
113: && ft.getAttributeCount() > 0);
114: }
115: } catch (java.net.SocketException se) {
116: se.printStackTrace();
117: }
118: }
119:
120: public static void doFeatureReader(URL url, boolean get,
121: boolean post, int i) throws NoSuchElementException,
122: IOException, IllegalAttributeException, SAXException {
123: if (url == null)
124: return;
125: try {
126: System.out.println("FeatureReaderTest + " + url);
127: WFSDataStore wfs = getDataStore(url);
128: assertNotNull("No featureTypes", wfs.getTypeNames());
129: assertNotNull("Null featureType in [0]",
130: wfs.getTypeNames()[i]);
131: Query query = new DefaultQuery(wfs.getTypeNames()[i]);
132:
133: if (post) {
134: // post
135: FeatureReader ft = wfs.getFeatureReaderPost(query,
136: Transaction.AUTO_COMMIT);
137: assertNotNull("FeatureType was null", ft);
138: assertTrue("must have 1 feature -- fair assumption", ft
139: .hasNext()
140: && ft.getFeatureType() != null
141: && ft.next() != null);
142: // disable for now
143: // assertNotNull("CRS missing ",ft.getFeatureType().getDefaultGeometry().getCoordinateSystem());
144: ft.close();
145: }
146: if (get) {
147: // get
148: FeatureReader ft = wfs.getFeatureReaderGet(query,
149: Transaction.AUTO_COMMIT);
150: assertNotNull("FeatureType was null", ft);
151: assertTrue("must have 1 feature -- fair assumption", ft
152: .hasNext()
153: && ft.getFeatureType() != null
154: && ft.next() != null);
155: // disable for now
156: // assertNotNull("CRS missing ",ft.getFeatureType().getDefaultGeometry().getCoordinateSystem());
157: ft.close();
158: }
159: } catch (java.net.SocketException se) {
160: se.printStackTrace();
161: }
162: }
163:
164: public static void doFeatureReaderWithQuery(URL url, boolean get,
165: boolean post, int i) throws NoSuchElementException,
166: IllegalAttributeException, IOException, SAXException {
167: if (url == null)
168: return;
169: try {
170: System.out.println("FeatureReaderWithFilterTest + " + url);
171: WFSDataStore wfs = getDataStore(url);
172: assertNotNull("No featureTypes", wfs.getTypeNames());
173: assertNotNull("Null featureType in [0]",
174: wfs.getTypeNames()[i]);
175: FeatureType ft = wfs.getSchema(wfs.getTypeNames()[i]);
176: // take atleast attributeType 3 to avoid the undeclared one .. inherited optional attrs
177:
178: String[] props;
179: props = new String[] { ft.getDefaultGeometry().getName() };
180:
181: DefaultQuery query = new DefaultQuery(ft.getTypeName());
182: query.setPropertyNames(props);
183: String fid = null;
184: if (get) {
185: // get
186: FeatureReader fr = wfs.getFeatureReaderGet(query,
187: Transaction.AUTO_COMMIT);
188: try {
189: assertNotNull("FeatureType was null", ft);
190:
191: FeatureType featureType = fr.getFeatureType();
192: if (ft.getAttributeCount() > 1) {
193: assertEquals(
194: "Query must restrict feature type to only having 1 AttributeType",
195: 1, featureType.getAttributeCount());
196: }
197: assertTrue(
198: "must have 1 feature -- fair assumption",
199: fr.hasNext() && featureType != null);
200: Feature feature = fr.next();
201: featureType = feature.getFeatureType();
202: if (ft.getAttributeCount() > 1) {
203: assertEquals(
204: "Query must restrict feature type to only having 1 AttributeType",
205: 1, featureType.getAttributeCount());
206: }
207: assertNotNull("must have 1 feature ", feature);
208: fid = feature.getID();
209: int j = 0;
210: while (fr.hasNext()) {
211: fr.next();
212: j++;
213: }
214: System.out.println(j + " Features");
215: } finally {
216: fr.close();
217: }
218: }
219: if (post) {
220: // post
221:
222: FeatureReader fr = wfs.getFeatureReaderPost(query,
223: Transaction.AUTO_COMMIT);
224: try {
225: assertNotNull("FeatureType was null", ft);
226: FeatureType featureType = fr.getFeatureType();
227: if (ft.getAttributeCount() > 1) {
228: assertEquals(
229: "Query must restrict feature type to only having 1 AttributeType",
230: 1, featureType.getAttributeCount());
231: }
232: assertTrue(
233: "must have 1 feature -- fair assumption",
234: fr.hasNext() && featureType != null);
235: Feature feature = fr.next();
236: featureType = feature.getFeatureType();
237: if (ft.getAttributeCount() > 1) {
238: assertEquals(
239: "Query must restrict feature type to only having 1 AttributeType",
240: 1, featureType.getAttributeCount());
241: }
242: assertNotNull("must have 1 feature ", feature);
243: fid = feature.getID();
244: int j = 0;
245: while (fr.hasNext()) {
246: fr.next();
247: j++;
248: }
249: System.out.println(j + " Features");
250: } finally {
251: fr.close();
252: }
253: }
254:
255: // test fid filter
256: query.setFilter(FilterFactoryFinder.createFilterFactory()
257: .createFidFilter(fid));
258: if (get) {
259: FeatureReader fr = wfs.getFeatureReaderGet(query,
260: Transaction.AUTO_COMMIT);
261: try {
262: assertNotNull("FeatureType was null", ft);
263: int j = 0;
264: while (fr.hasNext()) {
265: assertEquals(fid, fr.next().getID());
266: j++;
267: }
268: assertEquals(1, j);
269: } finally {
270: fr.close();
271: }
272: }
273: if (post) {
274: FeatureReader fr = wfs.getFeatureReaderPost(query,
275: Transaction.AUTO_COMMIT);
276: try {
277: assertNotNull("FeatureType was null", ft);
278: int j = 0;
279: while (fr.hasNext()) {
280: assertEquals(fid, fr.next().getID());
281: j++;
282: }
283: assertEquals(1, j);
284: } finally {
285: fr.close();
286: }
287: }
288: } catch (java.net.SocketException se) {
289: se.printStackTrace();
290: }
291:
292: }
293:
294: /** Request a subset of available properties
295: * @throws IllegalFilterException */
296: public static void doFeatureReaderWithBBox(URL url, boolean get,
297: boolean post, int i, Envelope bbox)
298: throws NoSuchElementException, IllegalAttributeException,
299: IOException, SAXException, IllegalFilterException {
300: if (url == null)
301: return; // test distabled (must be site specific)
302: try {
303: System.out.println("FeatureReaderWithFilterTest + " + url);
304: WFSDataStore wfs = getDataStore(url);
305: assertNotNull("No featureTypes", wfs.getTypeNames());
306: assertNotNull("Null featureType in [0]",
307: wfs.getTypeNames()[i]);
308: FeatureType ft = wfs.getSchema(wfs.getTypeNames()[i]);
309: // take atleast attributeType 3 to avoid the undeclared one .. inherited optional attrs
310:
311: FilterFactory factory = FilterFactoryFinder
312: .createFilterFactory();
313:
314: DefaultQuery query = new DefaultQuery(ft.getTypeName());
315: BBoxExpression theBBox = factory.createBBoxExpression(bbox);
316: AttributeExpression theGeom = factory
317: .createAttributeExpression(ft, ft
318: .getDefaultGeometry().getName());
319:
320: GeometryFilter filter = factory
321: .createGeometryFilter(FilterType.GEOMETRY_BBOX);
322: filter.addLeftGeometry(theGeom);
323: filter.addRightGeometry(theBBox);
324: query.setFilter(filter);
325: //query.setMaxFeatures(3);
326: if (get) {
327: // get
328: FeatureReader fr = wfs.getFeatureReaderGet(query,
329: Transaction.AUTO_COMMIT);
330: assertNotNull("FeatureType was null", ft);
331: assertTrue("must have 1 feature -- fair assumption", fr
332: .hasNext()
333: && fr.getFeatureType() != null
334: && fr.next() != null);
335: int j = 0;
336: while (fr.hasNext()) {
337: fr.next();
338: j++;
339: }
340: System.out.println("bbox selected " + j + " Features");
341: fr.close();
342: }
343: if (post) {
344: // post
345:
346: FeatureReader fr = wfs.getFeatureReaderPost(query,
347: Transaction.AUTO_COMMIT);
348: assertNotNull("FeatureType was null", ft);
349: assertTrue("must have 1 feature -- fair assumption", fr
350: .hasNext()
351: && fr.getFeatureType() != null
352: && fr.next() != null);
353: int j = 0;
354: while (fr.hasNext()) {
355: fr.next();
356: j++;
357: }
358: System.out.println("bbox selected " + j + " Features");
359: fr.close();
360: }
361: } catch (java.net.SocketException se) {
362: se.printStackTrace();
363: }
364: }
365: }
|