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;
17b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
18b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.ArrayList;
19b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.List;
20b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.Map;
21b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.TreeMap;
22b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
23b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport junit.framework.TestCase;
24b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
25b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpublic class CollectionWithBeanYamlTest extends TestCase {
26b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
27b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    @SuppressWarnings("unchecked")
28b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public void testYamlMap() {
29b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Map<String, Bean> data = new TreeMap<String, Bean>();
30b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        data.put("gold1", new Bean());
31b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        data.put("gold2", new Bean());
32b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
33b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Yaml yaml = new Yaml();
34b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String output = yaml.dump(data);
35b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(
36b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov                "gold1: !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: ''}\ngold2: !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: ''}\n",
37b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov                output);
38b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Object o = yaml.load(output);
39b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
40b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue(o instanceof Map);
41b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Map<String, Bean> m = (Map<String, Bean>) o;
42b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue(m.get("gold1") instanceof Bean);
43b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue("" + m.get("gold2").getClass(), m.get("gold2") instanceof Bean);
44b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
45b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
46b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    @SuppressWarnings("unchecked")
47b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public void testYamlList() {
48b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        List<Bean> data = new ArrayList<Bean>();
49b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        data.add(new Bean("1"));
50b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        data.add(new Bean("2"));
51b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
52b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Yaml yaml = new Yaml();
53b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String output = yaml.dump(data);
54b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(
55b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov                "- !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: '1'}\n- !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: '2'}\n",
56b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov                output);
57b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Object o = yaml.load(output);
58b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
59b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue(o instanceof List);
60b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        List<Bean> m = (List<Bean>) o;
61b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(2, m.size());
62b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue(m.get(0) instanceof Bean);
63b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue(m.get(1) instanceof Bean);
64b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
65b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
66b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public static class Bean {
67b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        private String a;
68b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
69b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public Bean() {
70b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            a = "";
71b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
72b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
73b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public Bean(String value) {
74b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            a = value;
75b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
76b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
77b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public String getA() {
78b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            return a;
79b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
80b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
81b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public void setA(String s) {
82b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            a = s;
83b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
84b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
85b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov}
86