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.issue95;
    18
    19  import java.util.ArrayList;
    20  import java.util.HashMap;
    21  import java.util.List;
    22  import java.util.Map;
    23  import java.util.Map.Entry;
    24  import java.util.Set;
    25
    26  import junit.framework.TestCase;
    27
    28  import org.junit.Assert;
    29  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
    30  import org.yaml.snakeyaml.TypeDescription;
    31  import org.yaml.snakeyaml.Yaml;
    32  import org.yaml.snakeyaml.constructor.Constructor;
    33  import org.yaml.snakeyaml.introspector.BeanAccess;
    34  import org.yaml.snakeyaml.nodes.Tag;
    35
    36  public class ArrayInGenericCollectionTest extends TestCase {
    37
    38      public static class A {
    39          private Map<String, String[]> meta = new HashMap<String, String[]>();
    40      }
    41
    42      public static class B {
    43          private List<String[]> meta = new ArrayList<String[]>();
    44      }
    45
    46      private A createA() {
    47          A a = new A();
    48          a.meta.put("met1", new String[] { "whatever" });
    49          a.meta.put("met2", new String[] { "something", "something else" });
    50          return a;
    51      }
    52
    53      private B createB() {
    54          B b = new B();
    55          b.meta.add(new String[] { "whatever" });
    56          b.meta.add(new String[] { "something", "something else" });
    57          return b;
    58      }
    59
    60      public void testArrayAsMapValue() {
    61          Yaml yaml2dump = new Yaml();
    62          yaml2dump.setBeanAccess(BeanAccess.FIELD);
    63          A data = createA();
    64          String dump = yaml2dump.dump(data);
    65          // System.out.println(dump);
    66
    67          Yaml yaml2load = new Yaml();
    68          yaml2load.setBeanAccess(BeanAccess.FIELD);
    69          A loaded = (A) yaml2load.load(dump);
    70
    71          assertEquals(data.meta.size(), loaded.meta.size());
    72          Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet();
    73          for (Entry<String, String[]> entry : loadedMeta) {
    74              assertTrue(data.meta.containsKey(entry.getKey()));
    75              Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue());
    76          }
    77      }
    78
    79      public void testArrayAsMapValueWithTypeDespriptor() {
    80          Yaml yaml2dump = new Yaml();
    81          yaml2dump.setBeanAccess(BeanAccess.FIELD);
    82          A data = createA();
    83          String dump = yaml2dump.dump(data);
    84          // System.out.println(dump);
    85
    86          TypeDescription aTypeDescr = new TypeDescription(A.class);
    87          aTypeDescr.putMapPropertyType("meta", String.class, String[].class);
    88
    89          Constructor c = new Constructor();
    90          c.addTypeDescription(aTypeDescr);
    91          Yaml yaml2load = new Yaml(c);
    92          yaml2load.setBeanAccess(BeanAccess.FIELD);
    93
    94          A loaded = (A) yaml2load.load(dump);
    95
    96          assertEquals(data.meta.size(), loaded.meta.size());
    97          Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet();
    98          for (Entry<String, String[]> entry : loadedMeta) {
    99              assertTrue(data.meta.containsKey(entry.getKey()));
    100             Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue());
    101         }
    102     }
    103
    104     public void testArrayAsListValue() {
    105         Yaml yaml2dump = new Yaml();
    106         yaml2dump.setBeanAccess(BeanAccess.FIELD);
    107         B data = createB();
    108         String dump = yaml2dump.dump(data);
    109         // System.out.println(dump);
    110
    111         Yaml yaml2load = new Yaml();
    112         yaml2load.setBeanAccess(BeanAccess.FIELD);
    113         B loaded = (B) yaml2load.load(dump);
    114
    115         Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray());
    116     }
    117
    118     public void testArrayAsListValueWithTypeDespriptor() {
    119         Yaml yaml2dump = new Yaml();
    120         yaml2dump.setBeanAccess(BeanAccess.FIELD);
    121         B data = createB();
    122         String dump = yaml2dump.dump(data);
    123         // System.out.println(dump);
    124
    125         TypeDescription aTypeDescr = new TypeDescription(B.class);
    126         aTypeDescr.putListPropertyType("meta", String[].class);
    127
    128         Constructor c = new Constructor();
    129         c.addTypeDescription(aTypeDescr);
    130         Yaml yaml2load = new Yaml(c);
    131         yaml2load.setBeanAccess(BeanAccess.FIELD);
    132
    133         B loaded = (B) yaml2load.load(dump);
    134
    135         Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray());
    136     }
    137
    138     public void testNoTags() {
    139         Yaml yaml2dump = new Yaml();
    140         yaml2dump.setBeanAccess(BeanAccess.FIELD);
    141         B data = createB();
    142         String dump = yaml2dump.dumpAs(data, Tag.MAP, FlowStyle.AUTO);
    143         // System.out.println(dump);
    144         assertEquals("meta:\n- [whatever]\n- [something, something else]\n", dump);
    145         //
    146         Constructor constr = new Constructor(B.class);
    147         Yaml yaml2load = new Yaml(constr);
    148         yaml2load.setBeanAccess(BeanAccess.FIELD);
    149         B loaded = (B) yaml2load.load(dump);
    150
    151         Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray());
    152     }
    153 }