View Javadoc

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