Skip to content

Commit

Permalink
(#28) Added setClob implementations in JdbcPreparedStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Mar 22, 2019
1 parent 2e1b347 commit c847681
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package com.consol.citrus.db.driver;

import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.utils.ClobUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.client.HttpClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
Expand Down Expand Up @@ -62,6 +66,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
/** A list of parameter sets for batch execution purposes */
private final List<Map<String, Object>> batchParameters = new LinkedList<>();

private final ClobUtils clobUtils = new ClobUtils();

public JdbcPreparedStatement(final HttpClient httpClient, final String preparedStatement, final String serverUrl, final JdbcConnection connection) {
super(httpClient, serverUrl, connection);
this.preparedStatement = preparedStatement;
Expand Down Expand Up @@ -219,8 +225,8 @@ public void setBlob(final int parameterIndex, final Blob x) throws SQLException
}

@Override
public void setClob(final int parameterIndex, final Clob x) throws SQLException {
throw new SQLException("Not supported JDBC prepared statement function 'setClob'");
public void setClob(final int parameterIndex, final Clob x) {
setParameter(parameterIndex, x);
}

@Override
Expand Down Expand Up @@ -285,7 +291,16 @@ public void setNClob(final int parameterIndex, final NClob value) throws SQLExce

@Override
public void setClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
throw new SQLException("Not supported JDBC prepared statement function 'setClob'");
if(clobUtils.fitsInInt(length)){
try {
final CitrusClob citrusClob = new CitrusClob();
final String desiredClobContent = IOUtils.toString(reader);
citrusClob.setString(1, desiredClobContent.substring(0, (int)length));
setParameter(parameterIndex, citrusClob);
} catch (final IOException e) {
throw new SQLException("Could not create Clob from reader", e);
}
}
}

@Override
Expand Down Expand Up @@ -345,7 +360,14 @@ public void setNCharacterStream(final int parameterIndex, final Reader value) th

@Override
public void setClob(final int parameterIndex, final Reader reader) throws SQLException {
throw new SQLException("Not supported JDBC prepared statement function 'setClob'");
try {
final CitrusClob citrusClob = new CitrusClob();
final String desiredClobContent = IOUtils.toString(reader);
citrusClob.setString(1, desiredClobContent);
setParameter(parameterIndex, citrusClob);
} catch (final IOException e) {
throw new SQLException("Could not create Clob from reader", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.consol.citrus.db.driver.data;

import com.consol.citrus.db.driver.utils.ClobUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -21,6 +23,7 @@
public class CitrusClob implements Clob {

private final StringBuilder stringBuilder = new StringBuilder();
private final ClobUtils clobUtils = new ClobUtils();

@Override
public long length() {
Expand All @@ -30,7 +33,7 @@ public long length() {
@Override
public String getSubString(final long pos, final int length) {
final long longOffset = applyOffset(pos);
if(fitsInInt(longOffset)){
if(clobUtils.fitsInInt(longOffset)){
final int offset = toIntExact(longOffset);
return stringBuilder.substring(offset, offset + length);
}
Expand All @@ -50,7 +53,7 @@ public InputStream getAsciiStream() {

@Override
public long position(final String searchstr, final long start) {
if(fitsInInt(start)){
if(clobUtils.fitsInInt(start)){
return stringBuilder.indexOf(searchstr, (int)start);
}
return -1;
Expand All @@ -59,7 +62,7 @@ public long position(final String searchstr, final long start) {
@Override
public long position(final Clob searchstr, final long start) throws SQLException {
final long clobLength = searchstr.length();
if(fitsInInt(clobLength)){
if(clobUtils.fitsInInt(clobLength)){
final String subString = searchstr.getSubString(1, (int)clobLength);
return position(subString, start);
}
Expand All @@ -74,7 +77,7 @@ public int setString(final long pos, final String str) {
@Override
public int setString(final long pos, final String str, final int offset, final int len) {
final long positionWithOffset = applyOffset(pos);
if(fitsInInt(positionWithOffset)){
if(clobUtils.fitsInInt(positionWithOffset)){
return setContent(stringBuilder, (int) positionWithOffset, str, offset, len);
}else {
return 0;
Expand Down Expand Up @@ -120,7 +123,7 @@ public void close() {

@Override
public void truncate(final long len) {
if(fitsInInt(len)){
if(clobUtils.fitsInInt(len)){
stringBuilder.delete((int)len, stringBuilder.length());
}
}
Expand All @@ -141,7 +144,7 @@ public Reader getCharacterStream(final long pos, final long length) throws SQLEx
}

final long posWithOffset = applyOffset(pos);
if(fitsInInt(posWithOffset + length)){
if(clobUtils.fitsInInt(posWithOffset + length)){
final int intPos = (int) posWithOffset;
final String substring = stringBuilder.substring(intPos, intPos + (int)length);
return new StringReader(substring);
Expand Down Expand Up @@ -170,10 +173,6 @@ public final int hashCode() {
return Objects.hash(stringBuilder);
}

private boolean fitsInInt(final long value) {
return (int)value == value;
}

private long applyOffset(final long pos) {
return pos - 1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.consol.citrus.db.driver.utils;

public class ClobUtils {

public boolean fitsInInt(final long value) {
return (int)value == value;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.consol.citrus.db.driver;

import com.consol.citrus.db.driver.data.CitrusClob;
import com.consol.citrus.db.driver.dataset.DataSet;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.HttpClient;
import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.StringReader;
import java.sql.Clob;
import java.sql.SQLException;

import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -112,6 +116,51 @@ public void textAddBatchWithSqlThrowsException() throws SQLException {
//exception is thrown
}

@Test
public void testSetClobWithParameterIndexAndClob() {

//GIVEN
final Clob clob = mock(Clob.class);

//WHEN
jdbcPreparedStatement.setClob(5, clob);

//THEN
verify(jdbcPreparedStatement).setParameter(5, clob);
}

@Test
public void setLimitedClobFromReader() throws Exception {

//GIVEN
final StringReader stringReader = new StringReader("Stay positive, always!");
final String expectedClobContent = "Stay positive";

//WHEN
jdbcPreparedStatement.setClob(5, stringReader, 13);

//THEN
final CitrusClob storedClob = (CitrusClob) jdbcPreparedStatement.getParameters().get("4");
final String clobContent = IOUtils.toString(storedClob.getCharacterStream());
assertEquals(clobContent, expectedClobContent);
}

@Test
public void setClobFromReader() throws Exception {

//GIVEN
final String expectedClobContent = "Stay positive, always!";
final StringReader stringReader = new StringReader(expectedClobContent);

//WHEN
jdbcPreparedStatement.setClob(5, stringReader);

//THEN
final CitrusClob storedClob = (CitrusClob) jdbcPreparedStatement.getParameters().get("4");
final String clobContent = IOUtils.toString(storedClob.getCharacterStream());
assertEquals(clobContent, expectedClobContent);
}

@Test
public void testToString(){
ToStringVerifier.forClass(JdbcPreparedStatement.class).verify();
Expand Down
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,17 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${apache.commons.io.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

<!-- Global dependencies -->
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<!-- Test scoped dependencies -->
<dependency>
<groupId>org.testng</groupId>
Expand Down Expand Up @@ -331,11 +335,6 @@
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit c847681

Please sign in to comment.