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 org.yaml.snakeyaml;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * Test Chapter 2.3 from the YAML specification
27   * 
28   * @author py4fun
29   * @see http://yaml.org/spec/1.1/
30   */
31  public class Chapter2_3Test extends TestCase {
32  
33      public void testExample_2_13() {
34          YamlDocument document = new YamlDocument("example2_13.yaml");
35          String data = (String) document.getNativeData();
36          assertEquals("\\//||\\/||\n// ||  ||__\n", data);
37      }
38  
39      public void testExample_2_14() {
40          YamlDocument document = new YamlDocument("example2_14.yaml");
41          String data = (String) document.getNativeData();
42          assertEquals("Mark McGwire's year was crippled by a knee injury.", data);
43      }
44  
45      public void testExample_2_15() {
46          String etalon = "Sammy Sosa completed another fine season with great stats.\n\n  63 Home Runs\n  0.288 Batting Average\n\nWhat a year!\n";
47          InputStream input = YamlDocument.class.getClassLoader().getResourceAsStream(
48                  YamlDocument.ROOT + "example2_15.yaml");
49          Yaml yaml = new Yaml();
50          String data = (String) yaml.load(input);
51          assertEquals(etalon, data);
52          //
53          String dumped = yaml.dump(data);
54          assertTrue(dumped.contains("Sammy Sosa completed another fine season with great stats"));
55          assertEquals("Must be splitted into 2 lines.", 2, dumped.split("\n").length);
56      }
57  
58      @SuppressWarnings("unchecked")
59      public void testExample_2_16() {
60          YamlDocument document = new YamlDocument("example2_16.yaml");
61          Map<String, String> map = (Map<String, String>) document.getNativeData();
62          assertEquals(map.toString(), 3, map.size());
63          assertEquals("Mark McGwire", map.get("name"));
64          assertEquals("Mark set a major league home run record in 1998.\n",
65                  map.get("accomplishment"));
66          assertEquals("65 Home Runs\n0.278 Batting Average\n", map.get("stats"));
67  
68      }
69  
70      @SuppressWarnings("unchecked")
71      public void testExample_2_17() throws IOException {
72          YamlDocument document = new YamlDocument("example2_17.yaml", false);
73          Map<String, String> map = (Map<String, String>) document.getNativeData();
74          assertEquals(map.toString(), 6, map.size());
75          assertEquals("Sosa did fine.\u263A", map.get("unicode"));
76          assertEquals("\b1998\t1999\t2000\n", map.get("control"));
77          assertEquals("\r\n is \r\n", map.get("hexesc"));
78          assertEquals("\"Howdy!\" he cried.", map.get("single"));
79          assertEquals(" # not a 'comment'.", map.get("quoted"));
80          assertEquals("|\\-*-/|", map.get("tie-fighter"));
81      }
82  
83      @SuppressWarnings("unchecked")
84      public void testExample_2_17_unicode() {
85          YamlDocument document = new YamlDocument("example2_17_unicode.yaml");
86          Map<String, String> map = (Map<String, String>) document.getNativeData();
87          assertEquals("Sosa did fine.\u263A", map.get("unicode"));
88      }
89  
90      @SuppressWarnings("unchecked")
91      public void testExample_2_17_control() {
92          YamlDocument document = new YamlDocument("example2_17_control.yaml", false);
93          Map<String, String> map = (Map<String, String>) document.getNativeData();
94          assertEquals("\b1998\t1999\t2000\n", map.get("control"));
95      }
96  
97      @SuppressWarnings("unchecked")
98      public void testExample_2_17_hexesc() {
99          YamlDocument document = new YamlDocument("example2_17_hexesc.yaml");
100         Map<String, String> map = (Map<String, String>) document.getNativeData();
101         assertEquals("\r\n is \r\n", map.get("hexesc"));
102     }
103 
104     @SuppressWarnings("unchecked")
105     public void testExample_2_17_single() {
106         YamlDocument document = new YamlDocument("example2_17_single.yaml");
107         Map<String, String> map = (Map<String, String>) document.getNativeData();
108         assertEquals("\"Howdy!\" he cried.", map.get("single"));
109     }
110 
111     @SuppressWarnings("unchecked")
112     public void testExample_2_17_quoted() {
113         YamlDocument document = new YamlDocument("example2_17_quoted.yaml");
114         Map<String, String> map = (Map<String, String>) document.getNativeData();
115         assertEquals(" # not a 'comment'.", map.get("quoted"));
116     }
117 
118     @SuppressWarnings("unchecked")
119     public void testExample_2_17_tie_fighter() {
120         YamlDocument document = new YamlDocument("example2_17_tie_fighter.yaml");
121         Map<String, String> map = (Map<String, String>) document.getNativeData();
122         assertEquals("|\\-*-/|", map.get("tie-fighter"));
123     }
124 
125     @SuppressWarnings("unchecked")
126     public void testExample_2_18() throws IOException {
127         YamlDocument document = new YamlDocument("example2_18.yaml");
128         Map<String, String> map = (Map<String, String>) document.getNativeData();
129         assertEquals(map.toString(), 2, map.size());
130         assertEquals("This unquoted scalar spans many lines.", map.get("plain"));
131         assertEquals("So does this quoted scalar.\n", map.get("quoted"));
132     }
133 }