Skip to content

Commit 767ecc1

Browse files
committed
Use try-with-resources; use final.
1 parent f5ed85f commit 767ecc1

11 files changed

Lines changed: 60 additions & 59 deletions

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testBase16EmptyOutputStream() throws IOException {
5252
* @throws IOException for some failure scenarios.
5353
*/
5454
@Test
55-
public void testBase16OutputStreamByChunk() throws Exception {
55+
public void testBase16OutputStreamByChunk() throws IOException {
5656
// Hello World test.
5757
byte[] encoded = StringUtils.getBytesUtf8("48656C6C6F20576F726C64");
5858
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
@@ -146,9 +146,9 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final boole
146146
}
147147

148148
// wrap encoder with decoder
149-
try (final ByteArrayOutputStream byteOut = new ByteArrayOutputStream()) {
150-
final OutputStream decoderOut = new Base16OutputStream(byteOut, false, lowerCase);
151-
final OutputStream encoderOut = new Base16OutputStream(decoderOut, true, lowerCase);
149+
try (final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
150+
final OutputStream decoderOut = new Base16OutputStream(byteOut, false, lowerCase);
151+
final OutputStream encoderOut = new Base16OutputStream(decoderOut, true, lowerCase)) {
152152

153153
encoderOut.write(decoded);
154154
final byte[] output = byteOut.toByteArray();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,13 @@ public void testSkipWrongArgument() throws Throwable {
537537
public void testStrictDecoding() throws Exception {
538538
for (final String s : Base32Test.BASE32_IMPOSSIBLE_CASES) {
539539
final byte[] encoded = StringUtils.getBytesUtf8(s);
540-
Base32InputStream in = new Base32InputStream(new ByteArrayInputStream(encoded), false);
540+
final Base32InputStream in = new Base32InputStream(new ByteArrayInputStream(encoded), false);
541541
// Default is lenient decoding; it should not throw
542542
assertFalse(in.isStrictDecoding());
543543
BaseNTestData.streamToBytes(in);
544544

545545
// Strict decoding should throw
546-
Base32InputStream in2 = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
546+
final Base32InputStream in2 = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
547547
assertTrue(in2.isStrictDecoding());
548548
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
549549
}

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
203203
out.close();
204204
output = byteOut.toByteArray();
205205

206-
assertArrayEquals(decoded, output, "Streaming chunked Base32 wrap-wrap-wrap!");
206+
assertArrayEquals(decoded, byteOut.toByteArray(), "Streaming chunked Base32 wrap-wrap-wrap!");
207207
}
208208

209209
/**
@@ -316,19 +316,20 @@ public void testStrictDecoding() throws Exception {
316316
for (final String s : Base32Test.BASE32_IMPOSSIBLE_CASES) {
317317
final byte[] encoded = StringUtils.getBytesUtf8(s);
318318
ByteArrayOutputStream bout = new ByteArrayOutputStream();
319-
Base32OutputStream out = new Base32OutputStream(bout, false);
320-
// Default is lenient decoding; it should not throw
321-
assertFalse(out.isStrictDecoding());
322-
out.write(encoded);
323-
out.close();
324-
assertTrue(bout.size() > 0);
325-
326-
// Strict decoding should throw
327-
bout = new ByteArrayOutputStream();
328-
final Base32OutputStream out2 = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
329-
assertTrue(out2.isStrictDecoding());
330-
assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
331-
out2.close();
319+
try (Base32OutputStream out = new Base32OutputStream(bout, false)) {
320+
// Default is lenient decoding; it should not throw
321+
assertFalse(out.isStrictDecoding());
322+
out.write(encoded);
323+
out.close();
324+
assertTrue(bout.size() > 0);
325+
326+
// Strict decoding should throw
327+
bout = new ByteArrayOutputStream();
328+
try (final Base32OutputStream out2 = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT)) {
329+
assertTrue(out2.isStrictDecoding());
330+
assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
331+
}
332+
}
332333
}
333334
}
334335
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,13 @@ public void testSkipWrongArgument() throws Throwable {
575575
public void testStrictDecoding() throws Exception {
576576
for (final String s : Base64Test.BASE64_IMPOSSIBLE_CASES) {
577577
final byte[] encoded = StringUtils.getBytesUtf8(s);
578-
Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded), false);
578+
final Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded), false);
579579
// Default is lenient decoding; it should not throw
580580
assertFalse(in.isStrictDecoding());
581581
BaseNTestData.streamToBytes(in);
582582

583583
// Strict decoding should throw
584-
Base64InputStream in2 = new Base64InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
584+
final Base64InputStream in2 = new Base64InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
585585
assertTrue(in2.isStrictDecoding());
586586
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
587587
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public void testStrictDecoding() throws Exception {
334334

335335
// Strict decoding should throw
336336
bout = new ByteArrayOutputStream();
337-
Base64OutputStream out = new Base64OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
337+
final Base64OutputStream out = new Base64OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
338338
// May throw on write or on close depending on the position of the
339339
// impossible last character in the output block size
340340
assertThrows(IllegalArgumentException.class, () -> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public void testNonBase64Test() throws Exception {
520520

521521
try {
522522
final Base64 b64 = new Base64();
523-
byte[] result = b64.decode(bArray);
523+
final byte[] result = b64.decode(bArray);
524524

525525
assertEquals(0, result.length, "The result should be empty as the test encoded content did " +
526526
"not contain any valid base 64 characters");

src/test/java/org/apache/commons/codec/digest/Blake3TestVectorsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private void initData(final int inputLength, final String hash, final String key
279279

280280
@ParameterizedTest
281281
@MethodSource("data")
282-
public void hashArbitraryOutputLength(int inputLength, String hash, String keyedHash, String deriveKey) throws DecoderException {
282+
public void hashArbitraryOutputLength(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException {
283283
initData(inputLength, hash, keyedHash, deriveKey);
284284
hasher.update(inputByteArray);
285285
final byte[] actual = hasher.doFinalize(hashByteArray.length);
@@ -288,15 +288,15 @@ public void hashArbitraryOutputLength(int inputLength, String hash, String keyed
288288

289289
@ParameterizedTest
290290
@MethodSource("data")
291-
public void hashTruncatedOutput(int inputLength, String hash, String keyedHash, String deriveKey) throws DecoderException {
291+
public void hashTruncatedOutput(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException {
292292
initData(inputLength, hash, keyedHash, deriveKey);
293293
final byte[] actual = Blake3.hash(inputByteArray);
294294
assertArrayEquals(Arrays.copyOf(this.hashByteArray, 32), actual);
295295
}
296296

297297
@ParameterizedTest
298298
@MethodSource("data")
299-
public void keyedHashArbitraryOutputLength(int inputLength, String hash, String keyedHash, String deriveKey) throws DecoderException {
299+
public void keyedHashArbitraryOutputLength(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException {
300300
initData(inputLength, hash, keyedHash, deriveKey);
301301
keyedHasher.update(inputByteArray);
302302
final byte[] actual = keyedHasher.doFinalize(keyedHashByteArray.length);
@@ -305,15 +305,15 @@ public void keyedHashArbitraryOutputLength(int inputLength, String hash, String
305305

306306
@ParameterizedTest
307307
@MethodSource("data")
308-
public void keyedHashTruncatedOutput(int inputLength, String hash, String keyedHash, String deriveKey) throws DecoderException {
308+
public void keyedHashTruncatedOutput(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException {
309309
initData(inputLength, hash, keyedHash, deriveKey);
310310
final byte[] actual = Blake3.keyedHash(KEY, inputByteArray);
311311
assertArrayEquals(Arrays.copyOf(keyedHashByteArray, 32), actual);
312312
}
313313

314314
@ParameterizedTest
315315
@MethodSource("data")
316-
public void keyDerivation(int inputLength, String hash, String keyedHash, String deriveKey) throws DecoderException {
316+
public void keyDerivation(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException {
317317
initData(inputLength, hash, keyedHash, deriveKey);
318318
kdfHasher.update(inputByteArray);
319319
final byte[] actual = kdfHasher.doFinalize(deriveKeyByteArray.length);

src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void tearDown() throws Exception {
124124

125125
@ParameterizedTest
126126
@MethodSource("data")
127-
public void testAlgorithm(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) throws NoSuchAlgorithmException {
127+
public void testAlgorithm(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) throws NoSuchAlgorithmException {
128128
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
129129
final String algorithm = hmacAlgorithm.getName();
130130
assertNotNull(algorithm);
@@ -135,63 +135,63 @@ public void testAlgorithm(HmacAlgorithms hmacAlgorithm, byte[] standardResultByt
135135

136136
@ParameterizedTest
137137
@MethodSource("data")
138-
public void testGetHmacEmptyKey(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
138+
public void testGetHmacEmptyKey(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
139139
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
140140
assertThrows(IllegalArgumentException.class, () -> HmacUtils.getInitializedMac(hmacAlgorithm, EMPTY_BYTE_ARRAY));
141141
}
142142

143143
@ParameterizedTest
144144
@MethodSource("data")
145-
public void testGetHmacNullKey(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
145+
public void testGetHmacNullKey(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
146146
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
147147
assertThrows(IllegalArgumentException.class, () -> HmacUtils.getInitializedMac(hmacAlgorithm, null));
148148
}
149149

150150
@ParameterizedTest
151151
@MethodSource("data")
152-
public void testHmacFailByteArray(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
152+
public void testHmacFailByteArray(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
153153
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
154154
assertThrows(IllegalArgumentException.class, () -> new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(STANDARD_PHRASE_BYTES));
155155
}
156156

157157
@ParameterizedTest
158158
@MethodSource("data")
159-
public void testHmacFailInputStream(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
159+
public void testHmacFailInputStream(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
160160
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
161161
assertThrows(IllegalArgumentException.class, () -> new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(new ByteArrayInputStream(STANDARD_PHRASE_BYTES)));
162162
}
163163

164164
@ParameterizedTest
165165
@MethodSource("data")
166-
public void testHmacFailString(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
166+
public void testHmacFailString(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
167167
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
168168
assertThrows(IllegalArgumentException.class, () -> new HmacUtils(hmacAlgorithm, (String) null).hmac(STANDARD_PHRASE_STRING));
169169
}
170170

171171
@ParameterizedTest
172172
@MethodSource("data")
173-
public void testHmacHexFailByteArray(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
173+
public void testHmacHexFailByteArray(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
174174
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
175175
assertThrows(IllegalArgumentException.class, () -> new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(STANDARD_PHRASE_BYTES));
176176
}
177177

178178
@ParameterizedTest
179179
@MethodSource("data")
180-
public void testHmacHexFailInputStream(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
180+
public void testHmacHexFailInputStream(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
181181
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
182182
assertThrows(IllegalArgumentException.class, () -> new HmacUtils(hmacAlgorithm, (byte[]) null).hmac(new ByteArrayInputStream(STANDARD_PHRASE_BYTES)));
183183
}
184184

185185
@ParameterizedTest
186186
@MethodSource("data")
187-
public void testHmacHexFailString(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
187+
public void testHmacHexFailString(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
188188
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
189189
assertThrows(IllegalArgumentException.class, () -> new HmacUtils(hmacAlgorithm, (String) null).hmac(STANDARD_PHRASE_STRING));
190190
}
191191

192192
@ParameterizedTest
193193
@MethodSource("data")
194-
public void testInitializedMac(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
194+
public void testInitializedMac(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
195195
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
196196
final Mac mac = HmacUtils.getInitializedMac(hmacAlgorithm, STANDARD_KEY_BYTES);
197197
final Mac mac2 = HmacUtils.getInitializedMac(hmacAlgorithm.getName(), STANDARD_KEY_BYTES);
@@ -201,44 +201,44 @@ public void testInitializedMac(HmacAlgorithms hmacAlgorithm, byte[] standardResu
201201

202202
@ParameterizedTest
203203
@MethodSource("data")
204-
public void testMacByteArary(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
204+
public void testMacByteArary(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
205205
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
206206
assertArrayEquals(standardResultBytes, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmac(STANDARD_PHRASE_BYTES));
207207
}
208208

209209
@ParameterizedTest
210210
@MethodSource("data")
211-
public void testMacHexByteArray(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
211+
public void testMacHexByteArray(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
212212
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
213213
assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(STANDARD_PHRASE_BYTES));
214214
}
215215

216216
@ParameterizedTest
217217
@MethodSource("data")
218-
public void testMacHexInputStream(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) throws IOException {
218+
public void testMacHexInputStream(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) throws IOException {
219219
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
220220
assertEquals(standardResultString,
221221
new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(new ByteArrayInputStream(STANDARD_PHRASE_BYTES)));
222222
}
223223

224224
@ParameterizedTest
225225
@MethodSource("data")
226-
public void testMacHexString(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
226+
public void testMacHexString(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
227227
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
228228
assertEquals(standardResultString, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmacHex(STANDARD_PHRASE_STRING));
229229
}
230230

231231
@ParameterizedTest
232232
@MethodSource("data")
233-
public void testMacInputStream(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) throws IOException {
233+
public void testMacInputStream(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) throws IOException {
234234
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
235235
assertArrayEquals(standardResultBytes,
236236
new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmac(new ByteArrayInputStream(STANDARD_PHRASE_BYTES)));
237237
}
238238

239239
@ParameterizedTest
240240
@MethodSource("data")
241-
public void testMacString(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) {
241+
public void testMacString(final HmacAlgorithms hmacAlgorithm, final byte[] standardResultBytes, final String standardResultString) {
242242
assumeTrue(HmacUtils.isAvailable(hmacAlgorithm));
243243
assertArrayEquals(standardResultBytes, new HmacUtils(hmacAlgorithm, STANDARD_KEY_BYTES).hmac(STANDARD_PHRASE_STRING));
244244
}

0 commit comments

Comments
 (0)