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.io;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.nio.charset.Charset;
24  import java.nio.charset.StandardCharsets;
25  import java.util.Set;
26  import java.util.SortedMap;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link Charsets}.
32   */
33  @SuppressWarnings("deprecation") // testing deprecated code
34  public class CharsetsTest {
35  
36      /**
37       * For parameterized tests.
38       */
39      public static final String AVAIL_CHARSETS = "org.apache.commons.io.CharsetsTest#availableCharsetsKeySet";
40      /**
41       * For parameterized tests.
42       */
43      public static final String REQUIRED_CHARSETS = "org.apache.commons.io.CharsetsTest#getRequiredCharsetNames";
44  
45      /**
46       * For parameterized tests.
47       *
48       * @return {@code Charset.availableCharsets().keySet()}.
49       */
50      public static Set<String> availableCharsetsKeySet() {
51          return Charset.availableCharsets().keySet();
52      }
53  
54      /**
55       * For parameterized tests.
56       *
57       * @return {@code Charset.requiredCharsets().keySet()}.
58       */
59      public static Set<String> getRequiredCharsetNames() {
60          return Charsets.requiredCharsets().keySet();
61      }
62  
63      @Test
64      public void testIso8859_1() {
65          assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
66      }
67  
68      @Test
69      public void testRequiredCharsets() {
70          final SortedMap<String, Charset> requiredCharsets = Charsets.requiredCharsets();
71          // test for what we expect to be there as of Java 6
72          // Make sure the object at the given key is the right one
73          assertEquals(requiredCharsets.get("US-ASCII").name(), "US-ASCII");
74          assertEquals(requiredCharsets.get("ISO-8859-1").name(), "ISO-8859-1");
75          assertEquals(requiredCharsets.get("UTF-8").name(), "UTF-8");
76          assertEquals(requiredCharsets.get("UTF-16").name(), "UTF-16");
77          assertEquals(requiredCharsets.get("UTF-16BE").name(), "UTF-16BE");
78          assertEquals(requiredCharsets.get("UTF-16LE").name(), "UTF-16LE");
79      }
80  
81      @Test
82      public void testToCharset_String() {
83          assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
84          assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
85          assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
86          assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8));
87      }
88  
89      @Test
90      public void testToCharset_String_Charset() {
91          assertNull(Charsets.toCharset((String) null, null));
92          assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null, Charset.defaultCharset()));
93          assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null, Charset.defaultCharset()));
94          assertNull(Charsets.toCharset((Charset) null, null));
95          assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset(), Charset.defaultCharset()));
96          assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8, Charset.defaultCharset()));
97          assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8, null));
98      }
99  
100     @Test
101     public void testUsAscii() {
102         assertEquals(StandardCharsets.US_ASCII.name(), Charsets.US_ASCII.name());
103     }
104 
105     @Test
106     public void testUtf16() {
107         assertEquals(StandardCharsets.UTF_16.name(), Charsets.UTF_16.name());
108     }
109 
110     @Test
111     public void testUtf16Be() {
112         assertEquals(StandardCharsets.UTF_16BE.name(), Charsets.UTF_16BE.name());
113     }
114 
115     @Test
116     public void testUtf16Le() {
117         assertEquals(StandardCharsets.UTF_16LE.name(), Charsets.UTF_16LE.name());
118     }
119 
120     @Test
121     public void testUtf8() {
122         assertEquals(StandardCharsets.UTF_8.name(), Charsets.UTF_8.name());
123     }
124 
125 }