test: tests for Base64Util
This commit is contained in:
@@ -58,8 +58,6 @@ import java.util.Objects;
|
||||
*/
|
||||
public final class Base64Util {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(Base64Util.class);
|
||||
|
||||
/**
|
||||
* Ensure that there is only one Base64 Encoder.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.onixbyte.devkit.utils;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Base64UtilTest {
|
||||
|
||||
@Test
|
||||
void testEncodeAndDecodeWithUtf8() {
|
||||
var original = "Hello, Base64!";
|
||||
var encoded = Base64Util.encode(original);
|
||||
assertNotNull(encoded);
|
||||
assertNotEquals(original, encoded);
|
||||
|
||||
var decoded = Base64Util.decode(encoded);
|
||||
assertNotNull(decoded);
|
||||
assertEquals(original, decoded);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEncodeAndDecodeWithCharset() {
|
||||
var original = "编码测试"; // Some unicode characters (Chinese)
|
||||
var charset = StandardCharsets.UTF_8;
|
||||
|
||||
var encoded = Base64Util.encode(original, charset);
|
||||
assertNotNull(encoded);
|
||||
assertNotEquals(original, encoded);
|
||||
|
||||
var decoded = Base64Util.decode(encoded, charset);
|
||||
assertNotNull(decoded);
|
||||
assertEquals(original, decoded);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEncodeUrlComponentsAndDecodeWithUtf8() {
|
||||
var original = "This is a test for URL-safe Base64 encoding+!";
|
||||
|
||||
var encodedUrl = Base64Util.encodeUrlComponents(original);
|
||||
assertNotNull(encodedUrl);
|
||||
assertNotEquals(original, encodedUrl);
|
||||
// URL-safe encoding should not contain '+' or '/' characters
|
||||
assertFalse(encodedUrl.contains("+"));
|
||||
assertFalse(encodedUrl.contains("/"));
|
||||
|
||||
var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl);
|
||||
assertNotNull(decodedUrl);
|
||||
assertEquals(original, decodedUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEncodeUrlComponentsAndDecodeWithCharset() {
|
||||
var original = "测试 URL 安全编码"; // Unicode string
|
||||
var charset = StandardCharsets.UTF_8;
|
||||
|
||||
var encodedUrl = Base64Util.encodeUrlComponents(original, charset);
|
||||
assertNotNull(encodedUrl);
|
||||
assertNotEquals(original, encodedUrl);
|
||||
|
||||
var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl, charset);
|
||||
assertNotNull(decodedUrl);
|
||||
assertEquals(original, decodedUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEncodeAndDecodeEmptyString() {
|
||||
var original = "";
|
||||
|
||||
var encoded = Base64Util.encode(original);
|
||||
assertNotNull(encoded);
|
||||
assertEquals("", Base64Util.decode(encoded));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEncodeAndDecodeNullSafety() {
|
||||
// Since Base64Util does not explicitly handle null, the test expects NPE if null is input
|
||||
assertThrows(NullPointerException.class, () -> Base64Util.encode(null));
|
||||
assertThrows(NullPointerException.class, () -> Base64Util.decode(null));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 OnixByte.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.onixbyte.devkit.utils;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestBase64Util {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(TestBase64Util.class);
|
||||
|
||||
@Test
|
||||
public void testEncode() {
|
||||
Assertions.assertEquals("SGVsbG8gV29ybGQ=", Base64Util.encode("Hello World"));
|
||||
Assertions.assertEquals("MTI3LjAuMC4x", Base64Util.encode("127.0.0.1"));
|
||||
Assertions.assertEquals("cm9vdA==", Base64Util.encode("root"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode() {
|
||||
Assertions.assertEquals("Hello World", Base64Util.decode("SGVsbG8gV29ybGQ="));
|
||||
Assertions.assertEquals("127.0.0.1", Base64Util.decode("MTI3LjAuMC4x"));
|
||||
Assertions.assertEquals("root", Base64Util.decode("cm9vdA=="));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeUriComponent() {
|
||||
Assertions.assertEquals("aHR0cHM6Ly9nb29nbGUuY29t", Base64Util.encodeUrlComponents("https://google.com"));
|
||||
Assertions.assertEquals("aHR0cDovLzEyNy4wLjAuMTo4MDgwL2FwaS91c2VyLzEyMzQ1", Base64Util.encodeUrlComponents("http://127.0.0.1:8080/api/user/12345"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeUriComponent() {
|
||||
Assertions.assertEquals("https://google.com", Base64Util.decodeUrlComponents("aHR0cHM6Ly9nb29nbGUuY29t"));
|
||||
Assertions.assertEquals("http://127.0.0.1:8080/api/user/12345", Base64Util.decodeUrlComponents("aHR0cDovLzEyNy4wLjAuMTo4MDgwL2FwaS91c2VyLzEyMzQ1"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user