View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml;
17  
18  import java.io.InputStream;
19  import java.util.Map;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
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          DumperOptions options = new DumperOptions();
50          options.setDefaultScalarStyle(ScalarStyle.FOLDED);
51          Yaml yaml = new Yaml(options);
52          String data = (String) yaml.load(input);
53          assertEquals(etalon, data);
54          //
55          String dumped = yaml.dump(data);
56          String etalonDumped = Util.getLocalResource("specification/example2_15_dumped.yaml");
57          assertEquals(etalonDumped, dumped);
58      }
59  
60      @SuppressWarnings("unchecked")
61      public void testExample_2_16() {
62          YamlDocument document = new YamlDocument("example2_16.yaml");
63          Map<String, String> map = (Map<String, String>) document.getNativeData();
64          assertEquals(map.toString(), 3, map.size());
65          assertEquals("Mark McGwire", map.get("name"));
66          assertEquals("Mark set a major league home run record in 1998.\n",
67                  map.get("accomplishment"));
68          assertEquals("65 Home Runs\n0.278 Batting Average\n", map.get("stats"));
69  
70      }
71  
72      @SuppressWarnings("unchecked")
73      public void testExample_2_17() {
74          YamlDocument document = new YamlDocument("example2_17.yaml", false);
75          Map<String, String> map = (Map<String, String>) document.getNativeData();
76          assertEquals(map.toString(), 6, map.size());
77          assertEquals("Sosa did fine.\u263A", map.get("unicode"));
78          assertEquals("\b1998\t1999\t2000\n", map.get("control"));
79          assertEquals("\r\n is \r\n", map.get("hexesc"));
80          assertEquals("\"Howdy!\" he cried.", map.get("single"));
81          assertEquals(" # not a 'comment'.", map.get("quoted"));
82          assertEquals("|\\-*-/|", map.get("tie-fighter"));
83      }
84  
85      @SuppressWarnings("unchecked")
86      public void testExample_2_17_unicode() {
87          YamlDocument document = new YamlDocument("example2_17_unicode.yaml");
88          Map<String, String> map = (Map<String, String>) document.getNativeData();
89          assertEquals("Sosa did fine.\u263A", map.get("unicode"));
90      }
91  
92      @SuppressWarnings("unchecked")
93      public void testExample_2_17_control() {
94          YamlDocument document = new YamlDocument("example2_17_control.yaml", false);
95          Map<String, String> map = (Map<String, String>) document.getNativeData();
96          assertEquals("\b1998\t1999\t2000\n", map.get("control"));
97      }
98  
99      @SuppressWarnings("unchecked")
100     public void testExample_2_17_hexesc() {
101         YamlDocument document = new YamlDocument("example2_17_hexesc.yaml");
102         Map<String, String> map = (Map<String, String>) document.getNativeData();
103         assertEquals("\r\n is \r\n", map.get("hexesc"));
104     }
105 
106     @SuppressWarnings("unchecked")
107     public void testExample_2_17_single() {
108         YamlDocument document = new YamlDocument("example2_17_single.yaml");
109         Map<String, String> map = (Map<String, String>) document.getNativeData();
110         assertEquals("\"Howdy!\" he cried.", map.get("single"));
111     }
112 
113     @SuppressWarnings("unchecked")
114     public void testExample_2_17_quoted() {
115         YamlDocument document = new YamlDocument("example2_17_quoted.yaml");
116         Map<String, String> map = (Map<String, String>) document.getNativeData();
117         assertEquals(" # not a 'comment'.", map.get("quoted"));
118     }
119 
120     @SuppressWarnings("unchecked")
121     public void testExample_2_17_tie_fighter() {
122         YamlDocument document = new YamlDocument("example2_17_tie_fighter.yaml");
123         Map<String, String> map = (Map<String, String>) document.getNativeData();
124         assertEquals("|\\-*-/|", map.get("tie-fighter"));
125     }
126 
127     @SuppressWarnings("unchecked")
128     public void testExample_2_18() {
129         YamlDocument document = new YamlDocument("example2_18.yaml");
130         Map<String, String> map = (Map<String, String>) document.getNativeData();
131         assertEquals(map.toString(), 2, map.size());
132         assertEquals("This unquoted scalar spans many lines.", map.get("plain"));
133         assertEquals("So does this quoted scalar.\n", map.get("quoted"));
134     }
135 }