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