BAEL-3927 : Encode a String to UTF-8 in Java (#8954)
* BAEL-3927 : Encode a String to UTF-8 in Java * Update core-java-modules/core-java-string-operations-2/src/test/java/com/baeldung/encode/StringEncodeUnitTest.java Co-Authored-By: KevinGilmore <kpg102@gmail.com> * BAEL-3927 : Applying suggested adjustments * BAEL-3927 : Simplifying the code snippets * BAEL-3927 : Coding styles adjustements * BAEL-3927 : Test adjustements to fit changes in article Co-authored-by: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
parent
b31b84d475
commit
35f1a3eba5
|
@ -0,0 +1,73 @@
|
||||||
|
package com.baeldung.encodetoutf8;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.binary.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class StringEncodeUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGermanAsciiString_whenComparing_thenCompareNotEquals() {
|
||||||
|
String germanString = "Entwickeln Sie mit Vergnügen";
|
||||||
|
byte[] germanBytes = germanString.getBytes();
|
||||||
|
|
||||||
|
String asciiEncodedString = new String(germanBytes, StandardCharsets.US_ASCII);
|
||||||
|
|
||||||
|
assertNotEquals(germanString, asciiEncodedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsAsciiString_whenComparing_thenCompareNotEquals() {
|
||||||
|
String englishString = "Develop with pleasure";
|
||||||
|
byte[] englishBytes = englishString.getBytes();
|
||||||
|
|
||||||
|
String asciiEncondedEnglishString = new String(englishBytes, StandardCharsets.US_ASCII);
|
||||||
|
|
||||||
|
assertEquals(englishString, asciiEncondedEnglishString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ApacheCommonsCodecEncode
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void givenSomeUnencodedString_whenApacheCommonsCodecEncode_thenCompareEquals() {
|
||||||
|
String rawString = "Entwickeln Sie mit Vergnügen";
|
||||||
|
byte[] bytes = StringUtils.getBytesUtf8(rawString);
|
||||||
|
|
||||||
|
String utf8EncodedString = StringUtils.newStringUtf8(bytes);
|
||||||
|
|
||||||
|
assertEquals(rawString, utf8EncodedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* CoreJavaEncode
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void givenSomeUnencodedString_whenCoreJavaEncode_thenCompareEquals() {
|
||||||
|
String rawString = "Entwickeln Sie mit Vergnügen";
|
||||||
|
byte[] bytes = rawString.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
assertEquals(rawString, utf8EncodedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Java7StandardCharsetsEncode
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void givenSomeUnencodedString_whenJava7StandardCharsetsEncode_thenCompareEquals() {
|
||||||
|
String rawString = "Entwickeln Sie mit Vergnügen";
|
||||||
|
ByteBuffer buffer = StandardCharsets.UTF_8.encode(rawString);
|
||||||
|
|
||||||
|
String utf8EncodedString = StandardCharsets.UTF_8.decode(buffer)
|
||||||
|
.toString();
|
||||||
|
|
||||||
|
assertEquals(rawString, utf8EncodedString);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue