Skip to content

Commit 2ae03e1

Browse files
committed
Reduce deprecation warning noise in test cases
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1683555 13f79535-47bb-0310-9956-ffa450edef68
1 parent c82fe35 commit 2ae03e1

13 files changed

Lines changed: 111 additions & 79 deletions

src/test/java/org/apache/commons/codec/CharsetsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @version $Id: CharEncodingTest.java 1298985 2012-03-09 19:12:49Z ggregory $
2929
*/
30+
@SuppressWarnings("deprecation") // TODO remove when Java 7 is minimum and Charsets constants can be replaced
3031
public class CharsetsTest {
3132

3233
@Test

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.fail;
2424

25+
import java.nio.charset.Charset;
2526
import java.util.Arrays;
2627

2728
import org.apache.commons.codec.Charsets;
2829
import org.junit.Test;
2930

3031
public class Base32Test {
3132

33+
@SuppressWarnings("deprecation") // TODO remove when Java 7 is minimum and Charsets constants can be replaced
34+
private static final Charset CHARSET_UTF8 = Charsets.UTF_8;
35+
3236
private static final String [][] BASE32_TEST_CASES = { // RFC 4648
3337
{"" ,""},
3438
{"f" ,"MY======"},
@@ -74,23 +78,23 @@ public class Base32Test {
7478
public void testBase32Samples() throws Exception {
7579
final Base32 codec = new Base32();
7680
for (final String[] element : BASE32_TEST_CASES) {
77-
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
81+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8)));
7882
}
7983
}
8084

8185
@Test
8286
public void testBase32HexSamples() throws Exception {
8387
final Base32 codec = new Base32(true);
8488
for (final String[] element : BASE32HEX_TEST_CASES) {
85-
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
89+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8)));
8690
}
8791
}
8892

8993
@Test
9094
public void testBase32Chunked () throws Exception {
9195
final Base32 codec = new Base32(20);
9296
for (final String[] element : BASE32_TEST_CASES_CHUNKED) {
93-
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
97+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8)));
9498
}
9599
}
96100

@@ -149,7 +153,7 @@ public void testBase32SamplesNonDefaultPadding() throws Exception {
149153
final Base32 codec = new Base32((byte)0x25); // '%' <=> 0x25
150154

151155
for (final String[] element : BASE32_PAD_TEST_CASES) {
152-
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(Charsets.UTF_8)));
156+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8)));
153157
}
154158
}
155159

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ public void testStaticDecodeChunked() throws DecoderException {
504504
}
505505
}
506506

