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