This is an automated email from the ASF dual-hosted git repository. veithen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ws-axiom.git
The following commit(s) were added to refs/heads/master by this push: new 18a17eaf9 Refactor DataSourceRegistry so that it can be used with Blob 18a17eaf9 is described below commit 18a17eaf9cf18782060cbcb40ab4aecdfea4b2fd Author: Andreas Veithen <andreas.veit...@gmail.com> AuthorDate: Mon Nov 7 00:12:18 2022 +0000 Refactor DataSourceRegistry so that it can be used with Blob --- testing/axiom-testsuite/pom.xml | 5 ++ .../axiom/ts/om/builder/TestCloseWithSystemId.java | 6 +- testing/dom-testsuite/pom.xml | 5 ++ .../apache/axiom/ts/dom/builder/TestParseURI.java | 6 +- testing/pom.xml | 1 + testing/{ => url-registry}/pom.xml | 67 ++++++++++------------ .../axiom/net/protocol/registry/DataProvider.java} | 10 ++-- .../axiom/net/protocol/registry}/Handler.java | 12 ++-- .../net/protocol/registry/URLConnectionImpl.java} | 17 +++--- .../net/protocol/registry/URLRegistration.java} | 5 +- .../axiom/net/protocol/registry/URLRegistry.java} | 42 ++++++++------ .../net/protocol/registry/URLRegistryTest.java} | 15 +++-- testing/xml-testsuite/pom.xml | 5 ++ .../axiom/ts/xml/ComputedMessageContent.java | 27 +-------- 14 files changed, 104 insertions(+), 119 deletions(-) diff --git a/testing/axiom-testsuite/pom.xml b/testing/axiom-testsuite/pom.xml index c99b55b62..88147d813 100644 --- a/testing/axiom-testsuite/pom.xml +++ b/testing/axiom-testsuite/pom.xml @@ -69,6 +69,11 @@ <artifactId>testutils</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>url-registry</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>xml-testsuite</artifactId> diff --git a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java index db82cb93c..df8f7cd05 100644 --- a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java +++ b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java @@ -23,12 +23,12 @@ import static com.google.common.truth.Truth.assertThat; import javax.activation.URLDataSource; import javax.xml.transform.stream.StreamSource; +import org.apache.axiom.net.protocol.registry.URLRegistration; +import org.apache.axiom.net.protocol.registry.URLRegistry; import org.apache.axiom.om.OMMetaFactory; import org.apache.axiom.om.OMXMLBuilderFactory; import org.apache.axiom.om.OMXMLParserWrapper; import org.apache.axiom.testutils.activation.InstrumentedDataSource; -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration; -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistry; import org.apache.axiom.ts.AxiomTestCase; import org.apache.axiom.ts.xml.XMLSample; @@ -41,7 +41,7 @@ public class TestCloseWithSystemId extends AxiomTestCase { protected void runTest() throws Throwable { InstrumentedDataSource ds = new InstrumentedDataSource(new URLDataSource(XMLSample.SIMPLE.getUrl())); - DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds); + URLRegistration registration = URLRegistry.register(ds); try { OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder( diff --git a/testing/dom-testsuite/pom.xml b/testing/dom-testsuite/pom.xml index 4a68f83a4..4d750c1f2 100644 --- a/testing/dom-testsuite/pom.xml +++ b/testing/dom-testsuite/pom.xml @@ -53,6 +53,11 @@ <artifactId>testutils</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>url-registry</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>xml-testsuite</artifactId> diff --git a/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java b/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java index 35489484d..5b06695ec 100644 --- a/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java +++ b/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestParseURI.java @@ -24,9 +24,9 @@ import javax.activation.URLDataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import org.apache.axiom.net.protocol.registry.URLRegistration; +import org.apache.axiom.net.protocol.registry.URLRegistry; import org.apache.axiom.testutils.activation.InstrumentedDataSource; -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration; -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistry; import org.apache.axiom.ts.dom.DOMTestCase; import org.apache.axiom.ts.xml.XMLSample; import org.w3c.dom.Document; @@ -43,7 +43,7 @@ public class TestParseURI extends DOMTestCase { protected void runTest() throws Throwable { InstrumentedDataSource ds = new InstrumentedDataSource(new URLDataSource( XMLSample.SIMPLE.getUrl())); - DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds); + URLRegistration registration = URLRegistry.register(ds); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(registration.getURL().toExternalForm()); diff --git a/testing/pom.xml b/testing/pom.xml index 3cbe84015..74abd2076 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -44,6 +44,7 @@ <module>xml-testsuite</module> <module>xml-truth</module> <module>jaxp-testsuite</module> + <module>url-registry</module> </modules> <build> diff --git a/testing/pom.xml b/testing/url-registry/pom.xml similarity index 51% copy from testing/pom.xml copy to testing/url-registry/pom.xml index 3cbe84015..13e9c608b 100644 --- a/testing/pom.xml +++ b/testing/url-registry/pom.xml @@ -22,53 +22,46 @@ <parent> <groupId>org.apache.ws.commons.axiom</groupId> - <artifactId>axiom</artifactId> + <artifactId>testing</artifactId> <version>2.0.0-SNAPSHOT</version> </parent> - <artifactId>testing</artifactId> - <packaging>pom</packaging> + <artifactId>url-registry</artifactId> - <name>Testing</name> + <url>http://ws.apache.org/axiom/</url> - <modules> - <module>axiom-testsuite</module> - <module>axiom-truth</module> - <module>dom-testsuite</module> - <module>jaxen-testsuite</module> - <module>multiton</module> - <module>saaj-testsuite</module> - <module>soap-testsuite</module> - <module>spring-ws-testsuite</module> - <module>testutils</module> - <module>xml-testsuite</module> - <module>xml-truth</module> - <module>jaxp-testsuite</module> - </modules> + <dependencies> + <dependency> + <groupId>jakarta.activation</groupId> + <artifactId>jakarta.activation-api</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>testutils</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> <build> <plugins> <plugin> - <groupId>com.github.veithen.maven</groupId> - <artifactId>jacoco-report-maven-plugin</artifactId> - <configuration> - <includeClasses>false</includeClasses> - </configuration> + <groupId>com.spotify.fmt</groupId> + <artifactId>fmt-maven-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>check</goal> + </goals> + </execution> + </executions> </plugin> </plugins> </build> - - <reporting> - <plugins> - <plugin> - <artifactId>maven-project-info-reports-plugin</artifactId> - <inherited>false</inherited> - <reportSets> - <reportSet> - <reports /> - </reportSet> - </reportSets> - </plugin> - </plugins> - </reporting> </project> diff --git a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/DataProvider.java similarity index 81% copy from testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java copy to testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/DataProvider.java index 4e9dfabfe..e27102f3e 100644 --- a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java +++ b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/DataProvider.java @@ -16,11 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.axiom.testutils.net.protocol.mem; +package org.apache.axiom.net.protocol.registry; -import java.net.URL; +import java.io.IOException; +import java.io.InputStream; -public interface DataSourceRegistration { - URL getURL(); - void unregister(); +public interface DataProvider { + InputStream getInputStream() throws IOException; } diff --git a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/Handler.java similarity index 76% rename from testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java rename to testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/Handler.java index 1bcc6c5e0..7633f42d8 100644 --- a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java +++ b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/Handler.java @@ -16,22 +16,20 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.axiom.testutils.net.protocol.mem; +package org.apache.axiom.net.protocol.registry; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; -import javax.activation.DataSource; - public final class Handler extends URLStreamHandler { @Override protected URLConnection openConnection(URL url) throws IOException { - DataSource dataSource = DataSourceRegistry.lookupDataSource(url.getPath()); - if (dataSource == null) { - throw new IOException("DataSource not found"); + DataProvider dataProvider = URLRegistry.lookup(url.getPath()); + if (dataProvider == null) { + throw new IOException("URL registration not found"); } - return new DataSourceURLConnection(url, dataSource); + return new URLConnectionImpl(url, dataProvider); } } diff --git a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLConnectionImpl.java similarity index 72% rename from testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java rename to testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLConnectionImpl.java index c41e23b8f..2aac07ef6 100644 --- a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java +++ b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLConnectionImpl.java @@ -16,29 +16,26 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.axiom.testutils.net.protocol.mem; +package org.apache.axiom.net.protocol.registry; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; -import javax.activation.DataSource; +final class URLConnectionImpl extends URLConnection { + private final DataProvider dataProvider; -final class DataSourceURLConnection extends URLConnection { - private final DataSource dataSource; - - DataSourceURLConnection(URL url, DataSource dataSource) { + URLConnectionImpl(URL url, DataProvider dataProvider) { super(url); - this.dataSource = dataSource; + this.dataProvider = dataProvider; } @Override - public void connect() throws IOException { - } + public void connect() throws IOException {} @Override public InputStream getInputStream() throws IOException { - return dataSource.getInputStream(); + return dataProvider.getInputStream(); } } diff --git a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLRegistration.java similarity index 90% rename from testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java rename to testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLRegistration.java index 4e9dfabfe..e9d83d1a8 100644 --- a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java +++ b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLRegistration.java @@ -16,11 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.axiom.testutils.net.protocol.mem; +package org.apache.axiom.net.protocol.registry; import java.net.URL; -public interface DataSourceRegistration { +public interface URLRegistration { URL getURL(); + void unregister(); } diff --git a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLRegistry.java similarity index 64% rename from testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java rename to testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLRegistry.java index 93257b34e..83cd62aef 100644 --- a/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java +++ b/testing/url-registry/src/main/java/org/apache/axiom/net/protocol/registry/URLRegistry.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.axiom.testutils.net.protocol.mem; +package org.apache.axiom.net.protocol.registry; import java.net.MalformedURLException; import java.net.URL; @@ -27,50 +27,54 @@ import java.util.UUID; import javax.activation.DataSource; -public final class DataSourceRegistry { +public final class URLRegistry { private static boolean handlerRegistered; - private static final Map<String,DataSource> dataSources = new HashMap<>(); - - private DataSourceRegistry() {} + private static final Map<String, DataProvider> dataProviders = new HashMap<>(); - public synchronized static DataSourceRegistration registerDataSource(DataSource dataSource) { + private URLRegistry() {} + + public static synchronized URLRegistration register(DataProvider dataProvider) { if (!handlerRegistered) { Properties systemProps = System.getProperties(); synchronized (systemProps) { - StringBuilder pkgs = new StringBuilder(systemProps.getProperty("java.protocol.handler.pkgs", "")); + StringBuilder pkgs = + new StringBuilder( + systemProps.getProperty("java.protocol.handler.pkgs", "")); if (pkgs.length() > 0) { pkgs.append('|'); } - pkgs.append("org.apache.axiom.testutils.net.protocol"); + pkgs.append("org.apache.axiom.net.protocol"); systemProps.setProperty("java.protocol.handler.pkgs", pkgs.toString()); } handlerRegistered = true; } final String id = UUID.randomUUID().toString(); - dataSources.put(id, dataSource); - return new DataSourceRegistration() { + dataProviders.put(id, dataProvider); + return new URLRegistration() { @Override public URL getURL() { try { - // Note: Woodstox expects that URL schemes are between 3 and 8 characters long; - // that's why we chose "mem" and not "ds". - return new URL("mem", "", id); + return new URL("registry", "", id); } catch (MalformedURLException ex) { // We should never get here throw new Error(ex); } } - + @Override public void unregister() { - synchronized (DataSourceRegistry.class) { - dataSources.remove(id); + synchronized (URLRegistry.class) { + dataProviders.remove(id); } } }; } - - static synchronized DataSource lookupDataSource(String id) { - return dataSources.get(id); + + public static URLRegistration register(DataSource dataSource) { + return register(dataSource::getInputStream); + } + + static synchronized DataProvider lookup(String id) { + return dataProviders.get(id); } } diff --git a/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java b/testing/url-registry/src/test/java/org/apache/axiom/net/protocol/registry/URLRegistryTest.java similarity index 73% rename from testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java rename to testing/url-registry/src/test/java/org/apache/axiom/net/protocol/registry/URLRegistryTest.java index 1c6234e35..463723a5d 100644 --- a/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java +++ b/testing/url-registry/src/test/java/org/apache/axiom/net/protocol/registry/URLRegistryTest.java @@ -16,27 +16,26 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.axiom.testutils.net.protocol.mem; +package org.apache.axiom.net.protocol.registry; import java.net.URL; import java.net.URLConnection; -import junit.framework.TestCase; - import org.apache.axiom.testutils.activation.RandomDataSource; import org.apache.axiom.testutils.io.IOTestUtils; -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration; -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistry; +import org.junit.Test; -public class DataSourceRegistryTest extends TestCase { +public class URLRegistryTest { + @Test public void test() throws Exception { RandomDataSource ds = new RandomDataSource(1000); - DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds); + URLRegistration registration = URLRegistry.register(ds); try { // We must be able to connect to the URL after converting it to a String URL url = new URL(registration.getURL().toString()); URLConnection connection = url.openConnection(); - IOTestUtils.compareStreams(connection.getInputStream(), "actual", ds.getInputStream(), "expected"); + IOTestUtils.compareStreams( + connection.getInputStream(), "actual", ds.getInputStream(), "expected"); } finally { registration.unregister(); } diff --git a/testing/xml-testsuite/pom.xml b/testing/xml-testsuite/pom.xml index f5a943f00..6561c7b2e 100644 --- a/testing/xml-testsuite/pom.xml +++ b/testing/xml-testsuite/pom.xml @@ -49,6 +49,11 @@ <artifactId>testutils</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>url-registry</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>jakarta.activation</groupId> <artifactId>jakarta.activation-api</artifactId> diff --git a/testing/xml-testsuite/src/main/java/org/apache/axiom/ts/xml/ComputedMessageContent.java b/testing/xml-testsuite/src/main/java/org/apache/axiom/ts/xml/ComputedMessageContent.java index f9fcb78b8..0e68dc1bb 100644 --- a/testing/xml-testsuite/src/main/java/org/apache/axiom/ts/xml/ComputedMessageContent.java +++ b/testing/xml-testsuite/src/main/java/org/apache/axiom/ts/xml/ComputedMessageContent.java @@ -20,14 +20,11 @@ package org.apache.axiom.ts.xml; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; -import javax.activation.DataSource; - -import org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistry; +import org.apache.axiom.net.protocol.registry.URLRegistry; public abstract class ComputedMessageContent extends MessageContent { private byte[] content; @@ -50,27 +47,7 @@ public abstract class ComputedMessageContent extends MessageContent { @Override public final synchronized URL getURL() { if (url == null) { - url = DataSourceRegistry.registerDataSource(new DataSource() { - @Override - public OutputStream getOutputStream() throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public String getName() { - return null; - } - - @Override - public InputStream getInputStream() throws IOException { - return ComputedMessageContent.this.getInputStream(); - } - - @Override - public String getContentType() { - return "application/octet-stream"; - } - }).getURL(); + url = URLRegistry.register(this::getInputStream).getURL(); } return url; }