View Javadoc
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.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   * Tests CharEncoding.
30   *
31   * @see CharEncoding
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       * The class can be instantiated.
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  }