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