|
| 1 | +package com.baeldung.encoding; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +public class CharacterEncodingExamplesUnitTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void givenTextFile_whenCalledWithEncodingASCII_thenProduceIncorrectResult() throws IOException { |
| 12 | + Assert.assertEquals( |
| 13 | + CharacterEncodingExamples.readFile( |
| 14 | + "src/test/resources/encoding.txt", "US-ASCII"), |
| 15 | + "The fa��ade pattern is a software-design pattern commonly used with object-oriented programming."); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void givenTextFile_whenCalledWithEncodingUTF8_thenProduceCorrectResult() throws IOException { |
| 20 | + Assert.assertEquals( |
| 21 | + CharacterEncodingExamples.readFile( |
| 22 | + "src/test/resources/encoding.txt", "UTF-8"), |
| 23 | + "The façade pattern is a software-design pattern commonly used with object-oriented programming."); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void givenCharacterA_whenConvertedtoBinaryWithEncodingASCII_thenProduceResult() throws IOException { |
| 28 | + Assert.assertEquals( |
| 29 | + CharacterEncodingExamples.convertToBinary("A", "US-ASCII"), |
| 30 | + "1000001 "); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenCharacterA_whenConvertedtoBinaryWithEncodingUTF8_thenProduceResult() throws IOException { |
| 35 | + Assert.assertEquals( |
| 36 | + CharacterEncodingExamples.convertToBinary("A", "UTF-8"), |
| 37 | + "1000001 "); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenCharacterCh_whenConvertedtoBinaryWithEncodingBig5_thenProduceResult() throws IOException { |
| 42 | + Assert.assertEquals( |
| 43 | + CharacterEncodingExamples.convertToBinary("語", "Big5"), |
| 44 | + "10111011 1111001 "); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF8_thenProduceResult() throws IOException { |
| 49 | + Assert.assertEquals( |
| 50 | + CharacterEncodingExamples.convertToBinary("語", "UTF-8"), |
| 51 | + "11101000 10101010 10011110 "); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF32_thenProduceResult() throws IOException { |
| 56 | + Assert.assertEquals( |
| 57 | + CharacterEncodingExamples.convertToBinary("語", "UTF-32"), |
| 58 | + "0 0 10001010 10011110 "); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments