This commit is related to BAEL-6404 (#16201)

This commit aims to add a test class "UTF8ToISOUnitTest.java" To convert UTF-8 to ISO-8859-1 in Java.
This commit is contained in:
MohamedHelmyKassab 2024-03-22 20:15:31 +02:00 committed by GitHub
parent da0dec8e13
commit 663d1831da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.UTF8ToISO;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertArrayEquals;
public class UTF8ToISOUnitTest {
String string = "âabcd";
byte[] expectedBytes = new byte[]{(byte) 0xE2, 0x61, 0x62, 0x63, 0x64};
@Test
public void givenUtf8String_whenUsingGetByte_thenIsoBytesShouldBeEqual() {
byte[] iso88591bytes = string.getBytes(StandardCharsets.ISO_8859_1);
assertArrayEquals(expectedBytes, iso88591bytes);
}
@Test
public void givenString_whenUsingByteBufferCharBufferConvertToIso_thenBytesShouldBeEqual() {
ByteBuffer inputBuffer = ByteBuffer.wrap(string.getBytes(StandardCharsets.UTF_8));
CharBuffer data = StandardCharsets.UTF_8.decode(inputBuffer);
ByteBuffer outputBuffer = StandardCharsets.ISO_8859_1.encode(data);
byte[] outputData = new byte[outputBuffer.remaining()];
outputBuffer.get(outputData);
assertArrayEquals(expectedBytes, outputData);
}
}