aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/examples/LoadExampleTest.java
blob: 9c620ee9e90d0bcc4546e61ca9a884bca4e1efbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package examples;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
import org.yaml.snakeyaml.Yaml;

public class LoadExampleTest extends TestCase {

  @SuppressWarnings("unchecked")
  public void testLoad() {
    Yaml yaml = new Yaml();
    String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
    List<String> list = yaml.load(document);
    assertEquals("[Hesperiidae, Papilionidae, Apatelodidae, Epiplemidae]", list.toString());
  }

  public void testLoadFromString() {
    Yaml yaml = new Yaml();
    String document = "hello: 25";
    @SuppressWarnings("unchecked")
    Map<String, Integer> map = yaml.load(document);
    assertEquals("{hello=25}", map.toString());
    assertEquals(Integer.valueOf(25), map.get("hello"));
  }

  public void testLoadFromStream() throws IOException {
    InputStream input = new FileInputStream(new File("src/test/resources/reader/utf-8.txt"));
    Yaml yaml = new Yaml();
    Object data = yaml.load(input);
    assertEquals("test", data);
    //
    data = yaml.load(new ByteArrayInputStream("test2".getBytes(StandardCharsets.UTF_8)));
    assertEquals("test2", data);
    input.close();
  }

  public void testLoadManyDocuments() throws IOException {
    InputStream input =
        new FileInputStream(new File("src/test/resources/specification/example2_28.yaml"));
    Yaml yaml = new Yaml();
    int counter = 0;
    for (Object data : yaml.loadAll(input)) {
      assertNotNull(data);
      assertTrue(data.toString().length() > 1);
      counter++;
    }
    assertEquals(3, counter);
    input.close();
  }

  public void testLoadManyDocumentsWithIterator() throws IOException {
    InputStream input =
        new FileInputStream(new File("src/test/resources/specification/example2_28.yaml"));
    Yaml yaml = new Yaml();
    int counter = 0;
    Iterator<Object> iter = yaml.loadAll(input).iterator();
    while (iter.hasNext()) {
      Object data = iter.next();
      assertNotNull(data);
      assertTrue(data.toString().length() > 1);
      counter++;
    }
    assertEquals(3, counter);
    input.close();
  }

  public void testLoadManyDocumentsWithIterator2() throws IOException {
    InputStream input =
        new FileInputStream(new File("src/test/resources/specification/example2_28.yaml"));
    Yaml yaml = new Yaml();
    Iterator<Object> iter = yaml.loadAll(input).iterator();
    Object data = iter.next();
    assertNotNull(data);
    data = iter.next();
    assertNotNull(data);
    data = iter.next();
    assertNotNull(data);
    try {
      iter.next();
      fail("Expect NoSuchElementException");
    } catch (NoSuchElementException e) {
      assertEquals("No document is available.", e.getMessage());
    }
    input.close();
  }
}