commit 7a899ba771e470526f93b5e883e245a54d29cfee Author: Trang Truong Thi Huyen Date: Tue May 12 15:52:21 2015 +0700 MAGNOLIA-6202: Add unit test for case stream closed correctly. diff --git a/magnolia-core/src/test/java/info/magnolia/commands/impl/ExportCommandTest.java b/magnolia-core/src/test/java/info/magnolia/commands/impl/ExportCommandTest.java index d0f3b84..98fd439 100644 --- a/magnolia-core/src/test/java/info/magnolia/commands/impl/ExportCommandTest.java +++ b/magnolia-core/src/test/java/info/magnolia/commands/impl/ExportCommandTest.java @@ -44,6 +44,8 @@ import info.magnolia.repository.RepositoryConstants; import info.magnolia.test.ComponentsTestUtil; import info.magnolia.test.RepositoryTestCase; +import java.io.OutputStream; + import javax.jcr.Node; import javax.jcr.Session; @@ -132,6 +134,19 @@ public class ExportCommandTest extends RepositoryTestCase { // THEN } + @Test + public void testExportCommandStreamClosedCorrectly() throws Exception { + // GIVEN + doReturn(true).when(exportCommand).checkPermissions(RepositoryConstants.WEBSITE, toCopyNode.getPath(), Permission.READ); + OutputStream stream = mock(OutputStream.class); + when(exportCommand.getOutputStream()).thenReturn(stream); + + // WHEN + exportCommand.execute(context); + + verify(exportCommand.getOutputStream(), atLeastOnce()).close(); + } + private void initContext(Context ctx) throws Exception { ctx.put(ExportCommand.EXPORT_EXTENSION, ".xml"); ctx.put(ExportCommand.EXPORT_FORMAT, true); commit 40f854396f5d0e4b6468ad4b08c13c5a98965051 Author: Trang Truong Thi Huyen Date: Fri May 8 15:41:10 2015 +0700 MAGNOLIA-6202: close stream ExportCommand properly. diff --git a/magnolia-core/src/main/java/info/magnolia/commands/impl/ExportCommand.java b/magnolia-core/src/main/java/info/magnolia/commands/impl/ExportCommand.java index 90660b8..cf783d3 100644 --- a/magnolia-core/src/main/java/info/magnolia/commands/impl/ExportCommand.java +++ b/magnolia-core/src/main/java/info/magnolia/commands/impl/ExportCommand.java @@ -43,6 +43,7 @@ import info.magnolia.repository.RepositoryConstants; import java.io.OutputStream; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -109,6 +110,8 @@ public class ExportCommand extends BaseRepositoryCommand { setFileName(getRepository() + pathName + getExt()); } catch (RuntimeException e) { throw e; + } finally { + IOUtils.closeQuietly(outputStream); } return true;