1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.List;
24 import java.util.Map;
25
26 import junit.framework.TestCase;
27
28 import org.yaml.snakeyaml.Yaml;
29
30 public class LoadExampleTest extends TestCase {
31 @SuppressWarnings("unchecked")
32 public void testLoad() {
33 Yaml yaml = new Yaml();
34 String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
35 List<String> list = (List<String>) yaml.load(document);
36 assertEquals("[Hesperiidae, Papilionidae, Apatelodidae, Epiplemidae]", list.toString());
37 }
38
39 public void testLoadFromString() {
40 Yaml yaml = new Yaml();
41 String document = "hello: 25";
42 @SuppressWarnings("unchecked")
43 Map<String, Integer> map = (Map<String, Integer>) yaml.load(document);
44 assertEquals("{hello=25}", map.toString());
45 assertEquals(new Integer(25), map.get("hello"));
46 }
47
48 public void testLoadFromStream() throws IOException {
49 InputStream input = new FileInputStream(new File("src/test/resources/reader/utf-8.txt"));
50 Yaml yaml = new Yaml();
51 Object data = yaml.load(input);
52 assertEquals("test", data);
53
54 data = yaml.load(new ByteArrayInputStream("test2".getBytes("UTF-8")));
55 assertEquals("test2", data);
56 input.close();
57 }
58
59 public void testLoadManyDocuments() throws IOException {
60 InputStream input = new FileInputStream(new File(
61 "src/test/resources/specification/example2_28.yaml"));
62 Yaml yaml = new Yaml();
63 int counter = 0;
64 for (Object data : yaml.loadAll(input)) {
65 assertNotNull(data);
66 assertTrue(data.toString().length() > 1);
67 counter++;
68 }
69 assertEquals(3, counter);
70 input.close();
71 }
72 }