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    *      https://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.codec;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.nio.charset.Charset;
23  import java.nio.charset.StandardCharsets;
24  import java.util.Collection;
25  import java.util.SortedSet;
26  import java.util.TreeSet;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Sanity checks for {@link Charsets}.
32   */
33  class CharsetsTest {
34  
35      private static final TreeSet<String> AVAILABLE_CHARSET_NAMES = new TreeSet<>(Charset.availableCharsets().keySet());
36  
37      public static SortedSet<String> getAvailableCharsetNames() {
38          return AVAILABLE_CHARSET_NAMES;
39      }
40  
41      public static Collection<Charset> getRequiredCharsets() {
42          return org.apache.commons.io.Charsets.requiredCharsets().values();
43      }
44  
45      @Test
46      void testDeprecatedConstructor() {
47          new Charsets();
48      }
49  
50      @SuppressWarnings("deprecation")
51      @Test
52      void testIso8859_1() {
53          assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
54      }
55  
56      @Test
57      void testToCharset() {
58          assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
59          assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
60          assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
61          assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8));
62      }
63  
64      @SuppressWarnings("deprecation")
65      @Test
66      void testUsAscii() {
67          assertEquals(StandardCharsets.US_ASCII.name(), Charsets.US_ASCII.name());
68      }
69  
70      @SuppressWarnings("deprecation")
71      @Test
72      void testUtf16() {
73          assertEquals(StandardCharsets.UTF_16.name(), Charsets.UTF_16.name());
74      }
75  
76      @SuppressWarnings("deprecation")
77      @Test
78      void testUtf16Be() {
79          assertEquals(StandardCharsets.UTF_16BE.name(), Charsets.UTF_16BE.name());
80      }
81  
82      @SuppressWarnings("deprecation")
83      @Test
84      void testUtf16Le() {
85          assertEquals(StandardCharsets.UTF_16LE.name(), Charsets.UTF_16LE.name());
86      }
87  
88      @SuppressWarnings("deprecation")
89      @Test
90      void testUtf8() {
91          assertEquals(StandardCharsets.UTF_8.name(), Charsets.UTF_8.name());
92      }
93  
94  }