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;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.TreeMap;
22  
23  import junit.framework.TestCase;
24  
25  public class CollectionWithBeanYamlTest extends TestCase {
26  
27      @SuppressWarnings("unchecked")
28      public void testYamlMap() {
29          Map<String, Bean> data = new TreeMap<String, Bean>();
30          data.put("gold1", new Bean());
31          data.put("gold2", new Bean());
32  
33          Yaml yaml = new Yaml();
34          String output = yaml.dump(data);
35          assertEquals(
36                  "gold1: !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: ''}\ngold2: !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: ''}\n",
37                  output);
38          Object o = yaml.load(output);
39  
40          assertTrue(o instanceof Map);
41          Map<String, Bean> m = (Map<String, Bean>) o;
42          assertTrue(m.get("gold1") instanceof Bean);
43          assertTrue("" + m.get("gold2").getClass(), m.get("gold2") instanceof Bean);
44      }
45  
46      @SuppressWarnings("unchecked")
47      public void testYamlList() {
48          List<Bean> data = new ArrayList<Bean>();
49          data.add(new Bean("1"));
50          data.add(new Bean("2"));
51  
52          Yaml yaml = new Yaml();
53          String output = yaml.dump(data);
54          assertEquals(
55                  "- !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: '1'}\n- !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: '2'}\n",
56                  output);
57          Object o = yaml.load(output);
58  
59          assertTrue(o instanceof List);
60          List<Bean> m = (List<Bean>) o;
61          assertEquals(2, m.size());
62          assertTrue(m.get(0) instanceof Bean);
63          assertTrue(m.get(1) instanceof Bean);
64      }
65  
66      public static class Bean {
67          private String a;
68  
69          public Bean() {
70              a = "";
71          }
72  
73          public Bean(String value) {
74              a = value;
75          }
76  
77          public String getA() {
78              return a;
79          }
80  
81          public void setA(String s) {
82              a = s;
83          }
84      }
85  }