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 assertSupportedEncoding(final String name)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 testConstructor()44 public void testConstructor() { 45 new CharEncoding(); 46 } 47 48 @Test testMustBeSupportedJava1_3_1_and_above()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 testSupported()59 public void testSupported() { 60 assertTrue(CharEncoding.isSupported("UTF8")); 61 assertTrue(CharEncoding.isSupported("UTF-8")); 62 assertTrue(CharEncoding.isSupported("ASCII")); 63 } 64 65 @Test testNotSupported()66 public void testNotSupported() { 67 assertFalse(CharEncoding.isSupported(null)); 68 assertFalse(CharEncoding.isSupported("")); 69 assertFalse(CharEncoding.isSupported(" ")); 70 assertFalse(CharEncoding.isSupported("\t\r\n")); 71 assertFalse(CharEncoding.isSupported("DOESNOTEXIST")); 72 assertFalse(CharEncoding.isSupported("this is not a valid encoding name")); 73 } 74 75 @Test testStandardCharsetsEquality()76 public void testStandardCharsetsEquality() { 77 assertEquals(StandardCharsets.ISO_8859_1.name(), CharEncoding.ISO_8859_1); 78 assertEquals(StandardCharsets.US_ASCII.name(), CharEncoding.US_ASCII); 79 assertEquals(StandardCharsets.UTF_8.name(), CharEncoding.UTF_8); 80 assertEquals(StandardCharsets.UTF_16.name(), CharEncoding.UTF_16); 81 assertEquals(StandardCharsets.UTF_16BE.name(), CharEncoding.UTF_16BE); 82 assertEquals(StandardCharsets.UTF_16LE.name(), CharEncoding.UTF_16LE); 83 } 84 } 85