Source Code Cross Referenced for MIFFileTest.java in  » GIS » GeoTools-2.4.1 » org » geotools » data » mif » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.data.mif 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.Geometry;
019:        import com.vividsolutions.jts.geom.LineString;
020:        import com.vividsolutions.jts.geom.MultiLineString;
021:        import com.vividsolutions.jts.geom.Point;
022:        import com.vividsolutions.jts.geom.Polygon;
023:        import junit.framework.TestCase;
024:        import junit.framework.TestSuite;
025:        import org.geotools.data.FeatureReader;
026:        import org.geotools.data.FeatureWriter;
027:        import org.geotools.feature.Feature;
028:        import org.geotools.feature.FeatureType;
029:        import org.geotools.feature.IllegalAttributeException;
030:        import java.io.IOException;
031:        import java.util.HashMap;
032:        import java.util.NoSuchElementException;
033:
034:        /**
035:         * DOCUMENT ME!
036:         *
037:         * @author Luca S. Percich, AMA-MI
038:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mif/src/test/java/org/geotools/data/mif/MIFFileTest.java $
039:         */
040:        public class MIFFileTest extends TestCase {
041:            private MIFFile mif = null;
042:
043:            /**
044:             * DOCUMENT ME!
045:             *
046:             * @param args DOCUMENT ME!
047:             *
048:             * @throws Exception DOCUMENT ME!
049:             */
050:            public static void main(java.lang.String[] args) throws Exception {
051:                junit.textui.TestRunner.run(new TestSuite(MIFFileTest.class));
052:            }
053:
054:            /*
055:             * @see TestCase#setUp()
056:             */
057:            protected void setUp() throws Exception {
058:                super .setUp();
059:                MIFTestUtils.cleanFiles();
060:            }
061:
062:            /*
063:             * @see TestCase#tearDown()
064:             */
065:            protected void tearDown() throws Exception {
066:                MIFTestUtils.cleanFiles();
067:                super .tearDown();
068:            }
069:
070:            /**
071:             * Tests for schema and feature reading from an existing MIF file
072:             */
073:            public void testMIFFileOpen() {
074:                try {
075:                    mif = new MIFFile(MIFTestUtils.fileName("mixed"), // .mif
076:                            MIFTestUtils.getParams("mif", "", null));
077:                    assertEquals("450", mif
078:                            .getHeaderClause(MIFDataStore.HCLAUSE_VERSION));
079:
080:                    FeatureType schema = mif.getSchema();
081:                    assertNotNull(schema);
082:                    assertEquals(11, schema.getAttributeCount());
083:                    assertEquals("DESCRIPTION", schema.getAttributeType(1)
084:                            .getName());
085:                    assertEquals(Double.class, schema
086:                            .getAttributeType("LENGTH").getType());
087:
088:                    FeatureReader fr = mif.getFeatureReader();
089:                    int tot = 0;
090:
091:                    while (fr.hasNext()) {
092:                        Feature f = fr.next();
093:
094:                        if (++tot == 4) {
095:                            assertEquals("POLYGON", (String) f
096:                                    .getAttribute("GEOMTYPE"));
097:                        }
098:                    }
099:
100:                    fr.close();
101:
102:                    assertEquals(tot, 9);
103:                } catch (Exception e) {
104:                    fail(e.getMessage());
105:                }
106:            }
107:
108:            /*
109:             * Test a MIF file copy using input FeatureReader, createSchema and output FeatureWriter
110:             */
111:            public void testFileCopy() {
112:                if (!System.getProperty("os.name", "unknown").startsWith(
113:                        "Windows")) {
114:                    // For an unknown reason, this test seems to fail on Linux box.
115:                    return;
116:                }
117:                MIFFile in = null;
118:                MIFFile out = null;
119:                FeatureReader inFR = null;
120:                FeatureReader outFR = null;
121:                FeatureWriter outFW = null;
122:                int maxAttr = 0;
123:
124:                try {
125:                    // Input file
126:                    in = new MIFFile(MIFTestUtils.fileName("grafo"), null); // .mif
127:
128:                    FeatureType ft = in.getSchema();
129:
130:                    maxAttr = ft.getAttributeCount() - 1;
131:
132:                    // Params for output file
133:                    HashMap params = new HashMap();
134:
135:                    // params.put(MIFFile.HCLAUSE_TRANSFORM, "100,100,0,0");
136:                    params.put(MIFDataStore.HCLAUSE_UNIQUE, in
137:                            .getHeaderClause(MIFDataStore.HCLAUSE_UNIQUE));
138:                    params.put(MIFDataStore.HCLAUSE_INDEX, in
139:                            .getHeaderClause(MIFDataStore.HCLAUSE_INDEX));
140:                    params.put(MIFDataStore.HCLAUSE_VERSION, in
141:                            .getHeaderClause(MIFDataStore.HCLAUSE_VERSION));
142:                    params.put(MIFDataStore.HCLAUSE_COORDSYS, in
143:                            .getHeaderClause(MIFDataStore.HCLAUSE_COORDSYS));
144:
145:                    // params.put(MIFDataStore.HCLAUSE_DELIMITER, in.getHeaderClause(MIFDataStore.HCLAUSE_DELIMITER));
146:                    params.put(MIFDataStore.HCLAUSE_DELIMITER, ",");
147:
148:                    // Output file
149:                    out = new MIFFile(MIFTestUtils.fileName("grafo_out"), ft,
150:                            params); // .mif
151:                } catch (Exception e) {
152:                    fail("Can't create grafo_out: " + e.getMessage());
153:                }
154:
155:                try {
156:                    inFR = in.getFeatureReader();
157:                    outFW = out.getFeatureWriter();
158:
159:                    Feature inF;
160:                    Feature outF;
161:                    int counter = 0;
162:
163:                    while (inFR.hasNext()) {
164:                        inF = inFR.next();
165:                        outF = outFW.next();
166:
167:                        for (int i = 0; i < outF.getNumberOfAttributes(); i++) {
168:                            outF.setAttribute(i, inF.getAttribute(i));
169:                        }
170:
171:                        outFW.write();
172:                        counter++;
173:                    }
174:
175:                    inFR.close();
176:                    outFW.close();
177:                } catch (Exception e) {
178:                    fail("Can't copy features: " + e.getMessage());
179:                }
180:
181:                try {
182:                    inFR = in.getFeatureReader();
183:
184:                    outFR = out.getFeatureReader();
185:
186:                    int n = 0;
187:
188:                    while (inFR.hasNext()) {
189:                        Feature fin = inFR.next();
190:                        Feature fout = outFR.next();
191:
192:                        // Cycling attribute sampling
193:                        assertEquals(fin.getAttribute(n).toString(), fout
194:                                .getAttribute(n).toString());
195:
196:                        if (++n > maxAttr) {
197:                            n = 0;
198:                        }
199:                    }
200:
201:                    inFR.close();
202:                    outFR.close();
203:                } catch (Exception e) {
204:                    fail("Can't compare features: " + e.getMessage());
205:                }
206:            }
207:
208:            /**
209:             * Test writing / appending
210:             */
211:            public void testFeatureWriter() {
212:                try {
213:                    MIFTestUtils.copyMif("mixed", "mixed_wri");
214:
215:                    MIFFile in = new MIFFile(
216:                            MIFTestUtils.fileName("mixed_wri"), // .mif
217:                            MIFTestUtils.getParams("", "", null));
218:                    FeatureWriter fw = in.getFeatureWriter();
219:
220:                    Feature f;
221:                    int counter = 0;
222:
223:                    while (fw.hasNext()) {
224:                        f = fw.next();
225:                        ++counter;
226:
227:                        if (counter == 5) {
228:                            fw.remove(); // removes multilinestring line
229:                        } else if (counter == 7) {
230:                            f.setAttribute("DESCRIPTION", "fubar");
231:                            fw.write();
232:                        } else {
233:                            f.setAttribute("DESCRIPTION", "foo"); // shouldn't affect data because I dont call write()
234:                        }
235:                    }
236:
237:                    // Appends a line
238:                    Feature newf = fw.next();
239:                    newf.setAttribute("DESCRIPTION", "newline");
240:                    fw.write();
241:
242:                    fw.close();
243:
244:                    // Reopens a writer to modify feature # 3
245:                    fw = in.getFeatureWriter();
246:                    f = fw.next();
247:                    f = fw.next();
248:                    f = fw.next();
249:                    f.setAttribute("NUM_OF_SEGMENTS", new Integer(179));
250:                    fw.write();
251:                    fw.close(); // should rewrite all other features
252:
253:                    FeatureReader fr = in.getFeatureReader();
254:                    counter = 0;
255:
256:                    while (fr.hasNext()) {
257:                        f = fr.next();
258:                        ++counter;
259:
260:                        String descr = (String) f.getAttribute("DESCRIPTION");
261:                        assertEquals(false, descr.equals("foo"));
262:
263:                        if (counter == 3) {
264:                            assertEquals(179, ((Integer) f
265:                                    .getAttribute("NUM_OF_SEGMENTS"))
266:                                    .intValue());
267:                        } else if (counter == 5) {
268:                            assertEquals("Single polygon with 2 holes", descr);
269:                        } else if (counter == 6) {
270:                            assertEquals("fubar", descr);
271:                        } else if (counter == 9) {
272:                            assertEquals("newline", descr);
273:                        }
274:                    }
275:
276:                    fr.close();
277:                    assertEquals(9, counter);
278:                } catch (Exception e) {
279:                    fail(e.getMessage());
280:                }
281:            }
282:
283:            /**
284:             * Test opening of two FeatureReaders on the same MIF file.
285:             */
286:            public void testConcurrentReader() {
287:                try {
288:                    MIFFile mif1 = new MIFFile(MIFTestUtils.fileName("grafo"), // .mif
289:                            MIFTestUtils.getParams("", "", null));
290:
291:                    MIFFile mif2 = new MIFFile(MIFTestUtils.fileName("grafo"), // .mif
292:                            MIFTestUtils.getParams("", "", null));
293:
294:                    FeatureReader fr1 = mif1.getFeatureReader();
295:
296:                    fr1.next();
297:
298:                    Feature f1 = fr1.next();
299:
300:                    FeatureReader fr2 = mif2.getFeatureReader();
301:
302:                    fr2.next();
303:
304:                    Feature f2 = fr2.next();
305:
306:                    for (int i = 0; i < f1.getNumberOfAttributes(); i++) {
307:                        assertEquals("Features are different", f1.getAttribute(
308:                                i).toString(), f2.getAttribute(i).toString());
309:                    }
310:
311:                    fr2.close();
312:                    fr1.close();
313:                } catch (Exception e) {
314:                    fail(e.getMessage());
315:                }
316:            }
317:
318:            /**
319:             * DOCUMENT ME!
320:             */
321:            public void testUntypedGeometryTypes() {
322:                try {
323:                    mif = new MIFFile(MIFTestUtils.fileName("mixed"), // .mif
324:                            MIFTestUtils.getParams("mif", "", null, "untyped"));
325:
326:                    FeatureType ft = mif.getSchema();
327:
328:                    assertEquals(ft.getDefaultGeometry().getType(),
329:                            Geometry.class);
330:
331:                    FeatureReader fr = mif.getFeatureReader();
332:
333:                    while (fr.hasNext()) {
334:                        Feature f = fr.next();
335:                        String geomtype = (String) f.getAttribute("GEOMTYPE");
336:
337:                        if (geomtype.equals("LINE")) {
338:                            geomtype = "LINESTRING";
339:                        }
340:
341:                        Geometry geom = (Geometry) f.getAttribute("the_geom");
342:
343:                        if (geom == null) {
344:                            assertEquals(geomtype, "NULL");
345:                        } else {
346:                            String gtype = geom.getClass().getName();
347:                            gtype = gtype.substring(28).toUpperCase();
348:
349:                            boolean compat = (geomtype.equals(gtype));
350:
351:                            // compat = compat || geomtype.equals("MULTI" + gtype);
352:                            assertTrue("Uncompatible types: " + gtype + ", "
353:                                    + geomtype, compat);
354:
355:                            if (geomtype.equals("POLYGON")) {
356:                                assertEquals("Bad number of holes",
357:                                        ((Integer) f
358:                                                .getAttribute("NUM_OF_HOLES"))
359:                                                .intValue(), ((Polygon) geom)
360:                                                .getNumInteriorRing());
361:                            }
362:                        }
363:                    }
364:
365:                    fr.close();
366:                } catch (Exception e) {
367:                    fail(e.getMessage());
368:                }
369:            }
370:
371:            /**
372:             * DOCUMENT ME!
373:             */
374:            public void testTypedGeometryUntyped() {
375:                doTestTyping("grafo", "untyped", Geometry.class,
376:                        LineString.class, false);
377:            }
378:
379:            /**
380:             * DOCUMENT ME!
381:             */
382:            public void testTypedGeometryTyped() {
383:                doTestTyping("grafo", "typed", LineString.class, false);
384:            }
385:
386:            /**
387:             * DOCUMENT ME!
388:             */
389:            public void testTypedGeometryTypesMulti() {
390:                doTestTyping("grafo", "multi", MultiLineString.class, false);
391:            }
392:
393:            /**
394:             * DOCUMENT ME!
395:             */
396:            public void testTypedGeometryTypesLineString() {
397:                doTestTyping("grafo", "LineString", LineString.class, false);
398:            }
399:
400:            /**
401:             * DOCUMENT ME!
402:             */
403:            public void testTypedGeometryTypesMultiLineString() {
404:                doTestTyping("grafo", "MultiLineString", MultiLineString.class,
405:                        false);
406:            }
407:
408:            /**
409:             * DOCUMENT ME!
410:             */
411:            public void testTypedGeometryTypesPoint() {
412:                // If I force to MultiLineString, the features read must already be MultiLineString
413:                doTestTyping("nodi", "Point", Point.class, false);
414:            }
415:
416:            public void testTypedGeometryText() {
417:                doTestTyping("text", "Text", Point.class, false);
418:            }
419:
420:            public void testTypedGeometryTextAuto() {
421:                doTestTyping("text", "Typed", Point.class, false);
422:            }
423:
424:            /**
425:             * DOCUMENT ME!
426:             */
427:            public void testTypedGeometryTypesMultiPoint() {
428:                // If I force to MultiLineString, the features read must already be MultiLineString
429:                doTestTyping("nodi", "multi", Point.class, false);
430:            }
431:
432:            private void doTestTyping(String typeName, String geomType,
433:                    Class geomClass, boolean errorExpected) {
434:                doTestTyping(typeName, geomType, geomClass, geomClass,
435:                        errorExpected);
436:            }
437:
438:            private void doTestTyping(String typeName, String geomType,
439:                    Class geomClass, Class instanceGeomClass,
440:                    boolean errorExpected) {
441:                try {
442:                    mif = new MIFFile(MIFTestUtils.fileName(typeName), // .mif
443:                            MIFTestUtils.getParams("mif", "", null, geomType));
444:
445:                    FeatureType ft = mif.getSchema();
446:
447:                    assertEquals(geomClass, ft.getDefaultGeometry().getType());
448:
449:                    FeatureReader fr = mif.getFeatureReader();
450:
451:                    try {
452:                        Feature f = fr.next();
453:                        Geometry geom = (Geometry) f.getAttribute("the_geom");
454:
455:                        if (errorExpected) {
456:                            fail("Expected error reading geometric attribute");
457:                        } else {
458:                            assertEquals(instanceGeomClass, geom.getClass());
459:                        }
460:                    } catch (Exception e) {
461:                        if (!errorExpected) {
462:                            fail(e.getMessage());
463:                        }
464:                    }
465:
466:                    fr.close();
467:                } catch (Exception e) {
468:                    if (!errorExpected) {
469:                        fail(e.getMessage());
470:                    }
471:                }
472:            }
473:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.