View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }