Skip to content

Commit 961cfef

Browse files
committed
Add Base64Util with encode/decode and unit tests
1 parent d8ddb07 commit 961cfef

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.Base64;
4+
5+
/**
6+
* Converts text to Base64 and Base64 back to text
7+
*
8+
* @author Ayrton Souza
9+
*/
10+
public final class Base64Util {
11+
12+
private Base64Util() {
13+
}
14+
15+
/**
16+
* Encodes a given string into Base64 format.
17+
*
18+
* @param input The text to be converted to Base64.
19+
* @return Base64 representation of the input text.
20+
* @throws IllegalArgumentException if input is null.
21+
*/
22+
public static String encode(String input) {
23+
if (input == null) {
24+
throw new IllegalArgumentException("Input cannot be null");
25+
}
26+
return Base64.getEncoder().encodeToString(input.getBytes());
27+
}
28+
29+
/**
30+
* Decodes a Base64 string back to original text.
31+
*
32+
* @param base64 The Base64 encoded string.
33+
* @return The decoded original text.
34+
* @throws IllegalArgumentException if base64 is null or not a valid Base64 string.
35+
*/
36+
public static String decode(String base64) {
37+
if (base64 == null) {
38+
throw new IllegalArgumentException("Input cannot be null");
39+
}
40+
try {
41+
byte[] decodedBytes = Base64.getDecoder().decode(base64);
42+
return new String(decodedBytes);
43+
} catch (IllegalArgumentException e) {
44+
throw new IllegalArgumentException("Invalid Base64 input", e);
45+
}
46+
}
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
public class Base64Test{
11+
12+
@Test
13+
// Test encoding and decoding normal strings
14+
public void testEncodeAndDecode() {
15+
String original = "Hello, World!";
16+
String encoded = Base64Util.encode(original);
17+
String decoded = Base64Util.decode(encoded);
18+
19+
assertEquals(original, decoded);
20+
}
21+
22+
@Test
23+
// Test encoding and decoding empty string
24+
public void testEmptyString() {
25+
String encoded = Base64Util.encode("");
26+
String decoded = Base64Util.decode(encoded);
27+
28+
assertEquals("", decoded);
29+
}
30+
31+
@Test
32+
// Test encoding null input
33+
public void testNullInputEncode() {
34+
assertThrows(IllegalArgumentException.class, () -> Base64Util.encode(null));
35+
}
36+
37+
@Test
38+
// Test decoding null input
39+
public void testNullInputDecode() {
40+
assertThrows(IllegalArgumentException.class, () -> Base64Util.decode(null));
41+
}
42+
43+
@ParameterizedTest
44+
@CsvSource({
45+
"invalid@@base64",
46+
"12345$%",
47+
"====",
48+
"abc?def"
49+
})
50+
// Test decoding invalid Base64 strings
51+
void testInvalidBase64Decode(String invalidBase64) {
52+
assertThrows(IllegalArgumentException.class, () -> Base64Util.decode(invalidBase64));
53+
}
54+
55+
}
56+

0 commit comments

Comments
 (0)