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.gml3;
017:
018: import junit.framework.TestCase;
019: import org.apache.xerces.parsers.SAXParser;
020: import org.eclipse.xsd.XSDSchema;
021: import org.xml.sax.InputSource;
022: import org.xml.sax.SAXException;
023: import org.xml.sax.SAXParseException;
024: import org.xml.sax.helpers.DefaultHandler;
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.File;
028: import java.net.URL;
029: import java.net.URLConnection;
030: import java.util.ArrayList;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.gml3.bindings.TEST;
033: import org.geotools.gml3.bindings.TestConfiguration;
034: import org.geotools.xml.Encoder;
035: import org.geotools.xml.Parser;
036: import org.geotools.xml.SchemaLocator;
037:
038: public class GML3EncodingTest extends TestCase {
039: boolean isOffline() throws Exception {
040: //this test will only run if network is available
041: URL url = new URL("http://schemas.opengis.net");
042:
043: try {
044: URLConnection conn = url.openConnection();
045: conn.getInputStream().read();
046: } catch (Exception e) {
047: return true;
048: }
049:
050: return false;
051: }
052:
053: public void testWithConfiguration() throws Exception {
054: if (isOffline()) {
055: return;
056: }
057:
058: TestConfiguration configuration = new TestConfiguration();
059:
060: //first parse in test data
061: Parser parser = new Parser(configuration);
062: FeatureCollection fc = (FeatureCollection) parser
063: .parse(TestConfiguration.class
064: .getResourceAsStream("test.xml"));
065: assertNotNull(fc);
066:
067: SchemaLocator locator = new SchemaLocator(configuration);
068: XSDSchema schema = locator.locateSchema(null, TEST.NAMESPACE,
069: null, null);
070: assertNotNull(schema);
071:
072: Encoder encoder = new Encoder(configuration, schema);
073: ByteArrayOutputStream output = new ByteArrayOutputStream();
074: encoder.write(fc, TEST.TestFeatureCollection, output);
075:
076: SAXParser saxParser = new SAXParser();
077: saxParser.setFeature("http://xml.org/sax/features/validation",
078: true);
079: saxParser.setFeature(
080: "http://apache.org/xml/features/validation/schema",
081: true);
082: saxParser
083: .setFeature(
084: "http://apache.org/xml/features/validation/schema-full-checking",
085: true);
086:
087: saxParser
088: .setProperty(
089: "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
090: TEST.NAMESPACE);
091:
092: saxParser
093: .setProperty(
094: "http://apache.org/xml/properties/schema/external-schemaLocation",
095: TEST.NAMESPACE + " "
096: + configuration.getSchemaFileURL());
097:
098: final ArrayList errors = new ArrayList();
099: DefaultHandler handler = new DefaultHandler() {
100: public void error(SAXParseException e) throws SAXException {
101: System.out.println(e.getMessage());
102: errors.add(e);
103: }
104:
105: public void fatalError(SAXParseException e)
106: throws SAXException {
107: System.out.println(e.getMessage());
108: errors.add(e);
109: }
110: };
111:
112: saxParser.setErrorHandler(handler);
113: saxParser.parse(new InputSource(new ByteArrayInputStream(output
114: .toByteArray())));
115:
116: assertTrue(errors.isEmpty());
117: }
118:
119: public void testWithApplicationSchemaConfiguration()
120: throws Exception {
121: if (isOffline()) {
122: return;
123: }
124:
125: String schemaLocation = new File(TestConfiguration.class
126: .getResource("test.xsd").getFile()).getAbsolutePath();
127:
128: ApplicationSchemaConfiguration configuration = new ApplicationSchemaConfiguration(
129: TEST.NAMESPACE, schemaLocation);
130:
131: //first parse in test data
132: Parser parser = new Parser(configuration);
133: FeatureCollection fc = (FeatureCollection) parser
134: .parse(TestConfiguration.class
135: .getResourceAsStream("test.xml"));
136: assertNotNull(fc);
137:
138: SchemaLocator locator = new SchemaLocator(configuration);
139: XSDSchema schema = locator.locateSchema(null, TEST.NAMESPACE,
140: null, null);
141: assertNotNull(schema);
142:
143: Encoder encoder = new Encoder(configuration, schema);
144:
145: ByteArrayOutputStream output = new ByteArrayOutputStream();
146: encoder.write(fc, TEST.TestFeatureCollection, output);
147:
148: SAXParser saxParser = new SAXParser();
149: saxParser.setFeature("http://xml.org/sax/features/validation",
150: true);
151: saxParser.setFeature(
152: "http://apache.org/xml/features/validation/schema",
153: true);
154: saxParser
155: .setFeature(
156: "http://apache.org/xml/features/validation/schema-full-checking",
157: true);
158:
159: saxParser
160: .setProperty(
161: "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
162: TEST.NAMESPACE);
163:
164: saxParser
165: .setProperty(
166: "http://apache.org/xml/properties/schema/external-schemaLocation",
167: TEST.NAMESPACE + " "
168: + configuration.getSchemaFileURL());
169:
170: final ArrayList errors = new ArrayList();
171: DefaultHandler handler = new DefaultHandler() {
172: public void error(SAXParseException e) throws SAXException {
173: System.out.println(e.getMessage());
174: errors.add(e);
175: }
176:
177: public void fatalError(SAXParseException e)
178: throws SAXException {
179: System.out.println(e.getMessage());
180: errors.add(e);
181: }
182: };
183:
184: saxParser.setErrorHandler(handler);
185: saxParser.parse(new InputSource(new ByteArrayInputStream(output
186: .toByteArray())));
187:
188: assertTrue(errors.isEmpty());
189: }
190: }
|