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.util.regex.Matcher;
    20
    21  import junit.framework.TestCase;
    22
    23  public class ReaderStringTest extends TestCase {
    24
    25      public void testCheckPrintable() {
    26          StreamReader reader = new StreamReader("test");
    27          reader.checkPrintable("test");
    28          Matcher matcher = StreamReader.NON_PRINTABLE.matcher("test");
    29          assertFalse(matcher.find());
    30
    31          try {
    32              reader.checkPrintable("test".toCharArray(), 0, 4);
    33          } catch (ReaderException e) {
    34              fail();
    35          }
    36
    37      }
    38
    39      public void testCheckNonPrintable() {
    40          Matcher matcher = StreamReader.NON_PRINTABLE.matcher("test\u0005 fail");
    41          assertTrue(matcher.find());
    42          try {
    43              new StreamReader("test\u0005 fail");
    44              fail("Non printable Unicode characters must not be accepted.");
    45          } catch (ReaderException e) {
    46              assertEquals(
    47                      "unacceptable character '' (0x5) special characters are not allowed\nin \"<string>\", position 4",
    48                      e.toString());
    49          }
    50      }
    51
    52      /**
    53       * test that regular expression and array check work the same
    54       */
    55      public void testCheckAll() {
    56          StreamReader streamReader = new StreamReader("");
    57          for (char i = 0; i < 256 * 256 - 1; i++) {
    58              char[] chars = new char[1];
    59              chars[0] = i;
    60              String str = new String(chars);
    61              Matcher matcher = StreamReader.NON_PRINTABLE.matcher(str);
    62              boolean regularExpressionResult = !matcher.find();
    63
    64              boolean charsArrayResult = true;
    65              try {
    66                  streamReader.checkPrintable(chars, 0, 1);
    67              } catch (Exception e) {
    68                  String error = e.getMessage();
    69                  assertTrue(
    70                          error,
    71                          error.startsWith("unacceptable character")
    72                                  || error.equals("special characters are not allowed"));
    73                  charsArrayResult = false;
    74              }
    75              assertEquals("Failed for #" + i, regularExpressionResult, charsArrayResult);
    76          }
    77      }
    78
    79      public void testForward() {
    80          StreamReader reader = new StreamReader("test");
    81          while (reader.peek() != '\u0000') {
    82              reader.forward(1);
    83          }
    84          reader = new StreamReader("test");
    85          assertEquals('t', reader.peek());
    86          reader.forward(1);
    87          assertEquals('e', reader.peek());
    88          reader.forward(1);
    89          assertEquals('s', reader.peek());
    90          reader.forward(1);
    91          assertEquals('t', reader.peek());
    92          reader.forward(1);
    93          assertEquals('\u0000', reader.peek());
    94      }
    95
    96      public void testPeekInt() {
    97          StreamReader reader = new StreamReader("test");
    98          assertEquals('t', reader.peek(0));
    99          assertEquals('e', reader.peek(1));
    100         assertEquals('s', reader.peek(2));
    101         assertEquals('t', reader.peek(3));
    102         reader.forward(1);
    103         assertEquals('e', reader.peek(0));
    104         assertEquals('s', reader.peek(1));
    105         assertEquals('t', reader.peek(2));
    106     }
    107
    108 }