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