From 74ca87699461f080e60201e4d07a1abdd1decafe Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 11 Apr 2019 13:35:13 +0200 Subject: [PATCH] (citrusframework/citrus-db#28) Added getBytes, setBytes and length implementation --- .../citrus/db/driver/data/CitrusBlob.java | 35 ++++++++---- .../citrus/db/driver/data/CitrusBlobTest.java | 56 +++++++++++++++++-- 2 files changed, 75 insertions(+), 16 deletions(-) diff --git a/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusBlob.java b/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusBlob.java index d5e4dff..67e5543 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusBlob.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/data/CitrusBlob.java @@ -10,17 +10,22 @@ public class CitrusBlob implements Blob { - private byte[] content = new byte[0]; + private byte[] content = ArrayUtils.EMPTY_BYTE_ARRAY; private final LobUtils lobUtils = new LobUtils(); @Override public long length() { - return 0; + return content.length; } @Override public byte[] getBytes(final long pos, final int length) { - return new byte[0]; + final long positionWithOffset = lobUtils.applyOffset(pos); + if(lobUtils.fitsInInt(positionWithOffset)){ + final int offsetPositionInInt = (int) positionWithOffset; + return ArrayUtils.subarray(content, offsetPositionInInt, offsetPositionInInt + length); + } + return new byte[length]; } @Override @@ -42,7 +47,7 @@ public long position(final Blob pattern, final long start) { public int setBytes(final long pos, final byte[] bytes) { final long positionWithOffset = lobUtils.applyOffset(pos); if(lobUtils.fitsInInt(positionWithOffset)){ - return setContent(content, (int)positionWithOffset, bytes, 0, bytes.length); + return setContent((int)positionWithOffset, bytes, 0, bytes.length); } return 0; @@ -50,6 +55,11 @@ public int setBytes(final long pos, final byte[] bytes) { @Override public int setBytes(final long pos, final byte[] bytes, final int offset, final int len) { + final long positionWithOffset = lobUtils.applyOffset(pos); + if(lobUtils.fitsInInt(positionWithOffset)){ + return setContent((int)positionWithOffset, bytes, offset, len); + } + return 0; } @@ -95,25 +105,26 @@ public String toString() { * Alters the content of this @{@link CitrusBlob} to contain the given bytes. * If the size of the altered bytes exceeds the current capacity, the capacity is automatically extended to the * required size. - * @param byteContent The byte content to alter * @param position The start position for altering the content. Starts at 0. * @param bytesToSet The bytes to set the content from. * @param offset the index of the first character of {@code bytesToSet} to be inserted. Starting at 0. * @param length The number of bytes to set. * @return The number of the bytes that have been set */ - private int setContent(final byte[] byteContent, - final int position, + private int setContent(final int position, final byte[] bytesToSet, final int offset, final int length) { - final boolean expandsCurrentContent = position + length > byteContent.length; + final boolean expandsCurrentContent = position + length > content.length; + final byte[] limitedBytesToSet = ArrayUtils.subarray(bytesToSet, offset, offset+length); + final byte[] bytesBeforeAddingContent = Arrays.copyOfRange(content, 0, position); - if(expandsCurrentContent){ - final byte[] untouchedContent = Arrays.copyOfRange(content,0, position); - content = ArrayUtils.addAll(untouchedContent, bytesToSet); + if(expandsCurrentContent) { + content = ArrayUtils.addAll(bytesBeforeAddingContent, limitedBytesToSet); }else{ - content = ArrayUtils.insert(position, content, bytesToSet); + final byte[] bytesAfterReplacement = Arrays.copyOfRange(content, position + length, content.length); + final byte[] bytesIncludingReplacement = ArrayUtils.addAll(bytesBeforeAddingContent, limitedBytesToSet); + content = ArrayUtils.addAll(bytesIncludingReplacement, bytesAfterReplacement); } return length; diff --git a/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusBlobTest.java b/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusBlobTest.java index 7498895..285decd 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusBlobTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/data/CitrusBlobTest.java @@ -10,6 +10,10 @@ public class CitrusBlobTest { + private final CitrusBlob citrusBlob = new CitrusBlob(); + + private final byte[] sampleBytes = "Beam Me Up, Scotty".getBytes(); + @Test public void testEqualsContract(){ EqualsVerifier @@ -23,15 +27,59 @@ public void testEqualsContract(){ public void testToString(){ //GIVEN - final byte[] expectedClobContent = "Beam Me Up, Scotty".getBytes(); - final CitrusBlob citrusBlob = new CitrusBlob(); - citrusBlob.setBytes(1, expectedClobContent); + citrusBlob.setBytes(1, sampleBytes); //WHEN final String clobContent = citrusBlob.toString(); //THEN - assertEquals(clobContent, Arrays.toString(expectedClobContent)); + assertEquals(clobContent, Arrays.toString(sampleBytes)); + } + + @Test + public void testGetBytes() { + + //GIVEN + citrusBlob.setBytes(1, sampleBytes); + final byte[] expectedBlobContent = "Me Up".getBytes(); + + //WHEN + final byte[] blobContent = citrusBlob.getBytes(6, 5); + + //THEN + final String blobString = new String(blobContent); + assertEquals(blobString, new String(expectedBlobContent)); + } + + @Test + public void testLength() { + + //GIVEN + citrusBlob.setBytes(1, sampleBytes); + + //WHEN + final long length = citrusBlob.length(); + //THEN + assertEquals(length, sampleBytes.length); + } + + @Test + public void testSetStringWithOffsetAndLengthReplacingContent() { + + //GIVEN + citrusBlob.setBytes(1, sampleBytes); + + final byte[] bytesToAdd = "pause".getBytes(); + + final byte[] expectedBlobContent = "Beam us Up, Scotty".getBytes(); + + //WHEN + final long writtenBytes = citrusBlob.setBytes(6, bytesToAdd, 2, 2); + + //THEN + final String blobString = new String(citrusBlob.getBytes(1, (int)citrusBlob.length())); + assertEquals(blobString, new String(expectedBlobContent)); + assertEquals(writtenBytes, 2); } } \ No newline at end of file