Skip to content

Commit 688c9f7

Browse files
committed
Use @ParameterizedTest with @MethodSource instead of a test loop
1 parent 431fd32 commit 688c9f7

1 file changed

Lines changed: 44 additions & 41 deletions

File tree

src/test/java/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import java.nio.charset.Charset;
2929
import java.nio.charset.StandardCharsets;
3030
import java.util.Arrays;
31+
import java.util.stream.IntStream;
3132

3233
import org.apache.commons.codec.CodecPolicy;
3334
import org.apache.commons.codec.DecoderException;
3435
import org.apache.commons.lang3.ArrayUtils;
3536
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.params.ParameterizedTest;
38+
import org.junit.jupiter.params.provider.MethodSource;
3639

3740
/**
3841
* Tests {@link Base32}.
@@ -211,6 +214,10 @@ private static void assertBase32DecodingOfTrailingBits(final int nbits) {
211214
}
212215
}
213216

217+
static IntStream rangeProvider() {
218+
return IntStream.range(0, 21);
219+
}
220+
214221
@Test
215222
void testBase32AtBufferEnd() {
216223
testBase32InBuffer(100, 0);
@@ -509,51 +516,47 @@ void testIsInAlphabet() {
509516
assertFalse(b32.isInAlphabet((byte) ('v' + 1)));
510517
}
511518

512-
@Test
513-
void testRandomBytes() {
514-
for (int i = 0; i < 20; i++) {
515-
final Base32 codec = new Base32();
516-
final byte[][] b = BaseNTestData.randomData(codec, i);
517-
assertEquals(b[1].length, codec.getEncodedLength(b[0]), i + " " + codec.lineLength);
518-
// assertEquals(b[0], codec.decode(b[1]));
519-
}
520-
}
521-
522-
@Test
523-
void testRandomBytesChunked() {
524-
for (int i = 0; i < 20; i++) {
525-
final Base32 codec = new Base32(10);
526-
final byte[][] b = BaseNTestData.randomData(codec, i);
527-
assertEquals(b[1].length, codec.getEncodedLength(b[0]), i + " " + codec.lineLength);
528-
// assertEquals(b[0], codec.decode(b[1]));
529-
}
519+
@ParameterizedTest
520+
@MethodSource("rangeProvider")
521+
void testRandomBytes(final int i) {
522+
final Base32 codec = new Base32();
523+
final byte[][] b = BaseNTestData.randomData(codec, i);
524+
assertEquals(b[1].length, codec.getEncodedLength(b[0]), i + " " + codec.lineLength);
525+
// assertEquals(b[0], codec.decode(b[1]));
530526
}
531527

532-
@Test
533-
void testRandomBytesHex() {
534-
for (int i = 0; i < 20; i++) {
535-
final Base32 codec = new Base32(true);
536-
final byte[][] b = BaseNTestData.randomData(codec, i);
537-
assertEquals(b[1].length, codec.getEncodedLength(b[0]), i + " " + codec.lineLength);
538-
// assertEquals(b[0], codec.decode(b[1]));
539-
}
528+
@ParameterizedTest
529+
@MethodSource("rangeProvider")
530+
void testRandomBytesChunked(final int i) {
531+
final Base32 codec = new Base32(10);
532+
final byte[][] b = BaseNTestData.randomData(codec, i);
533+
assertEquals(b[1].length, codec.getEncodedLength(b[0]), i + " " + codec.lineLength);
534+
// assertEquals(b[0], codec.decode(b[1]));
540535
}
541536

542-
@Test
543-
void testSingleCharEncoding() {
544-
for (int i = 0; i < 20; i++) {
545-
Base32 codec = new Base32();
546-
final BaseNCodec.Context context = new BaseNCodec.Context();
547-
final byte[] unencoded = new byte[i];
548-
final byte[] allInOne = codec.encode(unencoded);
549-
codec = new Base32();
550-
for (int j = 0; j < unencoded.length; j++) {
551-
codec.encode(unencoded, j, 1, context);
552-
}
553-
codec.encode(unencoded, 0, -1, context);
554-
final byte[] singly = new byte[allInOne.length];
555-
codec.readResults(singly, 0, 100, context);
556-
assertArrayEquals(allInOne, singly);
537+
@ParameterizedTest
538+
@MethodSource("rangeProvider")
539+
void testRandomBytesHex(final int i) {
540+
final Base32 codec = new Base32(true);
541+
final byte[][] b = BaseNTestData.randomData(codec, i);
542+
assertEquals(b[1].length, codec.getEncodedLength(b[0]), i + " " + codec.lineLength);
543+
// assertEquals(b[0], codec.decode(b[1]));
544+
}
545+
546+
@ParameterizedTest
547+
@MethodSource("rangeProvider")
548+
void testSingleCharEncoding(final int i) {
549+
Base32 codec = new Base32();
550+
final BaseNCodec.Context context = new BaseNCodec.Context();
551+
final byte[] unencoded = new byte[i];
552+
final byte[] allInOne = codec.encode(unencoded);
553+
codec = new Base32();
554+
for (int j = 0; j < unencoded.length; j++) {
555+
codec.encode(unencoded, j, 1, context);
557556
}
557+
codec.encode(unencoded, 0, -1, context);
558+
final byte[] singly = new byte[allInOne.length];
559+
codec.readResults(singly, 0, 100, context);
560+
assertArrayEquals(allInOne, singly);
558561
}
559562
}

0 commit comments

Comments
 (0)