1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.types;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.yaml.snakeyaml.DumperOptions;
24 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.nodes.Node;
27 import org.yaml.snakeyaml.nodes.Tag;
28 import org.yaml.snakeyaml.representer.Represent;
29 import org.yaml.snakeyaml.representer.Representer;
30
31
32
33
34 public class NullTagTest extends AbstractTest {
35
36 public void testNull() {
37 assertNull("Got: '" + load("---\n") + "'", load("---\n"));
38 assertNull(load("---\n..."));
39 assertNull(load("---\n...\n"));
40 assertNull(load("\n"));
41 assertNull(load(""));
42 assertNull(load(" "));
43 assertNull(load("~"));
44 assertNull(load("---\n~"));
45 assertNull(load("null"));
46 assertNull(load("Null"));
47 assertNull(load("NULL"));
48 assertNull(getMapValue("empty:\n", "empty"));
49 assertNull(getMapValue("canonical: ~", "canonical"));
50 assertNull(getMapValue("english: null", "english"));
51 assertNull(getMapValue("english: Null", "english"));
52 assertNull(getMapValue("english: NULL", "english"));
53 assertEquals("null key", getMapValue("~: null key\n", null));
54 }
55
56 @SuppressWarnings("unchecked")
57 public void testSequenceNull() {
58 String input = "---\n# This sequence has five\n# entries, two have values.\nsparse:\n - ~\n - 2nd entry\n -\n - 4th entry\n - Null\n";
59 List<String> parsed = (List<String>) getMapValue(input, "sparse");
60 assertEquals(5, parsed.size());
61 assertNull(parsed.get(0));
62 assertEquals("2nd entry", parsed.get(1));
63 assertNull("Got: '" + parsed.get(2) + "'", parsed.get(2));
64 assertEquals("4th entry", parsed.get(3));
65 assertNull(parsed.get(4));
66 }
67
68 public void testNullInMap() {
69 String input = "key1: null\n~: value1";
70 Map<String, Object> parsed = getMap(input);
71 assertEquals(2, parsed.size());
72 assertTrue(parsed.containsKey(null));
73 Object value1 = parsed.get(null);
74 assertEquals("value1", value1);
75
76 assertNull(parsed.get("key1"));
77
78 assertFalse(getMap("key2: value2").containsKey(null));
79 }
80
81 public void testNullShorthand() {
82 assertNull(getMapValue("nothing: !!null null", "nothing"));
83 }
84
85 public void testNullTag() {
86 assertNull(getMapValue("nothing: !<tag:yaml.org,2002:null> null", "nothing"));
87 }
88
89 public void testNullOut() {
90 String output = dump(null);
91 assertEquals("null\n", output);
92 }
93
94 public void testNullOutAsEmpty() {
95 Yaml yaml = new Yaml(new NullRepresenter());
96 String output = yaml.dump(null);
97 assertEquals("", output);
98 }
99
100
101
102
103 public void testNullOutAsEmpty2() {
104 Yaml yaml = new Yaml(new NullRepresenter());
105 Map<String, String> map = new HashMap<String, String>();
106 map.put("aaa", "foo");
107 map.put("bbb", null);
108 String output = yaml.dump(map);
109 assertEquals("{aaa: foo, bbb: !!null ''}\n", output);
110 }
111
112
113
114
115 public void testBoolOutAsEmpty3() {
116 DumperOptions options = new DumperOptions();
117 options.setDefaultFlowStyle(FlowStyle.BLOCK);
118 Yaml yaml = new Yaml(new NullRepresenter(), options);
119 Map<String, String> map = new HashMap<String, String>();
120 map.put("aaa", "foo");
121 map.put("bbb", null);
122 String output = yaml.dump(map);
123 assertEquals("aaa: foo\nbbb:\n", output);
124 }
125
126 private class NullRepresenter extends Representer {
127 public NullRepresenter() {
128 super();
129
130
131 this.nullRepresenter = new RepresentNull();
132 }
133
134 private class RepresentNull implements Represent {
135 public Node representData(Object data) {
136
137 return representScalar(Tag.NULL, "");
138 }
139 }
140 }
141
142 public void testNoAnchors() {
143 List<String> list = new ArrayList<String>(3);
144 list.add(null);
145 list.add("value");
146 list.add(null);
147 Yaml yaml = new Yaml();
148 String output = yaml.dump(list);
149 assertEquals("Null values must not get anchors and aliases.", "[null, value, null]\n",
150 output);
151 }
152 }