001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.gml2;
017:
018: import junit.framework.TestCase;
019: import org.w3c.dom.Document;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.transform.Transformer;
026: import javax.xml.transform.TransformerFactory;
027: import javax.xml.transform.dom.DOMSource;
028: import javax.xml.transform.stream.StreamResult;
029: import com.vividsolutions.jts.geom.Point;
030: import org.geotools.feature.Feature;
031: import org.geotools.xml.Parser;
032: import org.geotools.xml.StreamingParser;
033:
034: public class GMLApplicationSchemaParsingTest extends TestCase {
035: public void testStreamFeatureWithIncorrectSchemaLocation()
036: throws Exception {
037: InputStream in = getClass().getResourceAsStream("feature.xml");
038:
039: DocumentBuilderFactory factory = DocumentBuilderFactory
040: .newInstance();
041: factory.setNamespaceAware(true);
042:
043: Document document = factory.newDocumentBuilder().parse(in);
044:
045: //update hte schema location
046: document.getDocumentElement().removeAttribute(
047: "xsi:schemaLocation");
048:
049: //reserialize the document
050: File schemaFile = File.createTempFile("test", "xsd");
051: schemaFile.deleteOnExit();
052:
053: Transformer tx = TransformerFactory.newInstance()
054: .newTransformer();
055: tx.transform(new DOMSource(document), new StreamResult(
056: schemaFile));
057:
058: in.close();
059: in = new FileInputStream(schemaFile);
060:
061: GMLConfiguration configuration = new GMLConfiguration();
062: configuration.getProperties().add(
063: Parser.Properties.IGNORE_SCHEMA_LOCATION);
064: configuration.getProperties().add(
065: Parser.Properties.PARSE_UNKNOWN_ELEMENTS);
066:
067: StreamingParser parser = new StreamingParser(configuration, in,
068: "//TestFeature");
069:
070: for (int i = 0; i < 3; i++) {
071: Feature f = (Feature) parser.parse();
072:
073: assertNotNull(f);
074: }
075:
076: assertNull(parser.parse());
077:
078: try {
079: in.close();
080: } catch (IOException e) {
081: // nothing to do, but this throws an exception under java 6
082: }
083: }
084:
085: public void testStreamPointWithIncorrectSchemaLocation()
086: throws Exception {
087: InputStream in = getClass().getResourceAsStream("feature.xml");
088:
089: DocumentBuilderFactory factory = DocumentBuilderFactory
090: .newInstance();
091: factory.setNamespaceAware(true);
092:
093: Document document = factory.newDocumentBuilder().parse(in);
094:
095: //update hte schema location
096: document.getDocumentElement().removeAttribute(
097: "xsi:schemaLocation");
098:
099: //reserialize the document
100: File schemaFile = File.createTempFile("test", "xsd");
101: schemaFile.deleteOnExit();
102:
103: Transformer tx = TransformerFactory.newInstance()
104: .newTransformer();
105: tx.transform(new DOMSource(document), new StreamResult(
106: schemaFile));
107:
108: in.close();
109: in = new FileInputStream(schemaFile);
110:
111: GMLConfiguration configuration = new GMLConfiguration();
112: configuration.getProperties().add(
113: Parser.Properties.IGNORE_SCHEMA_LOCATION);
114: configuration.getProperties().add(
115: Parser.Properties.PARSE_UNKNOWN_ELEMENTS);
116:
117: StreamingParser parser = new StreamingParser(configuration, in,
118: "//Point");
119:
120: for (int i = 0; i < 3; i++) {
121: Point p = (Point) parser.parse();
122:
123: assertNotNull(p);
124: assertEquals(i, p.getX(), 0d);
125: assertEquals(i, p.getY(), 0d);
126: }
127:
128: assertNull(parser.parse());
129:
130: try {
131: in.close();
132: } catch (IOException e) {
133: // nothing to do, but this throws an exception under java 6
134: }
135: }
136:
137: public void testWithCorrectSchemaLocation() throws Exception {
138: InputStream in = getClass().getResourceAsStream("feature.xml");
139: DocumentBuilderFactory factory = DocumentBuilderFactory
140: .newInstance();
141: factory.setNamespaceAware(true);
142:
143: Document document = factory.newDocumentBuilder().parse(in);
144:
145: //update hte schema location
146: String schemaLocation = getClass().getResource("test.xsd")
147: .toString();
148: document.getDocumentElement().setAttribute(
149: "xsi:schemaLocation",
150: TEST.NAMESPACE + " " + schemaLocation);
151:
152: //reserialize the document
153: File schemaFile = File.createTempFile("test", "xsd");
154: schemaFile.deleteOnExit();
155:
156: Transformer tx = TransformerFactory.newInstance()
157: .newTransformer();
158: tx.transform(new DOMSource(document), new StreamResult(
159: schemaFile));
160:
161: in.close();
162: in = new FileInputStream(schemaFile);
163:
164: StreamingParser parser = new StreamingParser(
165: new GMLConfiguration(), in, "//TestFeature");
166:
167: for (int i = 0; i < 3; i++) {
168: Feature f = (Feature) parser.parse();
169: assertNotNull(f);
170:
171: assertEquals(i + "", f.getID());
172: assertEquals(i, ((Point) f.getDefaultGeometry()).getX(), 0d);
173: assertEquals(i, ((Point) f.getDefaultGeometry()).getY(), 0d);
174: assertEquals(i, ((Integer) f.getAttribute("count"))
175: .intValue());
176: }
177:
178: assertNull(parser.parse());
179:
180: try {
181: in.close();
182: } catch (IOException e) {
183: // nothing to do, but this throws an exception under java 6
184: }
185: }
186: }
|