View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
17  package org.yaml.snakeyaml.issues.issue58;
18  
19  import java.util.ArrayList;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Yaml;
24  
25  public class NullValueDumperTest extends TestCase {
26  
27      public static class Foo {
28          private ArrayList<Object> bar = new ArrayList<Object>();
29  
30          public ArrayList<Object> getBar() {
31              return bar;
32          }
33  
34          public void setBar(ArrayList<Object> bar) {
35              this.bar = bar;
36          }
37      }
38  
39      public void testListElement() {
40          final Foo foo = new Foo();
41          foo.bar.add(1);
42          foo.bar.add("A");
43          foo.bar.add(3.14);
44          Yaml yaml = new Yaml();
45          assertEquals("bar:\n- 1\n- A\n- 3.14\n", yaml.dumpAsMap(foo));
46      }
47  
48      public void testNullListElement() {
49          final Foo foo = new Foo();
50  
51          foo.bar.add(1);
52          foo.bar.add("A");
53          foo.bar.add(null);
54          foo.bar.add(3.14);
55          Yaml yaml = new Yaml();
56          assertEquals("bar:\n- 1\n- A\n- null\n- 3.14\n", yaml.dumpAsMap(foo));
57          assertEquals(
58                  "!!org.yaml.snakeyaml.issues.issue58.NullValueDumperTest$Foo\nbar: [1, A, null, 3.14]\n",
59                  new Yaml().dump(foo));
60      }
61  }