cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6591] Closing temp InputStream, patch from Sanjin Tulac applied with thanks
Date Tue, 15 Sep 2015 12:39:33 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 0405291ed -> 7a40f5f9c


[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/7a40f5f9
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7a40f5f9
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7a40f5f9

Branch: refs/heads/2.7.x-fixes
Commit: 7a40f5f9cea86df8a4a59775bc279a5d810da2f8
Parents: 0405291
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Tue Sep 15 13:39:14 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Tue Sep 15 13:39:14 2015 +0100

----------------------------------------------------------------------
 rt/databinding/aegis/pom.xml                    |  7 +-
 .../cxf/aegis/type/mtom/DataSourceType.java     |  4 +-
 .../cxf/aegis/type/mtom/DataSourceTypeTest.java | 79 ++++++++++++++++++++
 3 files changed, 86 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/7a40f5f9/rt/databinding/aegis/pom.xml
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/pom.xml b/rt/databinding/aegis/pom.xml
index 0db67ea..cbd9df2 100644
--- a/rt/databinding/aegis/pom.xml
+++ b/rt/databinding/aegis/pom.xml
@@ -128,7 +128,12 @@
             <artifactId>spring-context</artifactId>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <version>3.4</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/cxf/blob/7a40f5f9/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/7a40f5f9/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);
+        }
+
+    }
+}


Mime
View raw message