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