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