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.issue112;
    18
    19  import static org.junit.Assert.assertEquals;
    20  import static org.junit.Assert.assertNotNull;
    21
    22  import java.io.IOException;
    23  import java.io.InputStream;
    24
    25  import org.junit.Test;
    26  import org.yaml.snakeyaml.Util;
    27  import org.yaml.snakeyaml.Yaml;
    28  import org.yaml.snakeyaml.constructor.Constructor;
    29
    30  public class ParameterisedTypeLoadingTestCase {
    31
    32      @Test
    33      public void testParameterisedTypeLoading() throws IOException {
    34          Yaml yamlParser = new Yaml(new Constructor(MyCompositeObject.class));
    35          MyCompositeObject obj = (MyCompositeObject) yamlParser.load(getInput());
    36          check(obj);
    37
    38          // dump the object
    39          Yaml yaml = new Yaml();
    40          String output = yaml.dumpAsMap(obj);
    41          assertEquals(Util.getLocalResource("issues/issue112-2.yaml"), output);
    42      }
    43
    44      @Test
    45      public void testJavaBeanLoader() throws IOException {
    46          Yaml yamlParser = new Yaml();
    47          MyCompositeObject obj = yamlParser.loadAs(getInput(), MyCompositeObject.class);
    48          check(obj);
    49      }
    50
    51      private void check(MyCompositeObject obj) {
    52          Object[] values = { 1, "two", 3, "four", "!!!" };
    53          assertNotNull(obj);
    54          assertEquals(5, obj.getThings().size());
    55          int i = 0;
    56          for (MyClass<? extends Object> thing : obj.getThings()) {
    57              assertEquals(MyClass.class, thing.getClass());
    58              assertNotNull("The 'name' property must be set.", thing.getName());
    59              assertEquals(values[i++], thing.getName());
    60          }
    61      }
    62
    63      private InputStream getInput() throws IOException {
    64          return this.getClass().getClassLoader().getResource("issues/issue112-1.yaml").openStream();
    65      }
    66  }