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