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