View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.yaml.snakeyaml.reader;
    18
    19  import java.io.ByteArrayInputStream;
    20  import java.io.File;
    21  import java.io.FileInputStream;
    22  import java.io.IOException;
    23  import java.io.InputStream;
    24  import java.io.Reader;
    25  import java.io.StringReader;
    26  import java.nio.charset.Charset;
    27
    28  import junit.framework.TestCase;
    29
    30  public class ReaderBomTest extends TestCase {
    31
    32      public void testReader() throws IOException {
    33          Reader input = new StringReader("test");
    34          StreamReader reader = new StreamReader(input);
    35          assertEquals('t', reader.peek());
    36          reader.forward(1);
    37          assertEquals('e', reader.peek());
    38          reader.forward(1);
    39          assertEquals('s', reader.peek());
    40          reader.forward(1);
    41          assertEquals('t', reader.peek());
    42          reader.forward(1);
    43          assertEquals('\u0000', reader.peek());
    44      }
    45
    46      public void testNoBom() throws IOException {
    47          byte[] data = "test".getBytes("UTF-8");
    48          ByteArrayInputStream input = new ByteArrayInputStream(data);
    49          Reader r = new UnicodeReader(input);
    50          StreamReader reader = new StreamReader(r);
    51          assertEquals('t', reader.peek());
    52          assertEquals(Charset.forName("UTF-8"), reader.getEncoding());
    53          reader.forward(1);
    54          assertEquals('e', reader.peek());
    55          reader.forward(1);
    56          assertEquals('s', reader.peek());
    57          reader.forward(1);
    58          assertEquals('t', reader.peek());
    59          reader.forward(1);
    60          assertEquals('\u0000', reader.peek());
    61          r.close();
    62      }
    63
    64      public void testUtf8Bom() throws IOException {
    65          File file = new File("src/test/resources/reader/utf-8.txt");
    66          assertTrue("Test file not found: " + file.getAbsolutePath(), file.exists());
    67          InputStream input = new FileInputStream(file);
    68          StreamReader reader = new StreamReader(new UnicodeReader(input));
    69          assertEquals('t', reader.peek());
    70          reader.forward(1);
    71          assertEquals('e', reader.peek());
    72          reader.forward(1);
    73          assertEquals('s', reader.peek());
    74          reader.forward(1);
    75          assertEquals('t', reader.peek());
    76          reader.forward(1);
    77          assertEquals('\u0000', reader.peek());
    78          assertEquals(Charset.forName("UTF-8"), reader.getEncoding());
    79          input.close();
    80      }
    81
    82      public void testUnicodeLeBom() throws IOException {
    83          File file = new File("src/test/resources/reader/unicode-16le.txt");
    84          assertTrue("Test file not found: " + file.getAbsolutePath(), file.exists());
    85          InputStream input = new FileInputStream(file);
    86          StreamReader reader = new StreamReader(new UnicodeReader(input));
    87          assertEquals('t', reader.peek());
    88          reader.forward(1);
    89          assertEquals('e', reader.peek());
    90          reader.forward(1);
    91          assertEquals('s', reader.peek());
    92          reader.forward(1);
    93          assertEquals('t', reader.peek());
    94          reader.forward(1);
    95          assertEquals('\u0000', reader.peek());
    96          assertEquals(Charset.forName("UTF-16LE"), reader.getEncoding());
    97          input.close();
    98      }
    99
    100     public void testUnicodeBeBom() throws IOException {
    101         File file = new File("src/test/resources/reader/unicode-16be.txt");
    102         assertTrue("Test file not found: " + file.getAbsolutePath(), file.exists());
    103         InputStream input = new FileInputStream(file);
    104         StreamReader reader = new StreamReader(new UnicodeReader(input));
    105         assertEquals('t', reader.peek());
    106         reader.forward(1);
    107         assertEquals('e', reader.peek());
    108         reader.forward(1);
    109         assertEquals('s', reader.peek());
    110         reader.forward(1);
    111         assertEquals('t', reader.peek());
    112         reader.forward(1);
    113         assertEquals('\u0000', reader.peek());
    114         assertEquals(Charset.forName("UTF-16BE"), reader.getEncoding());
    115         input.close();
    116     }
    117 }