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