2424import static org .junit .Assert .fail ;
2525
2626import java .math .BigInteger ;
27+ import java .nio .charset .Charset ;
2728import java .util .Arrays ;
2829import java .util .Random ;
2930
4142 */
4243public 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 \n YWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo\r \n IGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi\r \n bGFoIGJsYWg=\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