507+
@SuppressWarnings("deprecation") // TODO remove when Java 7 is minimum and Charsets constants can be replaced
507508
private static byte[] utf8(final String s) {
508509

509510
// We would use commons-codec-1.4.jar own utility method for this, but we

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

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.fail;
2525

2626
import java.math.BigInteger;
27+
import java.nio.charset.Charset;
2728
import java.util.Arrays;
2829
import java.util.Random;
2930

@@ -41,6 +42,9 @@
4142
*/
4243
public class Base64Test {
4344

45+
@SuppressWarnings("deprecation") // TODO remove when Java 7 is minimum and Charsets constants can be replaced
46+
private static final Charset CHARSET_UTF8 = Charsets.UTF_8;
47+
4448
private final Random random = new Random();
4549

4650
/**
@@ -147,7 +151,7 @@ public void testCodeInteger1() {
147151
final BigInteger bigInt1 = new BigInteger("85739377120809420210425962799" + "0318636601332086981");
148152

149153
assertEquals(encodedInt1, new String(Base64.encodeInteger(bigInt1)));
150-
assertEquals(bigInt1, Base64.decodeInteger(encodedInt1.getBytes(Charsets.UTF_8)));
154+
assertEquals(bigInt1, Base64.decodeInteger(encodedInt1.getBytes(CHARSET_UTF8)));
151155
}
152156

153157
@Test
@@ -156,7 +160,7 @@ public void testCodeInteger2() {
156160
final BigInteger bigInt2 = new BigInteger("13936727572861167254666467268" + "91466679477132949611");
157161

158162
assertEquals(encodedInt2, new String(Base64.encodeInteger(bigInt2)));
159-
assertEquals(bigInt2, Base64.decodeInteger(encodedInt2.getBytes(Charsets.UTF_8)));
163+
assertEquals(bigInt2, Base64.decodeInteger(encodedInt2.getBytes(CHARSET_UTF8)));
160164
}
161165

162166
@Test
@@ -167,7 +171,7 @@ public void testCodeInteger3() {
167171
+ "4495062430572478766856090958495998158114332651671116876320938126");
168172

169173
assertEquals(encodedInt3, new String(Base64.encodeInteger(bigInt3)));
170-
assertEquals(bigInt3, Base64.decodeInteger(encodedInt3.getBytes(Charsets.UTF_8)));
174+
assertEquals(bigInt3, Base64.decodeInteger(encodedInt3.getBytes(CHARSET_UTF8)));
171175
}
172176

173177
@Test
@@ -184,7 +188,7 @@ public void testCodeInteger4() {
184188
+ "53542091716518238707344493641683483917");
185189

186190
assertEquals(encodedInt4, new String(Base64.encodeInteger(bigInt4)));
187-
assertEquals(bigInt4, Base64.decodeInteger(encodedInt4.getBytes(Charsets.UTF_8)));
191+
assertEquals(bigInt4, Base64.decodeInteger(encodedInt4.getBytes(CHARSET_UTF8)));
188192
}
189193

190194
@Test
@@ -269,54 +273,54 @@ public void testConstructor_Int_ByteArray_Boolean_UrlSafe() {
269273
*/
270274
@Test
271275
public void testDecodePadMarkerIndex2() {
272-
assertEquals("A", new String(Base64.decodeBase64("QQ==".getBytes(Charsets.UTF_8))));
276+
assertEquals("A", new String(Base64.decodeBase64("QQ==".getBytes(CHARSET_UTF8))));
273277
}
274278

275279
/**
276280
* Tests conditional branches for "marker1" test.
277281
*/
278282
@Test
279283
public void testDecodePadMarkerIndex3() {
280-
assertEquals("AA", new String(Base64.decodeBase64("QUE=".getBytes(Charsets.UTF_8))));
281-
assertEquals("AAA", new String(Base64.decodeBase64("QUFB".getBytes(Charsets.UTF_8))));
284+
assertEquals("AA", new String(Base64.decodeBase64("QUE=".getBytes(CHARSET_UTF8))));
285+
assertEquals("AAA", new String(Base64.decodeBase64("QUFB".getBytes(CHARSET_UTF8))));
282286
}
283287

284288
@Test
285289
public void testDecodePadOnly() {
286-
assertEquals(0, Base64.decodeBase64("====".getBytes(Charsets.UTF_8)).length);
287-
assertEquals("", new String(Base64.decodeBase64("====".getBytes(Charsets.UTF_8))));
290+
assertEquals(0, Base64.decodeBase64("====".getBytes(CHARSET_UTF8)).length);
291+
assertEquals("", new String(Base64.decodeBase64("====".getBytes(CHARSET_UTF8))));
288292
// Test truncated padding
289-
assertEquals(0, Base64.decodeBase64("===".getBytes(Charsets.UTF_8)).length);
290-
assertEquals(0, Base64.decodeBase64("==".getBytes(Charsets.UTF_8)).length);
291-
assertEquals(0, Base64.decodeBase64("=".getBytes(Charsets.UTF_8)).length);
292-
assertEquals(0, Base64.decodeBase64("".getBytes(Charsets.UTF_8)).length);
293+
assertEquals(0, Base64.decodeBase64("===".getBytes(CHARSET_UTF8)).length);
294+
assertEquals(0, Base64.decodeBase64("==".getBytes(CHARSET_UTF8)).length);
295+
assertEquals(0, Base64.decodeBase64("=".getBytes(CHARSET_UTF8)).length);
296+
assertEquals(0, Base64.decodeBase64("".getBytes(CHARSET_UTF8)).length);
293297
}
294298

295299
@Test
296300
public void testDecodePadOnlyChunked() {
297-
assertEquals(0, Base64.decodeBase64("====\n".getBytes(Charsets.UTF_8)).length);
298-
assertEquals("", new String(Base64.decodeBase64("====\n".getBytes(Charsets.UTF_8))));
301+
assertEquals(0, Base64.decodeBase64("====\n".getBytes(CHARSET_UTF8)).length);
302+
assertEquals("", new String(Base64.decodeBase64("====\n".getBytes(CHARSET_UTF8))));
299303
// Test truncated padding
300-
assertEquals(0, Base64.decodeBase64("===\n".getBytes(Charsets.UTF_8)).length);
301-
assertEquals(0, Base64.decodeBase64("==\n".getBytes(Charsets.UTF_8)).length);
302-
assertEquals(0, Base64.decodeBase64("=\n".getBytes(Charsets.UTF_8)).length);
303-
assertEquals(0, Base64.decodeBase64("\n".getBytes(Charsets.UTF_8)).length);
304+
assertEquals(0, Base64.decodeBase64("===\n".getBytes(CHARSET_UTF8)).length);
305+
assertEquals(0, Base64.decodeBase64("==\n".getBytes(CHARSET_UTF8)).length);
306+
assertEquals(0, Base64.decodeBase64("=\n".getBytes(CHARSET_UTF8)).length);
307+
assertEquals(0, Base64.decodeBase64("\n".getBytes(CHARSET_UTF8)).length);
304308
}
305309

306310
@Test
307311
public void testDecodeWithWhitespace() throws Exception {
308312

309313
final String orig = "I am a late night coder.";
310314

311-
final byte[] encodedArray = Base64.encodeBase64(orig.getBytes(Charsets.UTF_8));
315+
final byte[] encodedArray = Base64.encodeBase64(orig.getBytes(CHARSET_UTF8));
312316
final StringBuilder intermediate = new StringBuilder(new String(encodedArray));
313317

314318
intermediate.insert(2, ' ');
315319
intermediate.insert(5, '\t');
316320
intermediate.insert(10, '\r');
317321
intermediate.insert(15, '\n');
318322

319-
final byte[] encodedWithWS = intermediate.toString().getBytes(Charsets.UTF_8);
323+
final byte[] encodedWithWS = intermediate.toString().getBytes(CHARSET_UTF8);
320324
final byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
321325

322326
final String dest = new String(decodedWithWS);
@@ -393,7 +397,7 @@ private void testEncodeOverMaxSize(final int maxSize) throws Exception {
393397
@Test
394398
public void testIgnoringNonBase64InDecode() throws Exception {
395399
assertEquals("The quick brown fox jumped over the lazy dogs.", new String(Base64
396-
.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes(Charsets.UTF_8))));
400+
.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes(CHARSET_UTF8))));
397401
}
398402

