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