Test for ByteBuffer-to-ByteBuffer conversion (fresh copy)
ByteBufferConverter also defensively returns the rewind result now. Issue: SPR-13031
This commit is contained in:
@@ -97,6 +97,7 @@ final class ByteBufferConverter implements ConditionalGenericConverter {
|
|||||||
private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) {
|
private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) {
|
||||||
byte[] bytes = new byte[source.remaining()];
|
byte[] bytes = new byte[source.remaining()];
|
||||||
source.get(bytes);
|
source.get(bytes);
|
||||||
|
|
||||||
if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) {
|
if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) {
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
@@ -113,9 +114,7 @@ final class ByteBufferConverter implements ConditionalGenericConverter {
|
|||||||
// Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since
|
// Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since
|
||||||
// otherwise the overridden ByteBuffer-returning rewind method would be chosen
|
// otherwise the overridden ByteBuffer-returning rewind method would be chosen
|
||||||
// which isn't available on JDK 8.
|
// which isn't available on JDK 8.
|
||||||
((Buffer) byteBuffer).rewind();
|
return ((Buffer) byteBuffer).rewind();
|
||||||
|
|
||||||
return byteBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ public class ByteBufferConverterTests {
|
|||||||
public void byteArrayToByteBuffer() throws Exception {
|
public void byteArrayToByteBuffer() throws Exception {
|
||||||
byte[] bytes = new byte[] { 1, 2, 3 };
|
byte[] bytes = new byte[] { 1, 2, 3 };
|
||||||
ByteBuffer convert = this.conversionService.convert(bytes, ByteBuffer.class);
|
ByteBuffer convert = this.conversionService.convert(bytes, ByteBuffer.class);
|
||||||
assertThat(bytes, not(sameInstance(convert.array())));
|
assertThat(convert.array(), not(sameInstance(bytes)));
|
||||||
assertThat(bytes, equalTo(convert.array()));
|
assertThat(convert.array(), equalTo(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -58,8 +58,8 @@ public class ByteBufferConverterTests {
|
|||||||
byte[] bytes = new byte[] { 1, 2, 3 };
|
byte[] bytes = new byte[] { 1, 2, 3 };
|
||||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||||
byte[] convert = this.conversionService.convert(byteBuffer, byte[].class);
|
byte[] convert = this.conversionService.convert(byteBuffer, byte[].class);
|
||||||
assertThat(bytes, not(sameInstance(convert)));
|
assertThat(convert, not(sameInstance(bytes)));
|
||||||
assertThat(bytes, equalTo(convert));
|
assertThat(convert, equalTo(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -67,8 +67,8 @@ public class ByteBufferConverterTests {
|
|||||||
byte[] bytes = new byte[] { 1, 2, 3 };
|
byte[] bytes = new byte[] { 1, 2, 3 };
|
||||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||||
OtherType convert = this.conversionService.convert(byteBuffer, OtherType.class);
|
OtherType convert = this.conversionService.convert(byteBuffer, OtherType.class);
|
||||||
assertThat(bytes, not(sameInstance(convert.bytes)));
|
assertThat(convert.bytes, not(sameInstance(bytes)));
|
||||||
assertThat(bytes, equalTo(convert.bytes));
|
assertThat(convert.bytes, equalTo(bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -76,8 +76,17 @@ public class ByteBufferConverterTests {
|
|||||||
byte[] bytes = new byte[] { 1, 2, 3 };
|
byte[] bytes = new byte[] { 1, 2, 3 };
|
||||||
OtherType otherType = new OtherType(bytes);
|
OtherType otherType = new OtherType(bytes);
|
||||||
ByteBuffer convert = this.conversionService.convert(otherType, ByteBuffer.class);
|
ByteBuffer convert = this.conversionService.convert(otherType, ByteBuffer.class);
|
||||||
assertThat(bytes, not(sameInstance(convert.array())));
|
assertThat(convert.array(), not(sameInstance(bytes)));
|
||||||
assertThat(bytes, equalTo(convert.array()));
|
assertThat(convert.array(), equalTo(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void byteBufferToByteBuffer() throws Exception {
|
||||||
|
byte[] bytes = new byte[] { 1, 2, 3 };
|
||||||
|
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||||
|
ByteBuffer convert = this.conversionService.convert(byteBuffer, ByteBuffer.class);
|
||||||
|
assertThat(convert, not(sameInstance(byteBuffer.rewind())));
|
||||||
|
assertThat(convert, equalTo(byteBuffer.rewind()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user