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.ruby;
18  
19  import junit.framework.TestCase;
20  
21  import org.yaml.snakeyaml.DumperOptions;
22  import org.yaml.snakeyaml.TypeDescription;
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  import org.yaml.snakeyaml.constructor.Constructor;
26  import org.yaml.snakeyaml.nodes.Tag;
27  import org.yaml.snakeyaml.representer.Representer;
28  
29  public class RubyTest extends TestCase {
30  
31      public void testParse() {
32          TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
33          assertNotNull(result);
34          assertEquals(0, result.getSub1().getAtt2());
35          assertEquals("MyString", result.getSub2().getAtt1());
36          assertEquals(1, result.getSub2().getAtt2().size());
37          assertEquals(12345, result.getSub2().getAtt3());
38      }
39  
40      public void testEmitNoTags() {
41          TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
42          DumperOptions options = new DumperOptions();
43          options.setExplicitStart(true);
44          Yaml yaml2 = new Yaml(options);
45          String output = yaml2.dumpAsMap(result);
46          assertFalse("No tags expected.", output.contains("Sub1"));
47          // System.out.println(output);
48          // parse back. Without tags it shall still work
49          Yaml beanLoader = new Yaml();
50          TestObject result2 = beanLoader.loadAs(output, TestObject.class);
51          assertEquals(0, result2.getSub1().getAtt2());
52          assertEquals("MyString", result2.getSub2().getAtt1());
53          assertEquals(1, result2.getSub2().getAtt2().size());
54          assertEquals(12345, result2.getSub2().getAtt3());
55      }
56  
57      public void testEmitWithTags() {
58          TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
59          DumperOptions options = new DumperOptions();
60          options.setExplicitStart(true);
61          Representer repr = new Representer();
62          repr.addClassTag(TestObject.class, new Tag("!ruby/object:Test::Module::Object"));
63          repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1"));
64          repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2"));
65          Yaml yaml2 = new Yaml(repr, options);
66          String output = yaml2.dump(result);
67          // System.out.println(output);
68          assertTrue("Tags must be present.",
69                  output.startsWith("--- !ruby/object:Test::Module::Object"));
70          assertTrue("Tags must be present: " + output,
71                  output.contains("!ruby/object:Test::Module::Sub1"));
72          assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2"));
73          // parse back.
74          TestObject result2 = parseObject(output);
75          assertEquals(0, result2.getSub1().getAtt2());
76          assertEquals("MyString", result2.getSub2().getAtt1());
77          assertEquals(1, result2.getSub2().getAtt2().size());
78          assertEquals(12345, result2.getSub2().getAtt3());
79      }
80  
81      public void testEmitWithTags2WithoutTagForParentJavabean() {
82          TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml"));
83          DumperOptions options = new DumperOptions();
84          options.setExplicitStart(true);
85          Representer repr = new Representer();
86          repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1"));
87          repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2"));
88          Yaml yaml2 = new Yaml(repr, options);
89          String output = yaml2.dump(result);
90          // System.out.println(output);
91          assertTrue("Tags must be present.",
92                  output.startsWith("--- !!org.yaml.snakeyaml.ruby.TestObject"));
93          assertTrue("Tags must be present: " + output,
94                  output.contains("!ruby/object:Test::Module::Sub1"));
95          assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2"));
96          // parse back.
97          TestObject result2 = parseObject(output);
98          assertEquals(0, result2.getSub1().getAtt2());
99          assertEquals("MyString", result2.getSub2().getAtt1());
100         assertEquals(1, result2.getSub2().getAtt2().size());
101         assertEquals(12345, result2.getSub2().getAtt3());
102     }
103 
104     private TestObject parseObject(String input) {
105         Constructor con = new Constructor(TestObject.class);
106         con.addTypeDescription(new TypeDescription(TestObject.class,
107                 "!ruby/object:Test::Module::Object"));
108         con.addTypeDescription(new TypeDescription(Sub1.class, "!ruby/object:Test::Module::Sub1"));
109         con.addTypeDescription(new TypeDescription(Sub2.class, "!ruby/object:Test::Module::Sub2"));
110 
111         Yaml yaml = new Yaml(con);
112         return (TestObject) yaml.load(input);
113     }
114 }