Skip to content

Commit 5a8b8b1

Browse files
poutsmabclozel
authored andcommitted
Add test for calculateCapacity
Issue: SPR-17558 Closes gh-2054
1 parent a00be62 commit 5a8b8b1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spring-core/src/main/java/org/springframework/core/codec/CharSequenceEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
9999
});
100100
}
101101

102-
private int calculateCapacity(CharSequence sequence, Charset charset) {
102+
int calculateCapacity(CharSequence sequence, Charset charset) {
103103
float maxBytesPerChar = this.charsetToMaxBytesPerChar
104104
.computeIfAbsent(charset, cs -> cs.newEncoder().maxBytesPerChar());
105105
float maxBytesForSequence = sequence.length() * maxBytesPerChar;

spring-core/src/test/java/org/springframework/core/codec/CharSequenceEncoderTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@
1616

1717
package org.springframework.core.codec;
1818

19+
import java.nio.charset.Charset;
20+
import java.util.stream.Stream;
21+
22+
import org.junit.Test;
1923
import reactor.core.publisher.Flux;
2024

2125
import org.springframework.core.ResolvableType;
2226
import org.springframework.util.MimeTypeUtils;
2327

28+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
29+
import static java.nio.charset.StandardCharsets.US_ASCII;
30+
import static java.nio.charset.StandardCharsets.UTF_16;
31+
import static java.nio.charset.StandardCharsets.UTF_8;
2432
import static org.junit.Assert.*;
2533

2634
/**
@@ -65,4 +73,17 @@ public void encode() {
6573
.verifyComplete());
6674
}
6775

76+
@Test
77+
public void calculateCapacity() {
78+
String sequence = "Hello World!";
79+
Stream.of(UTF_8, UTF_16, ISO_8859_1, US_ASCII, Charset.forName("BIG5"))
80+
.forEach(charset -> {
81+
int capacity = this.encoder.calculateCapacity(sequence, charset);
82+
int length = sequence.length();
83+
assertTrue(String.format("%s has capacity %d; length %d", charset, capacity, length),
84+
capacity >= length);
85+
});
86+
87+
}
88+
6889
}

0 commit comments

Comments
 (0)