[java-metadata-aggregator] 03/03: MDA-279 - Confirm stream from resource is closed correctly

Ian Young ian at iay.org.uk
Thu Mar 23 17:23:33 UTC 2023


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch main
in repository java-metadata-aggregator.

View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=e5f4f6379ca7a3b6a8527b113b97977d2a7c4ce7

commit e5f4f6379ca7a3b6a8527b113b97977d2a7c4ce7
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Thu Mar 23 17:23:26 2023 +0000

    MDA-279 - Confirm stream from resource is closed correctly
---
 .../metadata/dom/DOMResourceSourceStageTest.java   | 45 ++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/dom/DOMResourceSourceStageTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/dom/DOMResourceSourceStageTest.java
index 9a01141..7044d11 100644
--- a/mda-framework/src/test/java/net/shibboleth/metadata/dom/DOMResourceSourceStageTest.java
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/dom/DOMResourceSourceStageTest.java
@@ -20,12 +20,14 @@ package net.shibboleth.metadata.dom;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 
 import javax.annotation.Nonnull;
 
 import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.InputStreamResource;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.UrlResource;
 import org.testng.Assert;
@@ -161,4 +163,47 @@ public class DOMResourceSourceStageTest extends BaseTest {
             Assert.assertTrue(cause instanceof IOException, "cause should have been an IOException");
         }
     }
+
+    /**
+     * Implementation of {@link InputStream} that wraps {@link ByteArrayInputStream}
+     * while remembering if it was properly closed after use.
+     */
+    private static class WrappedInputStream extends ByteArrayInputStream {
+        private boolean closed = false;
+
+        public WrappedInputStream(byte[] buf) {
+            super(buf);
+        }
+        
+        public void close() throws IOException {
+            super.close();
+            closed = true;
+        }
+        
+        public boolean isClosed() {
+            return closed;
+        }
+    }
+
+    @Test
+    public void mda279() throws Exception {
+        final String data = "<x><y/></x>";
+        final var stream = new WrappedInputStream(data.getBytes("UTF-8"));
+        Assert.assertFalse(stream.isClosed());
+
+        final var stage = new DOMResourceSourceStage();
+        stage.setId("test");
+        stage.setParserPool(parserPool);
+        stage.setDOMResource(new InputStreamResource(stream));
+        stage.initialize();
+        Assert.assertFalse(stream.isClosed());
+
+        final var items = new ArrayList<Item<Element>>();
+        stage.execute(items);
+        Assert.assertEquals(items.size(), 1);
+
+        stage.destroy();
+
+        Assert.assertTrue(stream.isClosed());
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list