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