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 java.beans.IntrospectionException;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Yaml;
24  
25  public class BirdTest extends TestCase {
26  
27      public void testHome() throws IntrospectionException {
28          Bird bird = new Bird();
29          bird.setName("Eagle");
30          Nest home = new Nest();
31          home = new Nest();
32          home.setHeight(3);
33          bird.setHome(home);
34          Yaml yaml = new Yaml();
35          String output = yaml.dumpAsMap(bird);
36          Bird parsed;
37          String javaVendor = System.getProperty("java.vm.name");
38          Yaml loader = new Yaml();
39          if (GenericsBugDetector.isProperIntrospection()) {
40              // no global tags
41              System.out.println("java.vm.name: " + javaVendor);
42              assertEquals("no global tags must be emitted.", "home:\n  height: 3\nname: Eagle\n",
43                      output);
44              parsed = loader.loadAs(output, Bird.class);
45  
46          } else {
47              // with global tags
48              System.out
49                      .println("JDK requires global tags for JavaBean properties with Java Generics. java.vm.name: "
50                              + javaVendor);
51              assertEquals("global tags are inevitable here.",
52                      "home: !!org.yaml.snakeyaml.generics.Nest\n  height: 3\nname: Eagle\n", output);
53              parsed = loader.loadAs(output, Bird.class);
54          }
55          assertEquals(bird.getName(), parsed.getName());
56          assertEquals(bird.getHome().getHeight(), parsed.getHome().getHeight());
57      }
58  }