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.extensions.TestSetup;
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import org.eclipse.xsd.XSDSchema;
023: import org.eclipse.xsd.util.XSDSchemaLocationResolver;
024: import java.io.File;
025: import java.io.FileNotFoundException;
026: import java.net.URI;
027: import java.util.List;
028: import org.geotools.xml.Configuration;
029: import org.geotools.xml.Schemas;
030: import org.geotools.xs.XSConfiguration;
031:
032: /**
033: * Structure of the files in the test folder:
034: *
035: * <pre>
036: * <code>
037: * a.xsd
038: * b.xsd
039: * c.xsd
040: * </code>
041: * Where a.xsd includes b.xsd, a.xsd imports c.xsd, c.xsd imports subfolder/d.xsd.
042: * </pre>
043: *
044: * @author Gabriel Roldan
045: * @version $Id: ApplicationSchemaConfigurationTest.java 29135 2008-02-07 19:49:09Z desruisseaux $
046: * @URL $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/xml-gml3/src/test/java/org/geotools/gml3/ApplicationSchemaConfigurationTest.java $
047: * @since 2.4.x
048: *
049: */
050: public class ApplicationSchemaConfigurationTest extends TestCase {
051: private static final String namespace = "http://www.geotools.org/test";
052: private static ApplicationSchemaConfiguration configuration;
053: private static XSDSchema rootSchema;
054: private static String rootSchemaLocationFolder;
055: private static String rootSchemaLocation;
056:
057: /**
058: * Builds a test suite for all this class' tests with per suite
059: * initialization directed to {@link #oneTimeSetUp()} and per suite clean up
060: * directed to {@link #oneTimeTearDown()}
061: *
062: * @return
063: */
064: public static Test suite() {
065: TestSuite suite = new TestSuite();
066: suite.addTestSuite(ApplicationSchemaConfigurationTest.class);
067:
068: TestSetup wrapper = new TestSetup(suite) {
069: protected void setUp() throws Exception {
070: oneTimeSetUp();
071: }
072:
073: protected void tearDown() {
074: oneTimeTearDown();
075: }
076: };
077:
078: return wrapper;
079: }
080:
081: private static void oneTimeSetUp() throws Exception {
082: // test files located in src/test/resources/<this class package name>
083: final String testFilesFolderName = "./ApplicationSchemaConfigurationTestFiles";
084: final File file = new File(new URI(
085: ApplicationSchemaConfigurationTest.class.getResource(
086: testFilesFolderName).toExternalForm()));
087:
088: if (!file.exists()) {
089: throw new FileNotFoundException(
090: "test resources folder does not exists: "
091: + file.getAbsolutePath());
092: }
093:
094: rootSchemaLocationFolder = file.toURI().toString();
095: rootSchemaLocation = new File(file, "a.xsd").toURI().toString();
096: configuration = new ApplicationSchemaConfiguration(namespace,
097: rootSchemaLocation);
098:
099: rootSchema = Schemas.parse(rootSchemaLocation);
100: }
101:
102: private static void oneTimeTearDown() {
103: configuration = null;
104: rootSchema = null;
105: }
106:
107: /**
108: */
109: protected void setUp() throws Exception {
110: super .setUp();
111:
112: // may be we're running a single test instead of the whose suite
113: if (configuration == null) {
114: oneTimeSetUp();
115: }
116: }
117:
118: protected void tearDown() throws Exception {
119: super .tearDown();
120: }
121:
122: public void testGetDependencies() {
123: List dependencies = configuration.getDependencies();
124: assertNotNull(dependencies);
125: assertEquals(2, dependencies.size());
126:
127: Configuration dep1 = (Configuration) dependencies.get(0);
128: Configuration dep2 = (Configuration) dependencies.get(1);
129:
130: assertTrue(dep1 instanceof XSConfiguration);
131: assertTrue(dep2 instanceof GMLConfiguration);
132: }
133:
134: public void testGetSchemaLocationResolverPreconditions()
135: throws Exception {
136: final XSDSchemaLocationResolver resolver = configuration
137: .getSchemaLocationResolver();
138:
139: //null schema is allowed if for the same namespace than the configuration
140: String location = resolver.resolveSchemaLocation(null,
141: namespace, "a.xsd");
142: assertEquals(rootSchemaLocation, location);
143:
144: try {
145: //but now allowed otherwise
146: resolver.resolveSchemaLocation(null, namespace + "/2", "");
147: fail("Expected NPE on null schema");
148: } catch (NullPointerException e) {
149: assertTrue(true);
150: }
151:
152: try {
153: resolver.resolveSchemaLocation(rootSchema, null, "");
154: fail("Expected NPE on null uri");
155: } catch (NullPointerException e) {
156: assertTrue(true);
157: }
158:
159: try {
160: resolver.resolveSchemaLocation(rootSchema, namespace, null);
161: fail("Expected NPE on null schema location");
162: } catch (NullPointerException e) {
163: assertTrue(true);
164: }
165: }
166:
167: /**
168: * a.xsd include b.xsd
169: */
170: public void testGetSchemaLocationResolverRelativeInclude()
171: throws Exception {
172: final XSDSchemaLocationResolver resolver = configuration
173: .getSchemaLocationResolver();
174:
175: // simulate an include, empty locationUri
176: final String locationUri = "";
177: final String schemaLocation = "b.xsd";
178:
179: final String importLocation = resolver.resolveSchemaLocation(
180: rootSchema, locationUri, schemaLocation);
181: final String expectedLocation = new File(new File(new URI(
182: rootSchemaLocationFolder)), schemaLocation).toURL()
183: .toExternalForm();
184: assertEquals(expectedLocation, importLocation);
185: }
186:
187: /**
188: * a.xsd imports c.xsd
189: */
190: public void testGetSchemaLocationResolverRelativeImport()
191: throws Exception {
192: final XSDSchemaLocationResolver resolver = configuration
193: .getSchemaLocationResolver();
194:
195: // simulate a relative path import
196: final String locationUri = "http://www.geotools.org/test/2";
197: final String schemaLocation = "c.xsd";
198:
199: final String importLocation = resolver.resolveSchemaLocation(
200: rootSchema, locationUri, schemaLocation);
201: final String expectedLocation = new File(new File(new URI(
202: rootSchemaLocationFolder)), schemaLocation).toURL()
203: .toExternalForm();
204: assertEquals(expectedLocation, importLocation);
205: }
206:
207: /**
208: * a.xsd imports http://www.w3.org/2001/xml.xsd
209: */
210: public void testGetSchemaLocationResolverAbsoluteImport()
211: throws Exception {
212: final XSDSchemaLocationResolver resolver = configuration
213: .getSchemaLocationResolver();
214:
215: // simulate an absolute path import
216: final String locationUri = "http://www.w3.org/XML/1998/namespace";
217: final String schemaLocation = "http://www.w3.org/2001/xml.xsd";
218:
219: final String importLocation = resolver.resolveSchemaLocation(
220: rootSchema, locationUri, schemaLocation);
221:
222: assertNull(importLocation);
223: }
224: }
|