Skip to content

Commit

Permalink
(#28) Added setBlob functionality to JdbcPreparedStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Apr 12, 2019
1 parent f485b33 commit 75d4934
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 75d4934

Please sign in to comment.