001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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;
009: * version 2.1 of the License.
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.mif;
017:
018: import com.vividsolutions.jts.geom.LineString;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.geotools.data.DefaultQuery;
022: import org.geotools.data.DefaultTransaction;
023: import org.geotools.data.FeatureReader;
024: import org.geotools.data.FeatureSource;
025: import org.geotools.data.FeatureStore;
026: import org.geotools.data.FeatureWriter;
027: import org.geotools.data.Transaction;
028: import org.geotools.feature.AttributeTypeFactory;
029: import org.geotools.feature.Feature;
030: import org.geotools.feature.FeatureType;
031: import org.geotools.feature.FeatureTypeBuilder;
032: import org.geotools.filter.Filter;
033: import java.io.File;
034: import java.io.FileNotFoundException;
035: import java.io.IOException;
036: import java.sql.Date;
037: import java.util.HashMap;
038:
039: /**
040: * DOCUMENT ME!
041: *
042: * @author Luca S. Percich, AMA-MI
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mif/src/test/java/org/geotools/data/mif/MIFDataStoreTest.java $
044: */
045: public class MIFDataStoreTest extends TestCase {
046: private MIFDataStore ds;
047:
048: /**
049: * DOCUMENT ME!
050: *
051: * @param args DOCUMENT ME!
052: *
053: * @throws Exception DOCUMENT ME!
054: */
055: public static void main(java.lang.String[] args) throws Exception {
056: junit.textui.TestRunner.run(new TestSuite(
057: MIFDataStoreTest.class));
058: }
059:
060: /*
061: * @see TestCase#setUp()
062: */
063: protected void setUp() throws Exception {
064: super .setUp();
065: MIFTestUtils.cleanFiles();
066: }
067:
068: /*
069: * @see TestCase#tearDown()
070: */
071: protected void tearDown() throws Exception {
072: MIFTestUtils.cleanFiles();
073: super .tearDown();
074: }
075:
076: /**
077: * Utility method for instantiating a MIFDataStore
078: *
079: * @param initPath DOCUMENT ME!
080: *
081: * @return DOCUMENT ME!
082: */
083: protected boolean initDS(String initPath) {
084: try {
085: initPath = MIFTestUtils.fileName(initPath);
086:
087: HashMap params = MIFTestUtils.getParams("mif", initPath,
088: null);
089: ds = new MIFDataStore(initPath, params);
090: assertNotNull(ds);
091:
092: return true;
093: } catch (Exception e) {
094: fail(e.getMessage());
095:
096: return false;
097: }
098: }
099:
100: /**
101: * See if all the MIF in data dir are recognized
102: */
103: public void testOpenDir() {
104: initDS("");
105:
106: try {
107: assertNotNull(ds.getSchema("grafo"));
108: assertNotNull(ds.getSchema("nodi"));
109: assertNotNull(ds.getSchema("mixed"));
110: } catch (IOException e) {
111: fail(e.getMessage());
112: }
113: }
114:
115: /**
116: */
117: public void testCreateSchema() {
118: initDS("");
119:
120: try {
121: FeatureTypeBuilder builder = FeatureTypeBuilder
122: .newInstance("newschema");
123: builder.addType(AttributeTypeFactory.newAttributeType(
124: "obj", LineString.class, true));
125: builder.addType(AttributeTypeFactory.newAttributeType(
126: "charfield", String.class, false, 25, ""));
127: builder.addType(AttributeTypeFactory
128: .newAttributeType("intfield", Integer.class, false,
129: 0, new Integer(0)));
130:
131: builder.addType(AttributeTypeFactory.newAttributeType(
132: "datefield", Date.class, true));
133: builder.addType(AttributeTypeFactory.newAttributeType(
134: "doublefield", Double.class, false, 0,
135: new Double(0)));
136: builder.addType(AttributeTypeFactory.newAttributeType(
137: "floatfield", Float.class, false, 0, new Float(0)));
138: builder.addType(AttributeTypeFactory.newAttributeType(
139: "boolfield", Boolean.class, false, 0, new Boolean(
140: false)));
141:
142: FeatureType newFT = builder.getFeatureType();
143:
144: ds.createSchema(newFT);
145:
146: FeatureType builtFT = ds.getSchema("newschema");
147:
148: assertEquals(builtFT, newFT);
149: } catch (Exception e) {
150: fail(e.getMessage());
151: }
152: }
153:
154: /**
155: */
156: public void testCreateSchemaBadGeometry() {
157: initDS("");
158:
159: try {
160: FeatureTypeBuilder builder = FeatureTypeBuilder
161: .newInstance("newschema");
162: builder.addType(AttributeTypeFactory.newAttributeType(
163: "charfield", String.class, false, 25, ""));
164: builder.addType(AttributeTypeFactory
165: .newAttributeType("intfield", Integer.class, false,
166: 0, new Integer(0)));
167: builder.addType(AttributeTypeFactory.newAttributeType(
168: "obj", LineString.class, true));
169:
170: FeatureType newFT = builder.getFeatureType();
171:
172: ds.createSchema(newFT);
173: fail("SchemaException expected"); // Geometry must be the first field
174: } catch (Exception e) {
175: }
176: }
177:
178: /**
179: */
180: public void testCreateSchemaTwoGeometry() {
181: initDS("");
182:
183: try {
184: FeatureTypeBuilder builder = FeatureTypeBuilder
185: .newInstance("newschema");
186: builder.addType(AttributeTypeFactory.newAttributeType(
187: "obj", LineString.class, true));
188: builder.addType(AttributeTypeFactory.newAttributeType(
189: "charfield", String.class, false, 25, ""));
190: builder.addType(AttributeTypeFactory.newAttributeType(
191: "obj2", LineString.class, true));
192: builder.addType(AttributeTypeFactory
193: .newAttributeType("intfield", Integer.class, false,
194: 0, new Integer(0)));
195:
196: FeatureType newFT = builder.getFeatureType();
197:
198: ds.createSchema(newFT);
199: fail("SchemaException expected"); // Only one geometry
200: } catch (Exception e) {
201: }
202: }
203:
204: /**
205: */
206: public void testFeatureReaderFilter() {
207: initDS("grafo"); // .mif
208:
209: try {
210: FeatureReader fr = getFeatureReader("grafo", "ID = 33755");
211: Feature arc = null;
212: Integer id = new Integer(0);
213:
214: if (fr.hasNext()) {
215: arc = fr.next();
216: id = (Integer) arc.getAttribute("ID");
217: }
218:
219: assertNotNull(arc);
220: assertEquals(id.intValue(), 33755);
221: fr.close();
222: } catch (Exception e) {
223: fail(e.getMessage());
224: }
225: }
226:
227: /**
228: * Tests createSchema & FeatureWriter
229: */
230: public void testFeatureWriter() {
231: initDS("");
232:
233: String outmif = "grafo_new";
234:
235: try {
236: FeatureType newFT = MIFTestUtils.duplicateSchema(ds
237: .getSchema("grafo"), outmif);
238: ds.createSchema(newFT);
239:
240: int maxAttr = newFT.getAttributeCount() - 1;
241:
242: FeatureWriter fw = ds.getFeatureWriterAppend(outmif,
243: Transaction.AUTO_COMMIT);
244: Feature f;
245: FeatureReader fr = getFeatureReader("grafo",
246: "ID == 73690 || ID == 71045");
247:
248: int counter = 0;
249:
250: while (fr.hasNext()) {
251: ++counter;
252:
253: Feature fin = fr.next();
254: f = fw.next();
255:
256: for (int i = 0; i <= maxAttr; i++) {
257: f.setAttribute(i, fin.getAttribute(i));
258: }
259:
260: fw.write();
261: }
262:
263: fr.close();
264: fw.close();
265:
266: assertEquals(counter, 2);
267:
268: fw = ds.getFeatureWriter(outmif, MIFTestUtils
269: .parseFilter("ID == 71045"),
270: Transaction.AUTO_COMMIT);
271:
272: assertEquals(true, fw.hasNext());
273: f = fw.next();
274: fw.remove();
275:
276: fw.close();
277:
278: fw = ds.getFeatureWriterAppend(outmif,
279: Transaction.AUTO_COMMIT);
280:
281: f = fw.next();
282: f.setAttribute("ID", "99998");
283: f.setAttribute("NOMECOMUNE", "foobar");
284: fw.write();
285:
286: fw.close();
287:
288: fr = getFeatureReader(outmif);
289:
290: counter = 0;
291:
292: while (fr.hasNext()) {
293: f = fr.next();
294: counter++;
295: }
296:
297: fr.close();
298: assertEquals(counter, 2);
299: } catch (Exception e) {
300: fail(e.getMessage());
301: }
302: }
303:
304: /**
305: * Tests createSchema & FeatureWriter
306: */
307: public void testFeatureWriterAppendTransaction() {
308: try {
309: String outmif = "grafo_append";
310: MIFTestUtils.copyMif("grafo", outmif);
311: initDS(outmif);
312:
313: Feature f;
314: Transaction transaction = new DefaultTransaction("mif");
315:
316: try {
317: FeatureWriter fw = ds.getFeatureWriterAppend(outmif,
318: transaction);
319:
320: f = fw.next();
321: f = fw.next();
322: f.setAttribute("ID", "80001");
323: f.setAttribute("NOMECOMUNE", "foo");
324: fw.write();
325:
326: f = fw.next();
327: f.setAttribute("ID", "80002");
328: f.setAttribute("NOMECOMUNE", "bar");
329: fw.write();
330:
331: fw.close();
332:
333: transaction.commit();
334: } catch (Exception e) {
335: transaction.rollback();
336: fail(e.getMessage());
337: } finally {
338: transaction.close();
339: }
340:
341: FeatureReader fr = getFeatureReader(outmif,
342: "ID > 80000 && ID <80003");
343:
344: int counter = 0;
345:
346: while (fr.hasNext()) {
347: f = fr.next();
348: counter++;
349: }
350:
351: fr.close();
352:
353: assertEquals(counter, 2);
354: } catch (Exception e) {
355: e.printStackTrace();
356: fail(e.getMessage());
357: }
358: }
359:
360: /**
361: * DOCUMENT ME!
362: */
363: public void testFeatureSource() {
364: String outmif = "mixed_fs";
365:
366: try {
367: MIFTestUtils.copyMif("mixed", outmif);
368: } catch (IOException e) {
369: fail(e.getMessage());
370: }
371:
372: initDS(outmif);
373:
374: FeatureSource fs = null;
375: FeatureType featureType = null;
376:
377: try {
378: featureType = ds.getSchema(outmif);
379: assertNotNull("Cannot get FeatureType", featureType);
380: } catch (Exception e) {
381: fail("Cannot get FeatureType: " + e.getMessage());
382: }
383:
384: try {
385: fs = ds.getFeatureSource(outmif);
386: assertNotNull("Cannot get FeatureSource.", fs);
387: } catch (IOException e) {
388: fail("Cannot get FeatureSource: " + e.getMessage());
389: }
390:
391: try {
392: ((FeatureStore) fs).modifyFeatures(featureType
393: .getAttributeType("DESCRIPTION"), "FOO",
394: Filter.INCLUDE);
395: } catch (Exception e) {
396: fail("Cannot update Features: " + e.getMessage());
397: }
398:
399: try {
400: ((FeatureStore) fs).removeFeatures(MIFTestUtils
401: .parseFilter("GEOMTYPE != 'NULL'"));
402: } catch (IOException e) {
403: fail("Cannot delete Features: " + e.getMessage());
404: }
405:
406: try {
407: FeatureReader fr = getFeatureReader(outmif);
408:
409: assertEquals(true, fr.hasNext());
410:
411: Feature f = fr.next();
412: assertEquals("FOO", f.getAttribute("DESCRIPTION"));
413: assertEquals(false, fr.hasNext());
414:
415: fr.close();
416: } catch (Exception e) {
417: fail("Cannot check feature: " + e.getMessage());
418: }
419: }
420:
421: /**
422: * Test that feature get the correct SRID
423: */
424: public void testSRID() {
425: initDS("");
426:
427: FeatureReader fr;
428:
429: try {
430: fr = getFeatureReader("grafo");
431:
432: Feature f = fr.next();
433: assertEquals(f.getDefaultGeometry().getFactory().getSRID(),
434: MIFTestUtils.SRID);
435:
436: fr.close();
437: } catch (Exception e) {
438: fail("Cannot check SRID: " + e.getMessage());
439: }
440: }
441:
442: /**
443: * Obtain a feature reader for the given featureType / filter
444: *
445: * @param featureTypeName
446: * @param filter
447: *
448: *
449: * @throws Exception
450: */
451: protected FeatureReader getFeatureReader(String featureTypeName,
452: String filter) throws Exception {
453: DefaultQuery q = new DefaultQuery(featureTypeName, MIFTestUtils
454: .parseFilter(filter));
455:
456: return ds.getFeatureReader(q, Transaction.AUTO_COMMIT);
457: }
458:
459: /**
460: * Obtain a feature reader for all the features of the given featureType
461: *
462: * @param featureTypeName
463: *
464: *
465: * @throws Exception
466: */
467: protected FeatureReader getFeatureReader(String featureTypeName)
468: throws Exception {
469: return getFeatureReader(featureTypeName, "1=1");
470: }
471: }
|