Skip to content

Commit a6fd8da

Browse files
committed
Use StringUtils.join()
Use longer lines
1 parent 937df54 commit a6fd8da

File tree

1 file changed

+15
-58
lines changed

1 file changed

+15
-58
lines changed

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

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertFalse;
23-
import static org.junit.jupiter.api.Assertions.assertNotNull;
2423
import static org.junit.jupiter.api.Assertions.assertNull;
2524
import static org.junit.jupiter.api.Assertions.assertThrows;
2625
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -97,7 +96,6 @@ private static void assertBase64DecodingOfTrailingBits(final int nbits) {
9796
final Base64 defaultCodec = new Base64();
9897
assertFalse(defaultCodec.isStrictDecoding());
9998
assertEquals(CodecPolicy.LENIENT, defaultCodec.getCodecPolicy());
100-
10199
// Create the encoded bytes. The first characters must be valid so fill with 'zero'
102100
// then pad to the block size.
103101
final int length = nbits / 6;
@@ -116,8 +114,7 @@ private static void assertBase64DecodingOfTrailingBits(final int nbits) {
116114
// If the lower bits are set we expect an exception. This is not a valid
117115
// final character.
118116
if (invalid || (i & emptyBitsMask) != 0) {
119-
assertThrows(IllegalArgumentException.class, () -> codec.decode(encoded),
120-
"Final base-64 digit should not be allowed");
117+
assertThrows(IllegalArgumentException.class, () -> codec.decode(encoded), "Final base-64 digit should not be allowed");
121118
// The default lenient mode should decode this
122119
final byte[] decoded = defaultCodec.decode(encoded);
123120
// Re-encoding should not match the original array as it was invalid
@@ -149,7 +146,7 @@ static Stream<Object> testIsBase64() {
149146
Arguments.of(new byte[] { 'A', 'Z', 'a' }, true),
150147
Arguments.of(new byte[] { '/', '=', '+' }, true),
151148
Arguments.of(new byte[] { '$' }, false));
152-
// @formatter:off
149+
// @formatter:on
153150
}
154151

155152
private final Random random = new Random();
@@ -287,7 +284,6 @@ void testByteToStringVariations() throws DecoderException {
287284
final byte[] b2 = {};
288285
final byte[] b3 = null;
289286
final byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090"); // for url-safe tests
290-
291287
assertEquals("SGVsbG8gV29ybGQ=", base64.encodeToString(b1), "byteToString Hello World");
292288
assertEquals("SGVsbG8gV29ybGQ=", Base64.encodeBase64String(b1), "byteToString static Hello World");
293289
assertEquals("", base64.encodeToString(b2), "byteToString \"\"");
@@ -305,9 +301,7 @@ void testByteToStringVariations() throws DecoderException {
305301
@Test
306302
void testChunkedEncodeMultipleOf76() {
307303
final byte[] expectedEncode = Base64.encodeBase64(BaseNTestData.DECODED, true);
308-
// convert to "\r\n" so we're equal to the old openssl encoding test
309-
// stored
310-
// in Base64TestData.ENCODED_76_CHARS_PER_LINE:
304+
// convert to "\r\n" so we're equal to the old openssl encoding test stored in Base64TestData.ENCODED_76_CHARS_PER_LINE:
311305
final String actualResult = Base64TestData.ENCODED_76_CHARS_PER_LINE.replace("\n", "\r\n");
312306
final byte[] actualEncode = StringUtils.getBytesUtf8(actualResult);
313307
assertArrayEquals(expectedEncode, actualEncode, "chunkedEncodeMultipleOf76");
@@ -339,27 +333,22 @@ void testCodec263() {
339333
void testCodec265() {
340334
// 1GiB file to encode: 2^30 bytes
341335
final int size1GiB = 1 << 30;
342-
343336
// Expecting a size of 4 output bytes per 3 input bytes plus the trailing bytes
344337
// padded to a block size of 4.
345338
final int blocks = (int) Math.ceil(size1GiB / 3.0);
346339
final int expectedLength = 4 * blocks;
347-
348340
// This test is memory hungry. Check we can run it.
349341
final long presumableFreeMemory = BaseNCodecTest.getPresumableFreeMemory();
350-
351342
// Estimate the maximum memory required:
352-
// 1GiB + 1GiB + ~2GiB + ~1.33GiB + 32 KiB = ~5.33GiB
343+
// 1GiB + 1GiB + ~2GiB + ~1.33GiB + 32 KiB = ~5.33GiB
353344
//
354345
// 1GiB: Input buffer to encode
355346
// 1GiB: Existing working buffer (due to doubling of default buffer size of 8192)
356347
// ~2GiB: New working buffer to allocate (due to doubling)
357348
// ~1.33GiB: Expected output size (since the working buffer is copied at the end)
358349
// 32KiB: Some headroom
359350
final long estimatedMemory = (long) size1GiB * 4 + expectedLength + 32 * 1024;
360-
Assumptions.assumeTrue(presumableFreeMemory > estimatedMemory,
361-
"Not enough free memory for the test");
362-
351+
Assumptions.assumeTrue(presumableFreeMemory > estimatedMemory, "Not enough free memory for the test");
363352
final byte[] bytes = new byte[size1GiB];
364353
final byte[] encoded = Base64.encodeBase64(bytes);
365354
assertEquals(expectedLength, encoded.length);
@@ -379,7 +368,6 @@ void testCodec68() {
379368
void testCodeInteger1() {
380369
final String encodedInt1 = "li7dzDacuo67Jg7mtqEm2TRuOMU=";
381370
final BigInteger bigInt1 = new BigInteger("857393771208094202104259627990318636601332086981");
382-
383371
assertEquals(encodedInt1, new String(Base64.encodeInteger(bigInt1)));
384372
assertEquals(bigInt1, Base64.decodeInteger(encodedInt1.getBytes(CHARSET_UTF8)));
385373
}
@@ -388,7 +376,6 @@ void testCodeInteger1() {
388376
void testCodeInteger2() {
389377
final String encodedInt2 = "9B5ypLY9pMOmtxCeTDHgwdNFeGs=";
390378
final BigInteger bigInt2 = new BigInteger("1393672757286116725466646726891466679477132949611");
391-
392379
assertEquals(encodedInt2, new String(Base64.encodeInteger(bigInt2)));
393380
assertEquals(bigInt2, Base64.decodeInteger(encodedInt2.getBytes(CHARSET_UTF8)));
394381
}
@@ -401,7 +388,6 @@ void testCodeInteger3() {
401388
"10806548154093873461951748545" +
402389
"1196989136416448805819079363524309897749044958112417136240557" +
403390
"4495062430572478766856090958495998158114332651671116876320938126");
404-
405391
assertEquals(encodedInt3, new String(Base64.encodeInteger(bigInt3)));
406392
assertEquals(bigInt3, Base64.decodeInteger(encodedInt3.getBytes(CHARSET_UTF8)));
407393
}
@@ -418,7 +404,6 @@ void testCodeInteger4() {
418404
"748008534040890923814202286633163248086055216976551456088015" +
419405
"338880713818192088877057717530169381044092839402438015097654" +
420406
"53542091716518238707344493641683483917");
421-
422407
assertEquals(encodedInt4, new String(Base64.encodeInteger(bigInt4)));
423408
assertEquals(bigInt4, Base64.decodeInteger(encodedInt4.getBytes(CHARSET_UTF8)));
424409
}
@@ -430,8 +415,7 @@ void testCodeIntegerEdgeCases() {
430415

431416
@Test
432417
void testCodeIntegerNull() {
433-
assertThrows(NullPointerException.class, () -> Base64.encodeInteger(null),
434-
"Exception not thrown when passing in null to encodeInteger(BigInteger)");
418+
assertThrows(NullPointerException.class, () -> Base64.encodeInteger(null), "Exception not thrown when passing in null to encodeInteger(BigInteger)");
435419
}
436420

437421
@Test
@@ -451,7 +435,6 @@ void testConstructor_Int_ByteArray_Boolean_UrlSafe() {
451435
final byte[] encoded = base64.encode(BaseNTestData.DECODED);
452436
String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE;
453437
expectedResult = expectedResult.replace("=", ""); // url-safe has no
454-
// == padding.
455438
expectedResult = expectedResult.replace('\n', '\t');
456439
expectedResult = expectedResult.replace('+', '-');
457440
expectedResult = expectedResult.replace('/', '_');
@@ -483,9 +466,7 @@ void testConstructors() {
483466
assertThrows(IllegalArgumentException.class, () -> new Base64(64, new byte[] { 'A', '$' }),
484467
"Should have rejected attempt to use 'A$' as a line separator");
485468

486-
base64 = new Base64(64, new byte[] { ' ', '$', '\n', '\r', '\t' }); // OK
487-
488-
assertNotNull(base64);
469+
base64 = new Base64(64, new byte[] { ' ', '$', '\n', '\r', '\t' }); // OKassertNotNull(base64);
489470
}
490471

491472
@Test
@@ -729,10 +710,7 @@ void testIsStringBase64() {
729710
final String emptyString = "";
730711
final String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL==============";
731712
final String invalidString = validString + (char) 0; // append null character
732-
733-
assertThrows(NullPointerException.class, () -> Base64.isBase64(nullString),
734-
"Base64.isStringBase64() should not be null-safe.");
735-
713+
assertThrows(NullPointerException.class, () -> Base64.isBase64(nullString), "Base64.isStringBase64() should not be null-safe.");
736714
assertTrue(Base64.isBase64(emptyString), "Base64.isStringBase64(empty-string) is true");
737715
assertTrue(Base64.isBase64(validString), "Base64.isStringBase64(valid-string) is true");
738716
assertFalse(Base64.isBase64(invalidString), "Base64.isStringBase64(invalid-string) is false");
@@ -790,42 +768,32 @@ void testKnownEncodings() {
790768

791769
@Test
792770
void testNonBase64Test() throws Exception {
793-
794771
final byte[] bArray = { '%' };
795-
796-
assertFalse(Base64.isBase64(bArray),
797-
"Invalid Base64 array was incorrectly validated as an array of Base64 encoded data");
798-
772+
assertFalse(Base64.isBase64(bArray), "Invalid Base64 array was incorrectly validated as an array of Base64 encoded data");
799773
try {
800774
final Base64 b64 = new Base64();
801775
final byte[] result = b64.decode(bArray);
802-
803-
assertEquals(0, result.length, "The result should be empty as the test encoded content did " +
804-
"not contain any valid base 64 characters");
776+
assertEquals(0, result.length, "The result should be empty as the test encoded content did not contain any valid base 64 characters");
805777
} catch (final Exception e) {
806-
fail("Exception '" + e.getClass().getName() + "' was thrown when trying to decode " +
807-
"invalid base64 encoded data - RFC 2045 requires that all " +
808-
"non base64 character be discarded, an exception should not have been thrown");
778+
fail("Exception '" + e.getClass().getName() + "' was thrown when trying to decode invalid base64 encoded data - RFC 2045 requires that all " +
779+
"non base64 character be discarded, an exception should not have been thrown");
809780
}
810781
}
811782

812783
@Test
813784
void testObjectDecodeWithInvalidParameter() {
814785
assertThrows(DecoderException.class, () -> new Base64().decode(Integer.valueOf(5)),
815-
"decode(Object) didn't throw an exception when passed an Integer object");
786+
"decode(Object) didn't throw an exception when passed an Integer object");
816787
}
817788

818789
@Test
819790
void testObjectDecodeWithValidParameter() throws Exception {
820-
821791
final String original = "Hello World!";
822792
final Object o = Base64.encodeBase64(original.getBytes(CHARSET_UTF8));
823-
824793
final Base64 b64 = new Base64();
825794
final Object oDecoded = b64.decode(o);
826795
final byte[] baDecoded = (byte[]) oDecoded;
827796
final String dest = new String(baDecoded);
828-
829797
assertEquals(original, dest, "dest string does not equal original");
830798
}
831799

@@ -837,21 +805,17 @@ void testObjectEncode() throws Exception {
837805

838806
@Test
839807
void testObjectEncodeWithInvalidParameter() {
840-
assertThrows(EncoderException.class, () -> new Base64().encode("Yadayadayada"),
841-
"encode(Object) didn't throw an exception when passed a String object");
808+
assertThrows(EncoderException.class, () -> new Base64().encode("Yadayadayada"), "encode(Object) didn't throw an exception when passed a String object");
842809
}
843810

844811
@Test
845812
void testObjectEncodeWithValidParameter() throws Exception {
846-
847813
final String original = "Hello World!";
848814
final Object origObj = original.getBytes(CHARSET_UTF8);
849-
850815
final Base64 b64 = new Base64();
851816
final Object oEncoded = b64.encode(origObj);
852817
final byte[] bArray = Base64.decodeBase64((byte[]) oEncoded);
853818
final String dest = new String(bArray);
854-
855819
assertEquals(original, dest, "dest string does not equal original");
856820
}
857821

@@ -1507,13 +1471,6 @@ void testUUID() throws DecoderException {
15071471
}
15081472

15091473
private String toString(final byte[] data) {
1510-
final StringBuilder buf = new StringBuilder();
1511-
for (int i = 0; i < data.length; i++) {
1512-
buf.append(data[i]);
1513-
if (i != data.length - 1) {
1514-
buf.append(",");
1515-
}
1516-
}
1517-
return buf.toString();
1474+
return org.apache.commons.lang3.StringUtils.join(data, ',');
15181475
}
15191476
}

0 commit comments

Comments
 (0)