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