aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/issues/issue10/BasicDumpTest.java
blob: a16c4e27e93e1e6429df6090b9850863fc8c4b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 * Copyright (c) 2008, http://www.snakeyaml.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.yaml.snakeyaml.issues.issue10;

import java.util.ArrayList;
import java.util.Iterator;

import junit.framework.TestCase;

import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;

public class BasicDumpTest extends TestCase {

    public void testTag() {
        DataSource base = new DataSource();
        JDBCDataSource baseJDBC = new JDBCDataSource();
        baseJDBC.setParent(base);

        ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
        // trying expected order first
        dataSources.add(base);
        dataSources.add(baseJDBC);

        DataSources ds = new DataSources();
        ds.setDataSources(dataSources);

        Yaml yaml = new Yaml();
        String output = yaml.dump(ds);

        String etalon = Util.getLocalResource("javabeans/issue10-1.yaml");
        assertEquals(etalon.trim(), output.trim());
        Object obj = yaml.load(output);
        DataSources dsOut = (DataSources) obj;
        Iterator<DataSource> iter = dsOut.getDataSources().iterator();
        assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource);
        assertTrue(iter.next() instanceof JDBCDataSource);
    }

    public void testTag2() {
        DataSource base = new DataSource();
        JDBCDataSource baseJDBC = new JDBCDataSource();
        baseJDBC.setParent(base);

        ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
        dataSources.add(base);
        dataSources.add(baseJDBC);

        DataSources ds = new DataSources();
        ds.setDataSources(dataSources);

        Yaml yaml = new Yaml();
        String output = yaml.dumpAsMap(ds);

        String etalon = Util.getLocalResource("javabeans/issue10-2.yaml");
        assertEquals(etalon.trim(), output.trim());
    }

    /**
     * different order does not require the global tag
     */
    public void testTag3() {
        DataSource base = new DataSource();
        JDBCDataSource baseJDBC = new JDBCDataSource();
        baseJDBC.setParent(base);

        ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
        dataSources.add(baseJDBC);
        dataSources.add(base);

        DataSources ds = new DataSources();
        ds.setDataSources(dataSources);

        Yaml yaml = new Yaml();
        String output = yaml.dumpAsMap(ds);

        String etalon = Util.getLocalResource("javabeans/issue10-3.yaml");
        assertEquals(etalon.trim(), output.trim());
        // load
        Yaml beanLoader = new Yaml();
        DataSources bean = beanLoader.loadAs(output, DataSources.class);
        Iterator<DataSource> iter = bean.getDataSources().iterator();
        assertTrue(iter.next() instanceof JDBCDataSource);
        assertFalse("Must be DataSource.", iter.next() instanceof JDBCDataSource);
    }
}