Author: veithen
Date: Fri May 22 20:13:57 2015
New Revision: 1681227

URL: http://svn.apache.org/r1681227
Log:
Add a test case that checks that streams are closed properly when creating a 
builder from a StreamSource with system ID. 

Added:
    
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java
   (with props)
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/InstrumentedDataSource.java
   (with props)
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java
   (with props)
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java
   (with props)
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java
   (with props)
    
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java
   (with props)
    webservices/axiom/trunk/testing/testutils/src/test/
    webservices/axiom/trunk/testing/testutils/src/test/java/
    webservices/axiom/trunk/testing/testutils/src/test/java/org/
    webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/
    webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/
    
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/
    
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/
    
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/
    
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/
    
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java
   (with props)
Modified:
    
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java

Modified: 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1681227&r1=1681226&r2=1681227&view=diff
==============================================================================
--- 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
 (original)
+++ 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
 Fri May 22 20:13:57 2015
@@ -101,6 +101,7 @@ public class OMTestSuiteBuilder extends
         for (StreamType streamType : Multiton.getInstances(StreamType.class)) {
             addTest(new 
org.apache.axiom.ts.om.builder.TestCloseWithStream(metaFactory, streamType));
         }
+        addTest(new 
org.apache.axiom.ts.om.builder.TestCloseWithSystemId(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.builder.TestCloseWithXMLStreamReader(metaFactory));
         for (XMLSample file : getInstances(XMLSample.class)) {
             if (file.hasEntityReferences()) {

Added: 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java
 (added)
+++ 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,58 @@
+/*
+ * 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.axiom.ts.om.builder;
+
+import static org.apache.axiom.truth.AxiomTestVerb.ASSERT;
+
+import javax.activation.URLDataSource;
+import javax.xml.transform.stream.StreamSource;
+
+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;
+
+public class TestCloseWithSystemId extends AxiomTestCase {
+    public TestCloseWithSystemId(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    @Override
+    protected void runTest() throws Throwable {
+        InstrumentedDataSource ds = new InstrumentedDataSource(new 
URLDataSource(
+                XMLSample.SIMPLE.getUrl()));
+        DataSourceRegistration registration = 
DataSourceRegistry.registerDataSource(ds);
+        try {
+            OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(
+                    metaFactory.getOMFactory(),
+                    new StreamSource(registration.getURL().toExternalForm()));
+            builder.getDocumentElement();
+            builder.close();
+            // Since the caller doesn't have control over the stream, the 
builder is responsible
+            // for closing it.
+            ASSERT.that(ds.getOpenStreamCount()).isEqualTo(0);
+        } finally {
+            registration.unregister();
+        }
+    }
+}

Propchange: 
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCloseWithSystemId.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/InstrumentedDataSource.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/InstrumentedDataSource.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/InstrumentedDataSource.java
 (added)
+++ 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/InstrumentedDataSource.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,69 @@
+/*
+ * 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.axiom.testutils.activation;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.activation.DataSource;
+
+import org.apache.commons.io.input.ProxyInputStream;
+
+public final class InstrumentedDataSource implements DataSource {
+    private final DataSource parent;
+    private final AtomicInteger openStreamCount = new AtomicInteger();
+
+    public InstrumentedDataSource(DataSource parent) {
+        this.parent = parent;
+    }
+
+    @Override
+    public String getContentType() {
+        return parent.getContentType();
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        InputStream in = new ProxyInputStream(parent.getInputStream()) {
+            @Override
+            public void close() throws IOException {
+                super.close();
+                openStreamCount.decrementAndGet();
+            }
+        };
+        openStreamCount.incrementAndGet();
+        return in;
+    }
+
+    @Override
+    public String getName() {
+        return parent.getName();
+    }
+
+    @Override
+    public OutputStream getOutputStream() throws IOException {
+        throw new UnsupportedOperationException();
+    }
+    
+    public int getOpenStreamCount() {
+        return openStreamCount.get();
+    }
+}

Propchange: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/InstrumentedDataSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java
 (added)
+++ 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,26 @@
+/*
+ * 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.axiom.testutils.net.protocol.mem;
+
+import java.net.URL;
+
+public interface DataSourceRegistration {
+    URL getURL();
+    void unregister();
+}

Propchange: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java
 (added)
+++ 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,76 @@
+/*
+ * 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.axiom.testutils.net.protocol.mem;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.UUID;
+
+import javax.activation.DataSource;
+
+public final class DataSourceRegistry {
+    private static boolean handlerRegistered;
+    private static final Map<String,DataSource> dataSources = new 
HashMap<String,DataSource>();
+    
+    private DataSourceRegistry() {}
+
+    public synchronized static DataSourceRegistration 
registerDataSource(DataSource dataSource) {
+        if (!handlerRegistered) {
+            Properties systemProps = System.getProperties();
+            synchronized (systemProps) {
+                StringBuilder pkgs = new 
StringBuilder(systemProps.getProperty("java.protocol.handler.pkgs", ""));
+                if (pkgs.length() > 0) {
+                    pkgs.append('|');
+                }
+                pkgs.append("org.apache.axiom.testutils.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() {
+            @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);
+                } catch (MalformedURLException ex) {
+                    // We should never get here
+                    throw new Error(ex);
+                }
+            }
+            
+            @Override
+            public void unregister() {
+                synchronized (DataSourceRegistry.class) {
+                    dataSources.remove(id);
+                }
+            }
+        };
+    }
+    
+    static synchronized DataSource lookupDataSource(String id) {
+        return dataSources.get(id);
+    }
+}

Propchange: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java
 (added)
+++ 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,44 @@
+/*
+ * 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.axiom.testutils.net.protocol.mem;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import javax.activation.DataSource;
+
+final class DataSourceURLConnection extends URLConnection {
+    private final DataSource dataSource;
+    
+    DataSourceURLConnection(URL url, DataSource dataSource) {
+        super(url);
+        this.dataSource = dataSource;
+    }
+
+    @Override
+    public void connect() throws IOException {
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        return dataSource.getInputStream();
+    }
+}

Propchange: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceURLConnection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java
 (added)
+++ 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,37 @@
+/*
+ * 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.axiom.testutils.net.protocol.mem;
+
+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");
+        }
+        return new DataSourceURLConnection(url, dataSource);
+    }
+}

Propchange: 
webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/net/protocol/mem/Handler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java?rev=1681227&view=auto
==============================================================================
--- 
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java
 (added)
+++ 
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java
 Fri May 22 20:13:57 2015
@@ -0,0 +1,44 @@
+/*
+ * 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.axiom.testutils.net.protocol.mem;
+
+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;
+
+public class DataSourceRegistryTest extends TestCase {
+    public void test() throws Exception {
+        RandomDataSource ds = new RandomDataSource(1000);
+        DataSourceRegistration registration = 
DataSourceRegistry.registerDataSource(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(), 
ds.getInputStream());
+        } finally {
+            registration.unregister();
+        }
+    }
+}

Propchange: 
webservices/axiom/trunk/testing/testutils/src/test/java/org/apache/axiom/testutils/net/protocol/mem/DataSourceRegistryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to