From 3cd3e64231eebdb45f2de59f128efd470ea0499e Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 22 Mar 2019 09:24:42 +0100 Subject: [PATCH] (citrusframework/citrus-db#28) Added truncate implementation --- .../citrus/db/driver/data/CitrusClob.java | 20 ++++++++++++------- .../citrus/db/driver/data/CitrusClobTest.java | 16 +++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusClob.java b/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusClob.java index d4d512b..5d0296c 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusClob.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusClob.java @@ -119,8 +119,10 @@ public void close() { } @Override - public void truncate(final long len) throws SQLException { - + public void truncate(final long len) { + if(fitsInInt(len)){ + stringBuilder.delete((int)len, stringBuilder.length()); + } } @Override @@ -170,12 +172,16 @@ private long applyOffset(final long pos) { * @param stringToSet The String to set the content from. * @param offset the index of the first character of {@code stringToSet} to be inserted. Starting at 0. * @param length The length of the string to set. - * @return + * @return The length of the string that has been set */ - private int setContent(final StringBuilder stringBuilder, final int position, final String stringToSet, final int offset, final int length) { - final boolean expandsString = position + length > stringBuilder.length(); - - if(expandsString){ + private int setContent(final StringBuilder stringBuilder, + final int position, + final String stringToSet, + final int offset, + final int length) { + final boolean expandsCurrentContent = position + length > stringBuilder.length(); + + if(expandsCurrentContent){ stringBuilder.delete(position, stringBuilder.length()); stringBuilder.insert(position, stringToSet.toCharArray(), offset, length); }else{ diff --git a/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusClobTest.java b/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusClobTest.java index 8653746..7330d8d 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusClobTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusClobTest.java @@ -266,6 +266,22 @@ public void testSetCharacterStream() throws Exception{ assertEquals(clobContent, expectedClobContent); } + @Test + public void testTruncate() throws Exception { + + //GIVEN + citrusClob.setString(1, sampleText); + final String expectedTruncatedString = "Keep calm"; + final long desiredLength = expectedTruncatedString.length(); + + //WHEN + citrusClob.truncate(desiredLength); + + //THEN + final String clobContent = IOUtils.toString(citrusClob.getCharacterStream()); + assertEquals(clobContent, expectedTruncatedString); + } + @Test public void testEqualsContract(){ EqualsVerifier