Skip to content

Commit 008c9a3

Browse files
committed
Test for ByteBuffer-to-ByteBuffer conversion (fresh copy)
ByteBufferConverter also defensively returns the rewind result now. Issue: SPR-13031
1 parent 1e7d954 commit 008c9a3

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/ByteBufferConverter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
9797
private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) {
9898
byte[] bytes = new byte[source.remaining()];
9999
source.get(bytes);
100+
100101
if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) {
101102
return bytes;
102103
}
@@ -113,9 +114,7 @@ private Object convertToByteBuffer(Object source, TypeDescriptor sourceType) {
113114
// Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since
114115
// otherwise the overridden ByteBuffer-returning rewind method would be chosen
115116
// which isn't available on JDK 8.
116-
((Buffer) byteBuffer).rewind();
117-
118-
return byteBuffer;
117+
return ((Buffer) byteBuffer).rewind();
119118
}
120119

121120
}

spring-core/src/test/java/org/springframework/core/convert/support/ByteBufferConverterTests.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,44 @@ public void setup() {
4949
public void byteArrayToByteBuffer() throws Exception {
5050
byte[] bytes = new byte[] { 1, 2, 3 };
5151
ByteBuffer convert = this.conversionService.convert(bytes, ByteBuffer.class);
52-
assertThat(bytes, not(sameInstance(convert.array())));
53-
assertThat(bytes, equalTo(convert.array()));
52+
assertThat(convert.array(), not(sameInstance(bytes)));
53+
assertThat(convert.array(), equalTo(bytes));
5454
}
5555

5656
@Test
5757
public void byteBufferToByteArray() throws Exception {
5858
byte[] bytes = new byte[] { 1, 2, 3 };
5959
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
6060
byte[] convert = this.conversionService.convert(byteBuffer, byte[].class);
61-
assertThat(bytes, not(sameInstance(convert)));
62-
assertThat(bytes, equalTo(convert));
61+
assertThat(convert, not(sameInstance(bytes)));
62+
assertThat(convert, equalTo(bytes));
6363
}
6464

6565
@Test
6666
public void byteBufferToOtherType() throws Exception {
6767
byte[] bytes = new byte[] { 1, 2, 3 };
6868
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
6969
OtherType convert = this.conversionService.convert(byteBuffer, OtherType.class);
70-
assertThat(bytes, not(sameInstance(convert.bytes)));
71-
assertThat(bytes, equalTo(convert.bytes));
70+
assertThat(convert.bytes, not(sameInstance(bytes)));
71+
assertThat(convert.bytes, equalTo(bytes));
7272
}
7373

7474
@Test
7575
public void otherTypeToByteBuffer() throws Exception {
7676
byte[] bytes = new byte[] { 1, 2, 3 };
7777
OtherType otherType = new OtherType(bytes);
7878
ByteBuffer convert = this.conversionService.convert(otherType, ByteBuffer.class);
79-
assertThat(bytes, not(sameInstance(convert.array())));
80-
assertThat(bytes, equalTo(convert.array()));
79+
assertThat(convert.array(), not(sameInstance(bytes)));
80+
assertThat(convert.array(), equalTo(bytes));
81+
}
82+
83+
@Test
84+
public void byteBufferToByteBuffer() throws Exception {
85+
byte[] bytes = new byte[] { 1, 2, 3 };
86+
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
87+
ByteBuffer convert = this.conversionService.convert(byteBuffer, ByteBuffer.class);
88+
assertThat(convert, not(sameInstance(byteBuffer.rewind())));
89+
assertThat(convert, equalTo(byteBuffer.rewind()));
8190
}
8291

8392

0 commit comments

Comments
 (0)