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