Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AB802181A9 for ; Tue, 15 Sep 2015 12:50:20 +0000 (UTC) Received: (qmail 31167 invoked by uid 500); 15 Sep 2015 12:50:20 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 31104 invoked by uid 500); 15 Sep 2015 12:50:20 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 31095 invoked by uid 99); 15 Sep 2015 12:50:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Sep 2015 12:50:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7096DDFBD6; Tue, 15 Sep 2015 12:50:20 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-6591] Closing temp InputStream, patch from Sanjin Tulac applied with thanks Date: Tue, 15 Sep 2015 12:50:20 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/master e4f6f793c -> e30839db7 [CXF-6591] Closing temp InputStream, patch from Sanjin Tulac applied with thanks Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/e30839db Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/e30839db Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/e30839db Branch: refs/heads/master Commit: e30839db74b7e265cf23bda2a72ccf48db11e617 Parents: e4f6f79 Author: Sergey Beryozkin Authored: Tue Sep 15 13:39:14 2015 +0100 Committer: Sergey Beryozkin Committed: Tue Sep 15 13:50:04 2015 +0100 ---------------------------------------------------------------------- rt/databinding/aegis/pom.xml | 6 ++ .../cxf/aegis/type/mtom/DataSourceType.java | 4 +- .../cxf/aegis/type/mtom/DataSourceTypeTest.java | 79 ++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/e30839db/rt/databinding/aegis/pom.xml ---------------------------------------------------------------------- diff --git a/rt/databinding/aegis/pom.xml b/rt/databinding/aegis/pom.xml index 02a65f0..11ccc9d 100644 --- a/rt/databinding/aegis/pom.xml +++ b/rt/databinding/aegis/pom.xml @@ -131,6 +131,12 @@ spring-context test + + org.easymock + easymock + 3.4 + test + http://git-wip-us.apache.org/repos/asf/cxf/blob/e30839db/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/mtom/DataSourceType.java ---------------------------------------------------------------------- diff --git a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/mtom/DataSourceType.java b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/mtom/DataSourceType.java index 24d8952..4741717 100644 --- a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/mtom/DataSourceType.java +++ b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/mtom/DataSourceType.java @@ -21,7 +21,6 @@ package org.apache.cxf.aegis.type.mtom; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import javax.activation.DataHandler; import javax.activation.DataSource; @@ -69,8 +68,7 @@ public class DataSourceType extends AbstractXOPType { DataSource dataSource = (DataSource) object; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - InputStream stream = dataSource.getInputStream(); - IOUtils.copy(stream, baos); + IOUtils.copyAndCloseInput(dataSource.getInputStream(), baos); } catch (IOException e) { throw new RuntimeException(e); } http://git-wip-us.apache.org/repos/asf/cxf/blob/e30839db/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/mtom/DataSourceTypeTest.java ---------------------------------------------------------------------- diff --git a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/mtom/DataSourceTypeTest.java b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/mtom/DataSourceTypeTest.java new file mode 100644 index 0000000..91aabbd --- /dev/null +++ b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/mtom/DataSourceTypeTest.java @@ -0,0 +1,79 @@ +/** + * 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.cxf.aegis.type.mtom; + +import java.io.IOException; +import java.io.InputStream; + +import javax.activation.DataSource; + +import org.easymock.MockType; +import org.junit.Test; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.mock; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +/** + * Unit test for DataSourceType class, which also tests any static helper functions invoked + * by its implementation. + * + */ +public class DataSourceTypeTest { + + @Test + public void inputStreamShouldBeClosedOnHappyPath() throws Exception { + DataSource ds = mock(MockType.STRICT, DataSource.class); + InputStream is = mock(MockType.STRICT, InputStream.class); + expect(ds.getInputStream()).andReturn(is); + replay(ds); + expect(is.available()).andReturn(1); + expect(is.read(anyObject(byte[].class))).andReturn(-1); + is.close(); + replay(is); + + DataSourceType dst = new DataSourceType(false, null); + dst.getBytes(ds); + + verify(ds); + verify(is); + } + + @Test(expected = RuntimeException.class) + public void inputStreamShouldBeClosedOnReadingException() throws Exception { + DataSource ds = mock(MockType.STRICT, DataSource.class); + InputStream is = mock(MockType.STRICT, InputStream.class); + expect(ds.getInputStream()).andReturn(is); + replay(ds); + expect(is.available()).andThrow(new IOException()); + is.close(); + replay(is); + + DataSourceType dst = new DataSourceType(false, null); + try { + dst.getBytes(ds); + } finally { + verify(ds); + verify(is); + } + + } +}