399403
@Test
@@ -429,37 +433,37 @@ public void testIsUrlSafe() {
429433
@Test
430434
public void testKnownDecodings() {
431435
assertEquals("The quick brown fox jumped over the lazy dogs.", new String(Base64
432-
.decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes(Charsets.UTF_8))));
436+
.decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes(CHARSET_UTF8))));
433437
assertEquals("It was the best of times, it was the worst of times.", new String(Base64
434-
.decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes(Charsets.UTF_8))));
438+
.decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes(CHARSET_UTF8))));
435439
assertEquals("http://jakarta.apache.org/commmons", new String(Base64
436-
.decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes(Charsets.UTF_8))));
440+
.decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes(CHARSET_UTF8))));
437441
assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", new String(Base64
438-
.decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes(Charsets.UTF_8))));
442+
.decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes(CHARSET_UTF8))));
439443
assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }", new String(Base64.decodeBase64("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0="
440-
.getBytes(Charsets.UTF_8))));
441-
assertEquals("xyzzy!", new String(Base64.decodeBase64("eHl6enkh".getBytes(Charsets.UTF_8))));
444+
.getBytes(CHARSET_UTF8))));
445+
assertEquals("xyzzy!", new String(Base64.decodeBase64("eHl6enkh".getBytes(CHARSET_UTF8))));
442446
}
443447

