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