KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > CharEncodingTest


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.lang;
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import junit.textui.TestRunner;
23
24 /**
25  * Tests CharEncoding.
26  *
27  * @see CharEncoding
28  * @author Gary D. Gregory
29  * @version $Id: CharEncodingTest.java 161244 2005-04-14 06:16:36Z ggregory $
30  */

31 public class CharEncodingTest extends TestCase {
32
33     public static void main(String JavaDoc[] args) {
34         TestRunner.run(suite());
35     }
36
37     public static Test suite() {
38         TestSuite suite = new TestSuite(CharEncodingTest.class);
39         suite.setName("CharEncoding Tests");
40         return suite;
41     }
42
43     private void assertSupportedEncoding(String JavaDoc name) {
44         assertTrue("Encoding should be supported: " + name, CharEncoding.isSupported(name));
45     }
46
47     public void testMustBeSupportedJava1_3_1() {
48         if (SystemUtils.isJavaVersionAtLeast(1.3f)) {
49             this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
50             this.assertSupportedEncoding(CharEncoding.US_ASCII);
51             this.assertSupportedEncoding(CharEncoding.UTF_16);
52             this.assertSupportedEncoding(CharEncoding.UTF_16BE);
53             this.assertSupportedEncoding(CharEncoding.UTF_16LE);
54             this.assertSupportedEncoding(CharEncoding.UTF_8);
55         } else {
56             this.warn("Java 1.3 tests not run since the current version is " + SystemUtils.JAVA_VERSION);
57         }
58     }
59
60     public void testNotSupported() {
61         assertFalse(CharEncoding.isSupported(null));
62         assertFalse(CharEncoding.isSupported(""));
63         assertFalse(CharEncoding.isSupported(" "));
64         assertFalse(CharEncoding.isSupported("\t\r\n"));
65         assertFalse(CharEncoding.isSupported("DOESNOTEXIST"));
66         assertFalse(CharEncoding.isSupported("this is not a valid encoding name"));
67     }
68
69     public void testWorksOnJava1_1_8() {
70         //
71
// In this test, I simply deleted the encodings from the 1.3.1 list.
72
// The Javadoc do not specify which encodings are required.
73
//
74
if (SystemUtils.isJavaVersionAtLeast(1.1f)) {
75             this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
76             this.assertSupportedEncoding(CharEncoding.US_ASCII);
77             this.assertSupportedEncoding(CharEncoding.UTF_8);
78         } else {
79             this.warn("Java 1.1 tests not run since the current version is " + SystemUtils.JAVA_VERSION);
80         }
81     }
82
83     public void testWorksOnJava1_2_2() {
84         //
85
// In this test, I simply deleted the encodings from the 1.3.1 list.
86
// The Javadoc do not specify which encodings are required.
87
//
88
if (SystemUtils.isJavaVersionAtLeast(1.2f)) {
89             this.assertSupportedEncoding(CharEncoding.ISO_8859_1);
90             this.assertSupportedEncoding(CharEncoding.US_ASCII);
91             this.assertSupportedEncoding(CharEncoding.UTF_8);
92         } else {
93             this.warn("Java 1.2 tests not run since the current version is " + SystemUtils.JAVA_VERSION);
94         }
95     }
96
97     void warn(String JavaDoc msg) {
98         System.err.println(msg);
99     }
100 }
101
Popular Tags