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 package org.apache.commons.io; 18 19 import static org.junit.jupiter.api.Assertions.assertArrayEquals; 20 import static org.junit.jupiter.api.Assertions.assertEquals; 21 import static org.junit.jupiter.api.Assertions.assertNotEquals; 22 import static org.junit.jupiter.api.Assertions.assertNotNull; 23 import static org.junit.jupiter.api.Assertions.assertThrows; 24 25 import java.nio.charset.Charset; 26 27 import org.junit.jupiter.api.Test; 28 29 /** 30 * Test for {@link ByteOrderMark}. 31 */ 32 public class ByteOrderMarkTest { 33 34 private static final ByteOrderMark TEST_BOM_1 = new ByteOrderMark("test1", 1); 35 private static final ByteOrderMark TEST_BOM_2 = new ByteOrderMark("test2", 1, 2); 36 private static final ByteOrderMark TEST_BOM_3 = new ByteOrderMark("test3", 1, 2, 3); 37 38 /** Tests that {@link ByteOrderMark#getCharsetName()} can be loaded as a {@link java.nio.charset.Charset} as advertised. */ 39 @Test testConstantCharsetNames()40 public void testConstantCharsetNames() { 41 assertNotNull(Charset.forName(ByteOrderMark.UTF_8.getCharsetName())); 42 assertNotNull(Charset.forName(ByteOrderMark.UTF_16BE.getCharsetName())); 43 assertNotNull(Charset.forName(ByteOrderMark.UTF_16LE.getCharsetName())); 44 assertNotNull(Charset.forName(ByteOrderMark.UTF_32BE.getCharsetName())); 45 assertNotNull(Charset.forName(ByteOrderMark.UTF_32LE.getCharsetName())); 46 } 47 48 /** Tests Exceptions */ 49 @Test testConstructorExceptions()50 public void testConstructorExceptions() { 51 assertThrows(NullPointerException.class, () -> new ByteOrderMark(null, 1, 2, 3)); 52 assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("", 1, 2, 3)); 53 assertThrows(NullPointerException.class, () -> new ByteOrderMark("a", (int[]) null)); 54 assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("b")); 55 } 56 57 /** Tests {@link ByteOrderMark#equals(Object)} */ 58 @SuppressWarnings("EqualsWithItself") 59 @Test testEquals()60 public void testEquals() { 61 assertEquals(ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16BE); 62 assertEquals(ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16LE); 63 assertEquals(ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32BE); 64 assertEquals(ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32LE); 65 assertEquals(ByteOrderMark.UTF_8, ByteOrderMark.UTF_8); 66 67 assertNotEquals(ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE); 68 assertNotEquals(ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE); 69 assertNotEquals(ByteOrderMark.UTF_8, ByteOrderMark.UTF_32BE); 70 assertNotEquals(ByteOrderMark.UTF_8, ByteOrderMark.UTF_32LE); 71 72 assertEquals(TEST_BOM_1, TEST_BOM_1, "test1 equals"); 73 assertEquals(TEST_BOM_2, TEST_BOM_2, "test2 equals"); 74 assertEquals(TEST_BOM_3, TEST_BOM_3, "test3 equals"); 75 76 assertNotEquals(TEST_BOM_1, new Object(), "Object not equal"); 77 assertNotEquals(TEST_BOM_1, new ByteOrderMark("1a", 2), "test1-1 not equal"); 78 assertNotEquals(TEST_BOM_1, new ByteOrderMark("1b", 1, 2), "test1-2 not test2"); 79 assertNotEquals(TEST_BOM_2, new ByteOrderMark("2", 1, 1), "test2 not equal"); 80 assertNotEquals(TEST_BOM_3, new ByteOrderMark("3", 1, 2, 4), "test3 not equal"); 81 } 82 83 /** Tests {@link ByteOrderMark#getBytes()} */ 84 @Test testGetBytes()85 public void testGetBytes() { 86 assertArrayEquals(TEST_BOM_1.getBytes(), new byte[] { (byte) 1 }, "test1 bytes"); 87 TEST_BOM_1.getBytes()[0] = 2; 88 assertArrayEquals(TEST_BOM_1.getBytes(), new byte[] { (byte) 1 }, "test1 bytes"); 89 assertArrayEquals(TEST_BOM_2.getBytes(), new byte[] { (byte) 1, (byte) 2 }, "test1 bytes"); 90 assertArrayEquals(TEST_BOM_3.getBytes(), new byte[] { (byte) 1, (byte) 2, (byte) 3 }, "test1 bytes"); 91 } 92 93 /** Tests {@link ByteOrderMark#getCharsetName()} */ 94 @Test testGetCharsetName()95 public void testGetCharsetName() { 96 assertEquals("test1", TEST_BOM_1.getCharsetName(), "test1 name"); 97 assertEquals("test2", TEST_BOM_2.getCharsetName(), "test2 name"); 98 assertEquals("test3", TEST_BOM_3.getCharsetName(), "test3 name"); 99 } 100 101 /** Tests {@link ByteOrderMark#get(int)} */ 102 @Test testGetInt()103 public void testGetInt() { 104 assertEquals(1, TEST_BOM_1.get(0), "test1 get(0)"); 105 assertEquals(1, TEST_BOM_2.get(0), "test2 get(0)"); 106 assertEquals(2, TEST_BOM_2.get(1), "test2 get(1)"); 107 assertEquals(1, TEST_BOM_3.get(0), "test3 get(0)"); 108 assertEquals(2, TEST_BOM_3.get(1), "test3 get(1)"); 109 assertEquals(3, TEST_BOM_3.get(2), "test3 get(2)"); 110 } 111 112 /** Tests {@link ByteOrderMark#hashCode()} */ 113 @Test testHashCode()114 public void testHashCode() { 115 final int bomClassHash = ByteOrderMark.class.hashCode(); 116 assertEquals(bomClassHash + 1, TEST_BOM_1.hashCode(), "hash test1 "); 117 assertEquals(bomClassHash + 3, TEST_BOM_2.hashCode(), "hash test2 "); 118 assertEquals(bomClassHash + 6, TEST_BOM_3.hashCode(), "hash test3 "); 119 } 120 121 /** Tests {@link ByteOrderMark#length()} */ 122 @Test testLength()123 public void testLength() { 124 assertEquals(1, TEST_BOM_1.length(), "test1 length"); 125 assertEquals(2, TEST_BOM_2.length(), "test2 length"); 126 assertEquals(3, TEST_BOM_3.length(), "test3 length"); 127 } 128 129 /** Tests {@link ByteOrderMark#toString()} */ 130 @Test testToString()131 public void testToString() { 132 assertEquals("ByteOrderMark[test1: 0x1]", TEST_BOM_1.toString(), "test1 "); 133 assertEquals("ByteOrderMark[test2: 0x1,0x2]", TEST_BOM_2.toString(), "test2 "); 134 assertEquals("ByteOrderMark[test3: 0x1,0x2,0x3]", TEST_BOM_3.toString(), "test3 "); 135 } 136 } 137