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.generics;
18  
19  import static org.junit.Assert.assertArrayEquals;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  import org.yaml.snakeyaml.Yaml;
27  
28  public class ObjectValuesTest extends TestCase {
29  
30      public void testObjectValues() {
31          ObjectValues ov = new ObjectValues();
32          Integer obj = new Integer(131313);
33          ov.setObject(obj);
34          final Map<String, Map<Integer, Object>> prop2values = new HashMap<String, Map<Integer, Object>>();
35  
36          final String[] props = { "prop1", "prop2", "prop3" };
37          for (String name : props) {
38              Map<Integer, Object> values = new HashMap<Integer, Object>();
39              prop2values.put(name, values);
40              for (int i = 0; i < 3; i++) {
41                  values.put(i, name + i);
42              }
43          }
44  
45          ov.setValues(prop2values);
46          ov.setPossible(props);
47  
48          Yaml dumper = new Yaml();
49          String dumpedStr = dumper.dumpAsMap(ov);
50          Yaml loader = new Yaml();
51          ObjectValues ov2 = loader.loadAs(dumpedStr, ObjectValues.class);
52  
53          assertEquals(ov.getObject(), ov2.getObject());
54          assertEquals(ov.getValues(), ov2.getValues());
55          assertArrayEquals(ov.getPossible(), ov2.getPossible());
56          ov.getPossible()[0] = ov2.getPossible()[0];
57      }
58  
59      @SuppressWarnings("unchecked")
60      public void testObjectValuesWithParam() {
61          ObjectValuesWithParam<String, Integer> ov = new ObjectValuesWithParam<String, Integer>();
62          Integer obj = new Integer(131313);
63          ov.setObject(obj);
64          final Map<String, Map<Integer, Object>> prop2values = new HashMap<String, Map<Integer, Object>>();
65  
66          final String[] props = { "prop1", "prop2", "prop3" };
67          for (String name : props) {
68              Map<Integer, Object> values = new HashMap<Integer, Object>();
69              prop2values.put(name, values);
70              for (int i = 0; i < 3; i++) {
71                  values.put(i, name + i);
72              }
73          }
74  
75          ov.setValues(prop2values);
76          ov.setPossible(props);
77  
78          Yaml dumper = new Yaml();
79          String dumpedStr = dumper.dumpAsMap(ov);
80          Yaml loader = new Yaml();
81          ObjectValuesWithParam<String, Integer> ov2 = loader.loadAs(dumpedStr,
82                  new ObjectValuesWithParam<String, Integer>().getClass());
83  
84          assertEquals(ov.getObject(), ov2.getObject());
85          assertEquals(ov.getValues(), ov2.getValues());
86          assertArrayEquals(ov.getPossible(), ov2.getPossible());
87          // TODO: This actually FAILS. Use of GenericArrays is ..... no words.
88          // assertEquals(ov.getPossible()[0], ov2.getPossible()[0]);
89          try {
90              ov2.getPossible()[0].toString();
91          } catch (Exception e) {
92              assertTrue(e.getMessage(), e.getMessage().startsWith("[Ljava.lang.Object"));
93          }
94      }
95  }