From 75d49342f373625411c312c25845d6eafe97ea68 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 12 Apr 2019 15:27:23 +0200 Subject: [PATCH] (citrusframework/citrus-db#28) Added setBlob functionality to JdbcPreparedStatement --- .../db/driver/JdbcPreparedStatement.java | 13 +++-- .../citrus/db/driver/utils/LobUtils.java | 2 +- .../db/driver/JdbcPreparedStatementTest.java | 49 +++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java index 9f51994..bf6c781 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java @@ -16,6 +16,7 @@ package com.consol.citrus.db.driver; +import com.consol.citrus.db.driver.data.CitrusBlob; import com.consol.citrus.db.driver.data.CitrusClob; import com.consol.citrus.db.driver.utils.LobUtils; import org.apache.commons.lang3.ArrayUtils; @@ -230,8 +231,8 @@ public void setRef(final int parameterIndex, final Ref x) throws SQLException { } @Override - public void setBlob(final int parameterIndex, final Blob x) throws SQLException { - throw new SQLException("Not supported JDBC prepared statement function 'setBlob'"); + public void setBlob(final int parameterIndex, final Blob x) { + setParameter(parameterIndex, x); } @Override @@ -309,7 +310,10 @@ public void setClob(final int parameterIndex, final Reader reader, final long le @Override public void setBlob(final int parameterIndex, final InputStream inputStream, final long length) throws SQLException { - throw new SQLException("Not supported JDBC prepared statement function 'setBlob'"); + if(lobUtils.fitsInInt(length)){ + final CitrusBlob citrusBlob = lobUtils.createBlobFromInputStream(inputStream, (int) length); + setParameter(parameterIndex, citrusBlob); + } } @Override @@ -370,7 +374,8 @@ public void setClob(final int parameterIndex, final Reader reader) throws SQLExc @Override public void setBlob(final int parameterIndex, final InputStream inputStream) throws SQLException { - throw new SQLException("Not supported JDBC prepared statement function 'setBlob'"); + final CitrusBlob citrusClob = lobUtils.createBlobFromInputStream(inputStream, -1); + setParameter(parameterIndex, citrusClob); } @Override diff --git a/driver/src/main/java/com/consol/citrus/db/driver/utils/LobUtils.java b/driver/src/main/java/com/consol/citrus/db/driver/utils/LobUtils.java index bd91459..facdc9e 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/utils/LobUtils.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/utils/LobUtils.java @@ -43,7 +43,7 @@ public CitrusBlob createBlobFromInputStream(final InputStream inputStream, final try{ return createBlob(length, IOUtils.toByteArray(inputStream)); } catch (final IOException e) { - throw new SQLException("Could not create Clob from reader", e); + throw new SQLException("Could not create Blob from inputStream", e); } } diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java index b342622..d0381f7 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java @@ -1,5 +1,6 @@ package com.consol.citrus.db.driver; +import com.consol.citrus.db.driver.data.CitrusBlob; import com.consol.citrus.db.driver.data.CitrusClob; import com.consol.citrus.db.driver.dataset.DataSet; import com.consol.citrus.db.driver.utils.LobUtils; @@ -11,8 +12,10 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.io.InputStream; import java.io.Reader; import java.io.StringReader; +import java.sql.Blob; import java.sql.Clob; import java.sql.SQLException; @@ -182,6 +185,52 @@ public void setClobFromReader() throws Exception { verify(jdbcPreparedStatement).setParameter(5, citrusClobMock); } + @Test + public void testSetBlobWithParameterIndexAndClob() { + + //GIVEN + final Blob blob = mock(Blob.class); + + //WHEN + jdbcPreparedStatement.setBlob(5, blob); + + //THEN + verify(jdbcPreparedStatement).setParameter(5, blob); + } + + @Test + public void testSetLimitedBlobFromInputStream() throws Exception { + + //GIVEN + final long desiredLength = 13L; + when(lobUtils.fitsInInt(desiredLength)).thenReturn(true); + + final CitrusBlob expectedBlob = mock(CitrusBlob.class); + final InputStream inputStreamMock = mock(InputStream.class); + when(lobUtils.createBlobFromInputStream(inputStreamMock, (int)desiredLength)).thenReturn(expectedBlob); + + //WHEN + jdbcPreparedStatement.setBlob(12, inputStreamMock, desiredLength); + + //THEN + verify(jdbcPreparedStatement).setParameter(12, expectedBlob); + } + + @Test + public void setBlobFromInputStream() throws Exception { + + //GIVEN + final InputStream inputStreamMock = mock(InputStream.class); + final CitrusBlob citrusBlobMock = mock(CitrusBlob.class); + when(lobUtils.createBlobFromInputStream(inputStreamMock, -1)).thenReturn(citrusBlobMock); + + //WHEN + jdbcPreparedStatement.setBlob(5, inputStreamMock); + + //THEN + verify(jdbcPreparedStatement).setParameter(5, citrusBlobMock); + } + @Test public void testToString(){ ToStringVerifier