1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml.ruby; 15 16 import junit.framework.TestCase; 17 import org.yaml.snakeyaml.DumperOptions; 18 import org.yaml.snakeyaml.TypeDescription; 19 import org.yaml.snakeyaml.Util; 20 import org.yaml.snakeyaml.Yaml; 21 import org.yaml.snakeyaml.constructor.Constructor; 22 import org.yaml.snakeyaml.nodes.Tag; 23 import org.yaml.snakeyaml.representer.Representer; 24 25 public class RubyTest extends TestCase { 26 testParse()27 public void testParse() { 28 TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml")); 29 assertNotNull(result); 30 assertEquals(0, result.getSub1().getAtt2()); 31 assertEquals("MyString", result.getSub2().getAtt1()); 32 assertEquals(1, result.getSub2().getAtt2().size()); 33 assertEquals(12345, result.getSub2().getAtt3()); 34 } 35 testEmitNoTags()36 public void testEmitNoTags() { 37 TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml")); 38 DumperOptions options = new DumperOptions(); 39 options.setExplicitStart(true); 40 Yaml yaml2 = new Yaml(options); 41 String output = yaml2.dumpAsMap(result); 42 assertFalse("No tags expected.", output.contains("Sub1")); 43 // System.out.println(output); 44 // parse back. Without tags it shall still work 45 Yaml beanLoader = new Yaml(); 46 TestObject result2 = beanLoader.loadAs(output, TestObject.class); 47 assertEquals(0, result2.getSub1().getAtt2()); 48 assertEquals("MyString", result2.getSub2().getAtt1()); 49 assertEquals(1, result2.getSub2().getAtt2().size()); 50 assertEquals(12345, result2.getSub2().getAtt3()); 51 } 52 testEmitWithTags()53 public void testEmitWithTags() { 54 TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml")); 55 DumperOptions options = new DumperOptions(); 56 options.setExplicitStart(true); 57 Representer repr = new Representer(); 58 repr.addClassTag(TestObject.class, new Tag("!ruby/object:Test::Module::Object")); 59 repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1")); 60 repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2")); 61 Yaml yaml2 = new Yaml(repr, options); 62 String output = yaml2.dump(result); 63 // System.out.println(output); 64 assertTrue("Tags must be present.", output.startsWith("--- !ruby/object:Test::Module::Object")); 65 assertTrue("Tags must be present: " + output, 66 output.contains("!ruby/object:Test::Module::Sub1")); 67 assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2")); 68 // parse back. 69 TestObject result2 = parseObject(output); 70 assertEquals(0, result2.getSub1().getAtt2()); 71 assertEquals("MyString", result2.getSub2().getAtt1()); 72 assertEquals(1, result2.getSub2().getAtt2().size()); 73 assertEquals(12345, result2.getSub2().getAtt3()); 74 } 75 testEmitWithTags2WithoutTagForParentJavabean()76 public void testEmitWithTags2WithoutTagForParentJavabean() { 77 TestObject result = parseObject(Util.getLocalResource("ruby/ruby1.yaml")); 78 DumperOptions options = new DumperOptions(); 79 options.setExplicitStart(true); 80 Representer repr = new Representer(); 81 repr.addClassTag(Sub1.class, new Tag("!ruby/object:Test::Module::Sub1")); 82 repr.addClassTag(Sub2.class, new Tag("!ruby/object:Test::Module::Sub2")); 83 Yaml yaml2 = new Yaml(repr, options); 84 String output = yaml2.dump(result); 85 // System.out.println(output); 86 assertTrue("Tags must be present.", 87 output.startsWith("--- !!org.yaml.snakeyaml.ruby.TestObject")); 88 assertTrue("Tags must be present: " + output, 89 output.contains("!ruby/object:Test::Module::Sub1")); 90 assertTrue("Tags must be present.", output.contains("!ruby/object:Test::Module::Sub2")); 91 // parse back. 92 TestObject result2 = parseObject(output); 93 assertEquals(0, result2.getSub1().getAtt2()); 94 assertEquals("MyString", result2.getSub2().getAtt1()); 95 assertEquals(1, result2.getSub2().getAtt2().size()); 96 assertEquals(12345, result2.getSub2().getAtt3()); 97 } 98 parseObject(String input)99 private TestObject parseObject(String input) { 100 Constructor con = new Constructor(TestObject.class); 101 con.addTypeDescription( 102 new TypeDescription(TestObject.class, "!ruby/object:Test::Module::Object")); 103 con.addTypeDescription(new TypeDescription(Sub1.class, "!ruby/object:Test::Module::Sub1")); 104 con.addTypeDescription(new TypeDescription(Sub2.class, "!ruby/object:Test::Module::Sub2")); 105 106 Yaml yaml = new Yaml(con); 107 return yaml.load(input); 108 } 109 } 110