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.issues.issue68;
    18
    19  import java.io.FileInputStream;
    20  import java.io.FileNotFoundException;
    21  import java.io.IOException;
    22  import java.io.InputStream;
    23  import java.io.InputStreamReader;
    24  import java.io.UnsupportedEncodingException;
    25  import java.util.Map;
    26
    27  import junit.framework.TestCase;
    28
    29  import org.yaml.snakeyaml.Yaml;
    30  import org.yaml.snakeyaml.YamlDocument;
    31
    32  public class NonAsciiCharacterTest extends TestCase {
    33
    34      @SuppressWarnings("unchecked")
    35      public void testLoad() {
    36          Yaml yaml = new Yaml();
    37          Map<String, Map<String, String>> obj = (Map<String, Map<String, String>>) yaml
    38                  .load("test.string: {en: И}");
    39          assertEquals(1, obj.size());
    40          assertEquals("Map: " + obj.toString(), "И", obj.get("test.string").get("en"));
    41      }
    42
    43      public void testLoadFromFileWithWrongEncoding() {
    44          try {
    45              Yaml yaml = new Yaml();
    46              InputStream input = new FileInputStream("src/test/resources/issues/issue68.txt");
    47              Object text = yaml.load(new InputStreamReader(input, "Cp1252"));
    48              input.close();
    49              fail("Invalid UTF-8 must not be accepted: " + text.toString());
    50          } catch (Exception e) {
    51              assertEquals("special characters are not allowed", e.getMessage());
    52          }
    53      }
    54
    55      public void testLoadFromFile() throws UnsupportedEncodingException, FileNotFoundException {
    56          Yaml yaml = new Yaml();
    57          InputStream input = new FileInputStream("src/test/resources/issues/issue68.txt");
    58          String text = (String) yaml.load(new InputStreamReader(input, "UTF-8"));
    59          assertEquals("И жить торопится и чувствовать спешит...", text);
    60      }
    61
    62      public void testLoadFromInputStream() throws IOException {
    63          InputStream input;
    64          input = YamlDocument.class.getClassLoader().getResourceAsStream("issues/issue68.txt");
    65          if (input == null) {
    66              throw new RuntimeException("Can not find issues/issue68.txt");
    67          }
    68          Yaml yaml = new Yaml();
    69          String text = (String) yaml.load(input);// UTF-8 by default
    70          assertEquals("И жить торопится и чувствовать спешит...", text);
    71          input.close();
    72      }
    73  }