xref: /aosp_15_r20/external/snakeyaml/src/test/java/examples/DumpExampleTest.java (revision ac2a7c1bf4e14d82f3bd566dcc2d76d5b42faf34)
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples;
15 
16 import java.io.StringWriter;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import junit.framework.TestCase;
22 import org.yaml.snakeyaml.DumperOptions;
23 import org.yaml.snakeyaml.Yaml;
24 
25 public class DumpExampleTest extends TestCase {
26 
testDump()27   public void testDump() {
28     Map<String, Object> data = new HashMap<String, Object>();
29     data.put("name", "Silenthand Olleander");
30     data.put("race", "Human");
31     data.put("traits", new String[] {"ONE_HAND", "ONE_EYE"});
32     Yaml yaml = new Yaml();
33     String output = yaml.dump(data);
34     assertTrue(output.contains("name: Silenthand Olleander"));
35     assertTrue(output.contains("race: Human"));
36     assertTrue(output.contains("traits: [ONE_HAND, ONE_EYE]"));
37   }
38 
testDumpWriter()39   public void testDumpWriter() {
40     Map<String, Object> data = new HashMap<String, Object>();
41     data.put("name", "Silenthand Olleander");
42     data.put("race", "Human");
43     data.put("traits", new String[] {"ONE_HAND", "ONE_EYE"});
44     Yaml yaml = new Yaml();
45     StringWriter writer = new StringWriter();
46     yaml.dump(data, writer);
47     assertTrue(writer.toString().contains("name: Silenthand Olleander"));
48     assertTrue(writer.toString().contains("race: Human"));
49     assertTrue(writer.toString().contains("traits: [ONE_HAND, ONE_EYE]"));
50   }
51 
testDumpMany()52   public void testDumpMany() {
53     List<Integer> docs = new ArrayList<Integer>();
54     for (int i = 1; i < 4; i++) {
55       docs.add(i);
56     }
57     DumperOptions options = new DumperOptions();
58     options.setExplicitStart(true);
59     Yaml yaml = new Yaml(options);
60     String result = yaml.dumpAll(docs.iterator());
61     assertNotNull(result);
62     assertTrue(result.contains("--- 2"));
63   }
64 
testDumpCustomJavaClass()65   public void testDumpCustomJavaClass() {
66     Hero hero = new Hero("Galain Ysseleg", -3, 2);
67     DumperOptions options = new DumperOptions();
68     options.setAllowReadOnlyProperties(true);
69     Yaml yaml = new Yaml(options);
70     String output = yaml.dump(hero);
71     assertEquals("!!examples.Hero {hp: -3, name: Galain Ysseleg, sp: 2}\n", output);
72   }
73 
testDumperOptions()74   public void testDumperOptions() {
75     List<Integer> data = new ArrayList<Integer>();
76     for (int i = 0; i < 50; i++) {
77       data.add(i);
78     }
79     Yaml yaml = new Yaml();
80     String output = yaml.dump(data);
81     assertTrue(output.contains("[0, 1, 2, 3, 4, 5, 6, 7, 8"));
82     //
83     DumperOptions options = new DumperOptions();
84     options.setWidth(50);
85     options.setIndent(4);
86     yaml = new Yaml(options);
87     output = yaml.dump(data);
88     assertTrue(output.contains("1, 2"));
89   }
90 
testDumperOptionsCanonical()91   public void testDumperOptionsCanonical() {
92     List<Integer> data = new ArrayList<Integer>();
93     for (int i = 0; i < 5; i++) {
94       data.add(i);
95     }
96     DumperOptions options = new DumperOptions();
97     options.setCanonical(true);
98     Yaml yaml = new Yaml(options);
99     String output = yaml.dump(data);
100     assertTrue(output.contains("---"));
101     assertTrue(output.contains("!!seq ["));
102     assertTrue(output.contains("!!int \"3\","));
103   }
104 
testDumperOptionsFlowStyle()105   public void testDumperOptionsFlowStyle() {
106     List<Integer> data = new ArrayList<Integer>();
107     for (int i = 0; i < 5; i++) {
108       data.add(i);
109     }
110     DumperOptions options = new DumperOptions();
111     options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
112     Yaml yaml = new Yaml(options);
113     String output = yaml.dump(data);
114     assertTrue(output.contains("- 0\n"));
115     assertTrue(output.contains("- 1\n"));
116     assertTrue(output.contains("- 4\n"));
117   }
118 
testDumperOptionsStyle()119   public void testDumperOptionsStyle() {
120     List<Integer> data = new ArrayList<Integer>();
121     for (int i = 0; i < 5; i++) {
122       data.add(i);
123     }
124     DumperOptions options = new DumperOptions();
125     options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
126     Yaml yaml = new Yaml(options);
127     String output = yaml.dump(data);
128     assertTrue(output.contains("- !!int \"0\""));
129     assertTrue(output.contains("- !!int \"1\""));
130     assertTrue(output.contains("- !!int \"4\""));
131   }
132 }
133