1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang;
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import junit.textui.TestRunner;
24
25
26
27
28
29
30
31
32 public class CharEncodingTest extends TestCase {
33
34 public static void main(String[] args) {
35 TestRunner.run(suite());
36 }
37
38 public static Test suite() {
39 TestSuite suite = new TestSuite(CharEncodingTest.class);
40 suite.setName("CharEncoding Tests");
41 return suite;
42 }
43
44 private void assertSupportedEncoding(String name) {
45 assertTrue("Encoding should be supported: " + name, CharEncoding.isSupported(name));
46 }
47
48
49
50
51 public void testConstructor() {
52 new CharEncoding();
53 }
54
55 public void testMustBeSupportedJava1_3_1() {
56 if (SystemUtils.isJavaVersionAtLeast(1.3f)) {
57 this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
58 this.assertSupportedEncoding(CharEncoding.US_ASCII);
59 this.assertSupportedEncoding(CharEncoding.UTF_16);
60 this.assertSupportedEncoding(CharEncoding.UTF_16BE);
61 this.assertSupportedEncoding(CharEncoding.UTF_16LE);
62 this.assertSupportedEncoding(CharEncoding.UTF_8);
63 } else {
64 this.warn("Java 1.3 tests not run since the current version is " + SystemUtils.JAVA_VERSION);
65 }
66 }
67
68 public void testNotSupported() {
69 assertFalse(CharEncoding.isSupported(null));
70 assertFalse(CharEncoding.isSupported(""));
71 assertFalse(CharEncoding.isSupported(" "));
72 assertFalse(CharEncoding.isSupported("\t\r\n"));
73 assertFalse(CharEncoding.isSupported("DOESNOTEXIST"));
74 assertFalse(CharEncoding.isSupported("this is not a valid encoding name"));
75 }
76
77 public void testWorksOnJava1_1_8() {
78
79
80
81
82 if (SystemUtils.isJavaVersionAtLeast(1.1f)) {
83 this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
84 this.assertSupportedEncoding(CharEncoding.US_ASCII);
85 this.assertSupportedEncoding(CharEncoding.UTF_8);
86 } else {
87 this.warn("Java 1.1 tests not run since the current version is " + SystemUtils.JAVA_VERSION);
88 }
89 }
90
91 public void testWorksOnJava1_2_2() {
92
93
94
95
96 if (SystemUtils.isJavaVersionAtLeast(1.2f)) {
97 this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
98 this.assertSupportedEncoding(CharEncoding.US_ASCII);
99 this.assertSupportedEncoding(CharEncoding.UTF_8);
100 } else {
101 this.warn("Java 1.2 tests not run since the current version is " + SystemUtils.JAVA_VERSION);
102 }
103 }
104
105 void warn(String msg) {
106 System.err.println(msg);
107 }
108 }