1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
74 assertEquals("И жить торопится и чувствовать спешит...", text);
75 input.close();
76 }
77 }