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