View Javadoc

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