1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.issues.issue29;
17
18 import java.util.LinkedHashMap;
19 import java.util.Map;
20
21 import junit.framework.TestCase;
22
23 import org.yaml.snakeyaml.DumperOptions;
24 import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
25 import org.yaml.snakeyaml.Util;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.nodes.Node;
28 import org.yaml.snakeyaml.nodes.ScalarNode;
29 import org.yaml.snakeyaml.representer.Representer;
30
31
32
33
34 public class FlexibleScalarStyleTest extends TestCase {
35 public void testLong() {
36 DumperOptions options = new DumperOptions();
37 options.setDefaultScalarStyle(ScalarStyle.FOLDED);
38 Yaml yaml = new Yaml(options);
39 String result = yaml
40 .dump("qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq "
41 + "qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq "
42 + "qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n "
43 + "qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n");
44
45 assertTrue(result.startsWith(">\n"));
46 assertEquals(
47 ">\n qqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqq\n qqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqq 111111111111111111111111\n qqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqq\n",
48 result);
49 }
50
51 public void testNoFoldedScalar() {
52 DumperOptions options = new DumperOptions();
53 options.setWidth(30);
54 Yaml yaml = new Yaml(options);
55 String output = yaml.dump(getData());
56
57 String etalon = Util.getLocalResource("representer/scalar-style1.yaml");
58 assertEquals(etalon, output);
59 }
60
61 public void testCustomScalarStyle() {
62 DumperOptions options = new DumperOptions();
63 options.setWidth(30);
64 Yaml yaml = new Yaml(new MyRepresenter(), options);
65 String output = yaml.dump(getData());
66
67 String etalon = Util.getLocalResource("representer/scalar-style2.yaml");
68 assertEquals(etalon, output);
69 }
70
71 private Map<String, String> getData() {
72 Map<String, String> map = new LinkedHashMap<String, String>();
73 map.put("name", "Steve Jobs");
74 map.put("address", "Name\nStreet Number\nCountry");
75 map.put("description",
76 "1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
77 return map;
78 }
79
80 private class MyRepresenter extends Representer {
81
82 public MyRepresenter() {
83 super();
84 this.representers.put(String.class, new FlexibleRepresent());
85 }
86
87 private class FlexibleRepresent extends RepresentString {
88 public Node representData(Object data) {
89 ScalarNode node = (ScalarNode) super.representData(data);
90 if (node.getStyle() == null) {
91
92 if (node.getValue().length() < 25) {
93 return node;
94 } else {
95
96 return new ScalarNode(node.getTag(), node.getValue(), node.getStartMark(),
97 node.getEndMark(), '>');
98 }
99 } else {
100 return node;
101 }
102 }
103 }
104 }
105 }