|
| 1 | +package com.baeldung.commons.convertunicode; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | + |
| 7 | +public class UnicodeConverterUnitTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + public void whenInputHaveUnicodeSequences_ThenDecode() { |
| 11 | + String encodedString = "\\u0048\\u0065\\u006C\\u006C\\u006F World"; |
| 12 | + String expectedDecodedString = "Hello World"; |
| 13 | + assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString)); |
| 14 | + assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString)); |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + public void whenInputHaveNoUnicodeSequences_ThenDoNothing() { |
| 19 | + String inputString = "Hello World"; |
| 20 | + assertEquals(inputString, UnicodeConverterUtil.decodeWithApacheCommons(inputString)); |
| 21 | + assertEquals(inputString, UnicodeConverterUtil.decodeWithPlainJava(inputString)); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void whenInputHaveUnicodeSequencesInMiddle_ThenDecode() { |
| 26 | + String encodedString = "This is a test \\u0069\\u006E the middle."; |
| 27 | + String expectedDecodedString = "This is a test in the middle."; |
| 28 | + assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString)); |
| 29 | + assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString)); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void whenInputHaveMultipleUnicodeSequences_ThenDecode() { |
| 34 | + String encodedString = "Unicode: \\u0048\\u0065\\u006C\\u006C\\u006F \\u0057\\u006F\\u0072\\u006C\\u0064"; |
| 35 | + String expectedDecodedString = "Unicode: Hello World"; |
| 36 | + assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithApacheCommons(encodedString)); |
| 37 | + assertEquals(expectedDecodedString, UnicodeConverterUtil.decodeWithPlainJava(encodedString)); |
| 38 | + } |
| 39 | +} |
0 commit comments