Skip to content

Commit

Permalink
Stop this test from accessing the internet while we have all the reso…
Browse files Browse the repository at this point in the history
…urces it needs on the local disk
  • Loading branch information
aaime committed Jan 15, 2013
1 parent 6d536f2 commit 321ca68
Showing 1 changed file with 70 additions and 2 deletions.
Expand Up @@ -17,17 +17,24 @@

package org.geotools.gml3.v3_2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.URIHandlerImpl;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.impl.XSDSchemaImpl;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.geotools.data.DataUtilities;
import org.geotools.xml.Configuration;
import org.geotools.xml.SchemaIndex;
import org.geotools.xml.Schemas;
Expand Down Expand Up @@ -106,8 +113,32 @@ public void test_GM_Point_PropertyType() {
@SuppressWarnings("unchecked")
private static void checkChildElementTypes(String filename, String namespace, String name) {
ResourceSet resourceSet = XSDSchemaImpl.createResourceSet();
XSDResourceImpl resource = (XSDResourceImpl) resourceSet.getResource(
URI.createURI(EmfXsdLoadTest.class.getResource(filename).toString()), true);
resourceSet
.getURIConverter()
.getURIHandlers()
.add(0,
new LocalURIHanlder("http://schemas.opengis.net/iso/19139/20070417",
"file://./src/main/resources/org/geotools/gml3/v3_2"));
resourceSet
.getURIConverter()
.getURIHandlers()
.add(0,
new LocalURIHanlder("http://schemas.opengis.net/gml/3.2.1",
"file://./src/main/resources/org/geotools/gml3/v3_2"));
resourceSet
.getURIConverter()
.getURIHandlers()
.add(0,
new LocalURIHanlder("http://www.w3.org/1999/xlink.xsd",
"file://../xsd-core/src/main/resources/org/geotools/xlink/xlink.xsd"));
resourceSet
.getURIConverter()
.getURIHandlers()
.add(0,
new LocalURIHanlder("http://www.w3.org/2001/xml.xsd",
"file://../xsd-core/src/main/resources/org/geotools/xml/xml.xsd"));
URI resourceLocation = URI.createURI(EmfXsdLoadTest.class.getResource(filename).toString());
XSDResourceImpl resource = (XSDResourceImpl) resourceSet.getResource(resourceLocation, true);
XSDSchema schema = resource.getSchema();
Assert.assertNotNull(schema);
SchemaIndex index = null;
Expand Down Expand Up @@ -135,5 +166,42 @@ private static void checkChildElementTypes(String filename, String namespace, St
System.err.println();
Assert.assertFalse("Unexpected child element declaration with null type", foundNull);
}

private static class LocalURIHanlder extends URIHandlerImpl {

String remotePrefix;
String localPrefix;

public LocalURIHanlder(String remotePrefix, String localPrefix) {
super();
this.remotePrefix = remotePrefix;
this.localPrefix = localPrefix;
}

@Override
public boolean canHandle(URI uri) {
// System.out.println("Looking into " + uri);

if(uri.isFile()) {
return false;
}
String location = uri.toString();
if(location.startsWith(remotePrefix)) {
return true;
}
return false;
};

@Override
public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException {
String updated = uri.toString().replace(remotePrefix, localPrefix);
File canonical = DataUtilities.urlToFile(new URL(updated)).getCanonicalFile();
if(!canonical.exists()) {
throw new RuntimeException("Failed to locate file on the local filesystem: " + canonical.getAbsolutePath());
}
// System.out.println("Handled " + uri + " into " + canonical.getAbsolutePath());
return super.createInputStream(URI.createFileURI(canonical.getAbsolutePath()), options);
}
}

}

0 comments on commit 321ca68

Please sign in to comment.