444448
@Test
445449
public void testKnownEncodings() {
446450
assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==", new String(Base64
447-
.encodeBase64("The quick brown fox jumped over the lazy dogs.".getBytes(Charsets.UTF_8))));
451+
.encodeBase64("The quick brown fox jumped over the lazy dogs.".getBytes(CHARSET_UTF8))));
448452
assertEquals(
449453
"YmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJs\r\nYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo\r\nIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi\r\nbGFoIGJsYWg=\r\n",
450454
new String(
451455
Base64
452456
.encodeBase64Chunked("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah"
453-
.getBytes(Charsets.UTF_8))));
457+
.getBytes(CHARSET_UTF8))));
454458
assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==", new String(Base64
455-
.encodeBase64("It was the best of times, it was the worst of times.".getBytes(Charsets.UTF_8))));
459+
.encodeBase64("It was the best of times, it was the worst of times.".getBytes(CHARSET_UTF8))));
456460
assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==", new String(Base64
457-
.encodeBase64("http://jakarta.apache.org/commmons".getBytes(Charsets.UTF_8))));
461+
.encodeBase64("http://jakarta.apache.org/commmons".getBytes(CHARSET_UTF8))));
458462
assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==", new String(Base64
459-
.encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes(Charsets.UTF_8))));
463+
.encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes(CHARSET_UTF8))));
460464
assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=", new String(Base64.encodeBase64("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }"
461-
.getBytes(Charsets.UTF_8))));
462-
assertEquals("eHl6enkh", new String(Base64.encodeBase64("xyzzy!".getBytes(Charsets.UTF_8))));
465+
.getBytes(CHARSET_UTF8))));
466+
assertEquals("eHl6enkh", new String(Base64.encodeBase64("xyzzy!".getBytes(CHARSET_UTF8))));
463467
}
464468

465469
@Test
@@ -501,7 +505,7 @@ public void testObjectDecodeWithInvalidParameter() throws Exception {
501505
public void testObjectDecodeWithValidParameter() throws Exception {
502506

503507
final String original = "Hello World!";
504-
final Object o = Base64.encodeBase64(original.getBytes(Charsets.UTF_8));
508+
final Object o = Base64.encodeBase64(original.getBytes(CHARSET_UTF8));
505509

506510
final Base64 b64 = new Base64();
507511
final Object oDecoded = b64.decode(o);
@@ -526,7 +530,7 @@ public void testObjectEncodeWithInvalidParameter() throws Exception {
526530
public void testObjectEncodeWithValidParameter() throws Exception {
527531

528532
final String original = "Hello World!";
529-
final Object origObj = original.getBytes(Charsets.UTF_8);
533+
final Object origObj = original.getBytes(CHARSET_UTF8);
530534

531535
final Base64 b64 = new Base64();
532536
final Object oEncoded = b64.encode(origObj);
@@ -539,7 +543,7 @@ public void testObjectEncodeWithValidParameter() throws Exception {
539543
@Test
540544
public void testObjectEncode() throws Exception {
541545
final Base64 b64 = new Base64();
542-
assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(Charsets.UTF_8))));
546+
assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(CHARSET_UTF8))));
543547
}
544548

545549
@Test

0 commit comments

Comments
 (0)