1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.nio.charset.StandardCharsets;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31
32
33 @SuppressWarnings("deprecation")
34 public class CharEncodingTest extends AbstractLangTest {
35
36 private void assertSupportedEncoding(final String name) {
37 assertTrue(CharEncoding.isSupported(name), "Encoding should be supported: " + name);
38 }
39
40
41
42
43 @Test
44 public void testConstructor() {
45 new CharEncoding();
46 }
47
48 @Test
49 public void testMustBeSupportedJava1_3_1_and_above() {
50 this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
51 this.assertSupportedEncoding(CharEncoding.US_ASCII);
52 this.assertSupportedEncoding(CharEncoding.UTF_16);
53 this.assertSupportedEncoding(CharEncoding.UTF_16BE);
54 this.assertSupportedEncoding(CharEncoding.UTF_16LE);
55 this.assertSupportedEncoding(CharEncoding.UTF_8);
56 }
57
58 @Test
59 public void testNotSupported() {
60 assertFalse(CharEncoding.isSupported(null));
61 assertFalse(CharEncoding.isSupported(""));
62 assertFalse(CharEncoding.isSupported(" "));
63 assertFalse(CharEncoding.isSupported("\t\r\n"));
64 assertFalse(CharEncoding.isSupported("DOESNOTEXIST"));
65 assertFalse(CharEncoding.isSupported("this is not a valid encoding name"));
66 }
67
68 @Test
69 public void testStandardCharsetsEquality() {
70 assertEquals(StandardCharsets.ISO_8859_1.name(), CharEncoding.ISO_8859_1);
71 assertEquals(StandardCharsets.US_ASCII.name(), CharEncoding.US_ASCII);
72 assertEquals(StandardCharsets.UTF_8.name(), CharEncoding.UTF_8);
73 assertEquals(StandardCharsets.UTF_16.name(), CharEncoding.UTF_16);
74 assertEquals(StandardCharsets.UTF_16BE.name(), CharEncoding.UTF_16BE);
75 assertEquals(StandardCharsets.UTF_16LE.name(), CharEncoding.UTF_16LE);
76 }
77
78 @Test
79 public void testSupported() {
80 assertTrue(CharEncoding.isSupported("UTF8"));
81 assertTrue(CharEncoding.isSupported("UTF-8"));
82 assertTrue(CharEncoding.isSupported("ASCII"));
83 }
84 }