001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.lang;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024:
025: /**
026: * Tests CharEncoding.
027: *
028: * @see CharEncoding
029: * @author Gary D. Gregory
030: * @version $Id: CharEncodingTest.java 437554 2006-08-28 06:21:41Z bayard $
031: */
032: public class CharEncodingTest extends TestCase {
033:
034: public static void main(String[] args) {
035: TestRunner.run(suite());
036: }
037:
038: public static Test suite() {
039: TestSuite suite = new TestSuite(CharEncodingTest.class);
040: suite.setName("CharEncoding Tests");
041: return suite;
042: }
043:
044: private void assertSupportedEncoding(String name) {
045: assertTrue("Encoding should be supported: " + name,
046: CharEncoding.isSupported(name));
047: }
048:
049: /**
050: * The class can be instantiated.
051: */
052: public void testConstructor() {
053: new CharEncoding();
054: }
055:
056: public void testMustBeSupportedJava1_3_1() {
057: if (SystemUtils.isJavaVersionAtLeast(1.3f)) {
058: this .assertSupportedEncoding(CharEncoding.ISO_8859_1);
059: this .assertSupportedEncoding(CharEncoding.US_ASCII);
060: this .assertSupportedEncoding(CharEncoding.UTF_16);
061: this .assertSupportedEncoding(CharEncoding.UTF_16BE);
062: this .assertSupportedEncoding(CharEncoding.UTF_16LE);
063: this .assertSupportedEncoding(CharEncoding.UTF_8);
064: } else {
065: this
066: .warn("Java 1.3 tests not run since the current version is "
067: + SystemUtils.JAVA_VERSION);
068: }
069: }
070:
071: public void testNotSupported() {
072: assertFalse(CharEncoding.isSupported(null));
073: assertFalse(CharEncoding.isSupported(""));
074: assertFalse(CharEncoding.isSupported(" "));
075: assertFalse(CharEncoding.isSupported("\t\r\n"));
076: assertFalse(CharEncoding.isSupported("DOESNOTEXIST"));
077: assertFalse(CharEncoding
078: .isSupported("this is not a valid encoding name"));
079: }
080:
081: public void testWorksOnJava1_1_8() {
082: //
083: // In this test, I simply deleted the encodings from the 1.3.1 list.
084: // The Javadoc do not specify which encodings are required.
085: //
086: if (SystemUtils.isJavaVersionAtLeast(1.1f)) {
087: this .assertSupportedEncoding(CharEncoding.ISO_8859_1);
088: this .assertSupportedEncoding(CharEncoding.US_ASCII);
089: this .assertSupportedEncoding(CharEncoding.UTF_8);
090: } else {
091: this
092: .warn("Java 1.1 tests not run since the current version is "
093: + SystemUtils.JAVA_VERSION);
094: }
095: }
096:
097: public void testWorksOnJava1_2_2() {
098: //
099: // In this test, I simply deleted the encodings from the 1.3.1 list.
100: // The Javadoc do not specify which encodings are required.
101: //
102: if (SystemUtils.isJavaVersionAtLeast(1.2f)) {
103: this .assertSupportedEncoding(CharEncoding.ISO_8859_1);
104: this .assertSupportedEncoding(CharEncoding.US_ASCII);
105: this .assertSupportedEncoding(CharEncoding.UTF_8);
106: } else {
107: this
108: .warn("Java 1.2 tests not run since the current version is "
109: + SystemUtils.JAVA_VERSION);
110: }
111: }
112:
113: void warn(String msg) {
114: System.err.println(msg);
115: }
116: }
|