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