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.constructor;
    18
    19  import java.util.Iterator;
    20  import java.util.LinkedHashMap;
    21  import java.util.Map;
    22  import java.util.TimeZone;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.yaml.snakeyaml.Yaml;
    27
    28  public class ConstructorTest extends TestCase {
    29
    30      @SuppressWarnings("unchecked")
    31      public void testMapOrder() {
    32          String data = "one: zzz\ntwo: ccc\nthree: bbb\nfour: aaa";
    33          Object map = construct(data);
    34          assertNotNull(map);
    35          assertTrue(map.getClass().toString(), map instanceof LinkedHashMap);
    36          Map<String, String> m = (Map<String, String>) map;
    37          assertEquals(4, m.keySet().size());
    38          Iterator<String> iter = m.keySet().iterator();
    39          assertEquals("one", iter.next());
    40          assertEquals("two", iter.next());
    41          assertEquals("three", iter.next());
    42          assertEquals("four", iter.next());
    43      }
    44
    45      /**
    46       * create JavaBean
    47       */
    48      public void testGetBeanAssumeClass() {
    49          String data = "--- !!org.yaml.snakeyaml.constructor.Person\nfirstName: Andrey\nage: 99";
    50          Object obj = construct(data);
    51          assertNotNull(obj);
    52          assertTrue("Unexpected: " + obj.getClass().toString(), obj instanceof Person);
    53          Person person = (Person) obj;
    54          assertEquals("Andrey", person.getFirstName());
    55          assertNull(person.getLastName());
    56          assertEquals(99, person.getAge().intValue());
    57      }
    58
    59      /**
    60       * create instance using constructor arguments
    61       */
    62      public void testGetConstructorBean() {
    63          String data = "--- !!org.yaml.snakeyaml.constructor.Person [ Andrey, Somov, 99 ]";
    64          Object obj = construct(data);
    65          assertNotNull(obj);
    66          assertTrue(obj.getClass().toString(), obj instanceof Person);
    67          Person person = (Person) obj;
    68          assertEquals("Andrey", person.getFirstName());
    69          assertEquals("Somov", person.getLastName());
    70          assertEquals(99, person.getAge().intValue());
    71      }
    72
    73      /**
    74       * create instance using scalar argument
    75       */
    76      public void testGetConstructorFromScalar() {
    77          String data = "--- !!org.yaml.snakeyaml.constructor.Person 'Somov'";
    78          Object obj = construct(data);
    79          assertNotNull(obj);
    80          assertTrue(obj.getClass().toString(), obj instanceof Person);
    81          Person person = (Person) obj;
    82          assertNull("Andrey", person.getFirstName());
    83          assertEquals("Somov", person.getLastName());
    84          assertNull(person.getAge());
    85      }
    86
    87      public void testJavaBeanLoad() {
    88          java.util.Calendar cal = java.util.Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    89          cal.clear();
    90          cal.set(1982, 5 - 1, 3); // Java's months are zero-based...
    91
    92          TestBean expected = new TestBean("Ola Bini", 24, cal.getTime());
    93          assertEquals(
    94                  expected,
    95                  construct("--- !!org.yaml.snakeyaml.constructor.TestBean\nname: Ola Bini\nage: 24\nborn: 1982-05-03\n"));
    96      }
    97
    98      public void testWrongName() {
    99          try {
    100             construct("--- !!org.yaml.snakeyaml.constructor.TestBean\nwrongName: No one\nage: 24\nborn: 1982-05-03\n");
    101             fail("IntrospectionException expected.");
    102         } catch (Exception e) {
    103             assertEquals(
    104                     "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.constructor.TestBean; exception=Cannot create property=wrongName for JavaBean=#<org.jvyaml.TestBean name=\"null\" age=0 born=\"null\">; Unable to find property 'wrongName' on class: org.yaml.snakeyaml.constructor.TestBean",
    105                     e.getMessage());
    106         }
    107     }
    108
    109     private Object construct(String data) {
    110         Yaml yaml = new Yaml();
    111         return yaml.load(data);
    112     }
    113 }