This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 150edb7caee24d5d4bb21dbcd8ab6158ee33ea90 Author: Martin Desruisseaux <[email protected]> AuthorDate: Tue Dec 19 10:21:56 2023 +0100 Avoid attempt to download the document at the "http://test.net" URL. --- .../org/apache/sis/xml/XLinkMarshallingTest.java | 10 ++-- .../test/org/apache/sis/xml/test/TestCase.java | 7 +++ .../apache/sis/xml/test/TestReferenceResolver.java | 56 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/XLinkMarshallingTest.java b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/XLinkMarshallingTest.java index 8101c976a4..c793efd27e 100644 --- a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/XLinkMarshallingTest.java +++ b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/XLinkMarshallingTest.java @@ -48,7 +48,7 @@ public final class XLinkMarshallingTest extends TestCase { private static final String LINK_ONLY_XML = "<mdb:MD_Metadata xmlns:mdb=\"" + Namespaces.MDB + '"' + " xmlns:xlink=\"" + Namespaces.XLINK + "\">\n" + - " <mdb:identificationInfo xlink:href=\"http://test.net\"/>\n" + + " <mdb:identificationInfo xlink:href=\"" + DUMMY_URL + "\"/>\n" + "</mdb:MD_Metadata>"; /** @@ -59,7 +59,7 @@ public final class XLinkMarshallingTest extends TestCase { " xmlns:mri=\"" + Namespaces.MRI + '"' + " xmlns:gco=\"" + Namespaces.GCO + '"' + " xmlns:xlink=\"" + Namespaces.XLINK + "\">\n" + - " <mdb:identificationInfo xlink:href=\"http://test.net\">\n" + + " <mdb:identificationInfo xlink:href=\"" + DUMMY_URL + "\">\n" + " <mri:MD_DataIdentification>\n" + " <mri:abstract>\n" + " <gco:CharacterString>This is a test.</gco:CharacterString>\n" + @@ -79,7 +79,7 @@ public final class XLinkMarshallingTest extends TestCase { assertEquals(isNilExpected, identification instanceof NilObject, "NilObject"); assertInstanceOf(IdentifiedObject.class, identification, "Identification"); final XLink xlink = ((IdentifiedObject) identification).getIdentifierMap().getSpecialized(IdentifierSpace.XLINK); - assertEquals("http://test.net", xlink.getHRef().toString(), "xlink:href"); + assertEquals(DUMMY_URL, xlink.getHRef().toString(), "xlink:href"); } /** @@ -104,7 +104,7 @@ public final class XLinkMarshallingTest extends TestCase { @Test public void testLinkOnly() throws JAXBException, URISyntaxException { final XLink xlink = new XLink(); - xlink.setHRef(new URI("http://test.net")); + xlink.setHRef(new URI(DUMMY_URL)); final DefaultDataIdentification identification = new DefaultDataIdentification(); identification.getIdentifierMap().putSpecialized(IdentifierSpace.XLINK, xlink); final DefaultMetadata metadata = new DefaultMetadata(); @@ -136,7 +136,7 @@ public final class XLinkMarshallingTest extends TestCase { @Test public void testWithElement() throws JAXBException, URISyntaxException { final XLink xlink = new XLink(); - xlink.setHRef(new URI("http://test.net")); + xlink.setHRef(new URI(DUMMY_URL)); final DefaultDataIdentification identification = new DefaultDataIdentification(); identification.getIdentifierMap().putSpecialized(IdentifierSpace.XLINK, xlink); identification.setAbstract(new SimpleInternationalString("This is a test.")); diff --git a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestCase.java b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestCase.java index 583e05cf35..227147518c 100644 --- a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestCase.java +++ b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestCase.java @@ -65,6 +65,12 @@ import static org.apache.sis.metadata.Assertions.assertXmlEquals; * @see DocumentComparator */ public abstract class TestCase extends org.apache.sis.test.TestCase { + /** + * A dummy URL to not try to load. This URL is handled in a special way by the unmarshallers created + * by {@link #getMarshallerPool()}: they will not try to download the document at this address. + */ + protected static final String DUMMY_URL = "http://test.net"; + /** * Miscellaneous version constants used for ISO standards. */ @@ -140,6 +146,7 @@ public abstract class TestCase extends org.apache.sis.test.TestCase { assertNull(properties.put(XML.LOCALE, Locale.UK)); assertNull(properties.put(XML.TIMEZONE, TIMEZONE)); assertNull(properties.put(XML.LENIENT_UNMARSHAL, Boolean.TRUE)); + assertNull(properties.put(XML.RESOLVER, new TestReferenceResolver(DUMMY_URL))); defaultPool = new MarshallerPool(properties); } return defaultPool; diff --git a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestReferenceResolver.java b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestReferenceResolver.java new file mode 100644 index 0000000000..0fb00e1d5d --- /dev/null +++ b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/test/TestReferenceResolver.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.xml.test; + +import java.net.URI; +import org.apache.sis.xml.XLink; +import org.apache.sis.xml.MarshalContext; +import org.apache.sis.xml.ReferenceResolver; + + +/** + * The reference resolver to use for testing purpose. + * This resolver blocks attempts to download documents at test (non-real) URLs. + * + * @author Martin Desruisseaux (Geomatys) + */ +final class TestReferenceResolver extends ReferenceResolver { + /** + * A dummy URI to not try to load. + */ + private final URI ignore; + + /** + * Creates a default {@code ReferenceResolver}. + * + * @param dummyURI the dummy URL to ignore. + */ + TestReferenceResolver(final String dummyURI) { + ignore = URI.create(dummyURI); + } + + /** + * Returns an object of the given type for the given {@code xlink} attribute, except if it is a test URL. + */ + @Override + public <T> T resolve(final MarshalContext context, final Class<T> type, final XLink link) { + if (ignore.equals(link.getHRef())) { + return null; + } + return super.resolve(context, type, link); + } +}
