|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 OnixByte. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.devkit.utils; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.*; |
| 28 | + |
| 29 | +public class Base64UtilTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + void testEncodeAndDecodeWithUtf8() { |
| 33 | + var original = "Hello, Base64!"; |
| 34 | + var encoded = Base64Util.encode(original); |
| 35 | + assertNotNull(encoded); |
| 36 | + assertNotEquals(original, encoded); |
| 37 | + |
| 38 | + var decoded = Base64Util.decode(encoded); |
| 39 | + assertNotNull(decoded); |
| 40 | + assertEquals(original, decoded); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testEncodeAndDecodeWithCharset() { |
| 45 | + var original = "编码测试"; // Some unicode characters (Chinese) |
| 46 | + var charset = StandardCharsets.UTF_8; |
| 47 | + |
| 48 | + var encoded = Base64Util.encode(original, charset); |
| 49 | + assertNotNull(encoded); |
| 50 | + assertNotEquals(original, encoded); |
| 51 | + |
| 52 | + var decoded = Base64Util.decode(encoded, charset); |
| 53 | + assertNotNull(decoded); |
| 54 | + assertEquals(original, decoded); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testEncodeUrlComponentsAndDecodeWithUtf8() { |
| 59 | + var original = "This is a test for URL-safe Base64 encoding+!"; |
| 60 | + |
| 61 | + var encodedUrl = Base64Util.encodeUrlComponents(original); |
| 62 | + assertNotNull(encodedUrl); |
| 63 | + assertNotEquals(original, encodedUrl); |
| 64 | + // URL-safe encoding should not contain '+' or '/' characters |
| 65 | + assertFalse(encodedUrl.contains("+")); |
| 66 | + assertFalse(encodedUrl.contains("/")); |
| 67 | + |
| 68 | + var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl); |
| 69 | + assertNotNull(decodedUrl); |
| 70 | + assertEquals(original, decodedUrl); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testEncodeUrlComponentsAndDecodeWithCharset() { |
| 75 | + var original = "测试 URL 安全编码"; // Unicode string |
| 76 | + var charset = StandardCharsets.UTF_8; |
| 77 | + |
| 78 | + var encodedUrl = Base64Util.encodeUrlComponents(original, charset); |
| 79 | + assertNotNull(encodedUrl); |
| 80 | + assertNotEquals(original, encodedUrl); |
| 81 | + |
| 82 | + var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl, charset); |
| 83 | + assertNotNull(decodedUrl); |
| 84 | + assertEquals(original, decodedUrl); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testEncodeAndDecodeEmptyString() { |
| 89 | + var original = ""; |
| 90 | + |
| 91 | + var encoded = Base64Util.encode(original); |
| 92 | + assertNotNull(encoded); |
| 93 | + assertEquals("", Base64Util.decode(encoded)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testEncodeAndDecodeNullSafety() { |
| 98 | + // Since Base64Util does not explicitly handle null, the test expects NPE if null is input |
| 99 | + assertThrows(NullPointerException.class, () -> Base64Util.encode(null)); |
| 100 | + assertThrows(NullPointerException.class, () -> Base64Util.decode(null)); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments