@@ -167,56 +167,6 @@ public void testBase64() {
167167 assertEquals ("Hello World" , decodeString , "decode hello world" );
168168 }
169169
170- @ Test
171- public void testCustomEncodingAlphabet_illegal () {
172- byte [] encodeTable = {
173- '.' , '-' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M'
174- };
175- assertThrows (IllegalArgumentException .class , () -> new Base64 (encodeTable ));
176- }
177-
178- @ Test
179- public void testCustomEncodingAlphabet () {
180- // created a duplicate of STANDARD_ENCODE_TABLE and replaced two chars with
181- // custom values not already present in table
182- // A => . B => -
183- byte [] encodeTable = {
184- '.' , '-' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
185- 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
186- 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' ,
187- 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
188- '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/'
189- };
190-
191- // two instances: one with default table and one with adjusted encoding table
192- Base64 b64 = new Base64 ();
193- Base64 b64customEncoding = new Base64 (encodeTable );
194-
195- final String content = "! Hello World - this §$%" ;
196-
197- byte [] encodedBytes = b64 .encode (StringUtils .getBytesUtf8 (content ));
198- String encodedContent = StringUtils .newStringUtf8 (encodedBytes );
199-
200- byte [] encodedBytesCustom = b64customEncoding .encode (StringUtils .getBytesUtf8 (content ));
201- String encodedContentCustom = StringUtils .newStringUtf8 (encodedBytesCustom );
202-
203- assertTrue (
204- encodedContent .contains ("A" ) && encodedContent .contains ("B" ), "testing precondition not met - ecodedContent should contain parts of modified table" );
205-
206- assertEquals (
207- encodedContent
208- .replaceAll ("A" , "." ).replaceAll ("B" , "-" ) // replace alphabet adjustments
209- .replaceAll ("=" , "" ) // remove padding (not default alphabet)
210- , encodedContentCustom );
211-
212-
213- // try decode encoded content
214- final byte [] decode = b64customEncoding .decode (encodedBytesCustom );
215- final String decodeString = StringUtils .newStringUtf8 (decode );
216-
217- assertEquals (content , decodeString );
218- }
219-
220170 @ Test
221171 public void testBase64AtBufferEnd () {
222172 testBase64InBuffer (100 , 0 );
@@ -465,6 +415,56 @@ public void testConstructors() {
465415 assertNotNull (base64 );
466416 }
467417
418+ @ Test
419+ public void testCustomEncodingAlphabet () {
420+ // created a duplicate of STANDARD_ENCODE_TABLE and replaced two chars with
421+ // custom values not already present in table
422+ // A => . B => -
423+ byte [] encodeTable = {
424+ '.' , '-' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
425+ 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
426+ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' ,
427+ 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
428+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/'
429+ };
430+
431+ // two instances: one with default table and one with adjusted encoding table
432+ Base64 b64 = new Base64 ();
433+ Base64 b64customEncoding = new Base64 (encodeTable );
434+
435+ final String content = "! Hello World - this §$%" ;
436+
437+ byte [] encodedBytes = b64 .encode (StringUtils .getBytesUtf8 (content ));
438+ String encodedContent = StringUtils .newStringUtf8 (encodedBytes );
439+
440+ byte [] encodedBytesCustom = b64customEncoding .encode (StringUtils .getBytesUtf8 (content ));
441+ String encodedContentCustom = StringUtils .newStringUtf8 (encodedBytesCustom );
442+
443+ assertTrue (
444+ encodedContent .contains ("A" ) && encodedContent .contains ("B" ), "testing precondition not met - ecodedContent should contain parts of modified table" );
445+
446+ assertEquals (
447+ encodedContent
448+ .replaceAll ("A" , "." ).replaceAll ("B" , "-" ) // replace alphabet adjustments
449+ .replaceAll ("=" , "" ) // remove padding (not default alphabet)
450+ , encodedContentCustom );
451+
452+
453+ // try decode encoded content
454+ final byte [] decode = b64customEncoding .decode (encodedBytesCustom );
455+ final String decodeString = StringUtils .newStringUtf8 (decode );
456+
457+ assertEquals (content , decodeString );
458+ }
459+
460+ @ Test
461+ public void testCustomEncodingAlphabet_illegal () {
462+ byte [] encodeTable = {
463+ '.' , '-' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M'
464+ };
465+ assertThrows (IllegalArgumentException .class , () -> new Base64 (encodeTable ));
466+ }
467+
468468 private void testDecodeEncode (final String encodedText ) {
469469 final String decodedText = StringUtils .newStringUsAscii (Base64 .decodeBase64 (encodedText ));
470470 final String encodedText2 = Base64 .encodeBase64String (StringUtils .getBytesUtf8 (decodedText ));
0 commit comments