1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests CharEncoding.
27   * 
28   * @see CharEncoding
29   * @author Gary D. Gregory
30   * @version $Id: CharEncodingTest.java 437554 2006-08-28 06:21:41Z bayard $
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       * The class can be instantiated.
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          // In this test, I simply deleted the encodings from the 1.3.1 list.
80          // The Javadoc do not specify which encodings are required.
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          // In this test, I simply deleted the encodings from the 1.3.1 list.
94          // The Javadoc do not specify which encodings are required.
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 }