/*
 * See LICENSE file in distribution for copyright and licensing information.
 */
package org.yaml.snakeyaml.issues.issue10;

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

import junit.framework.TestCase;

import org.yaml.snakeyaml.JavaBeanDumper;
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);

        JavaBeanDumper yaml = new JavaBeanDumper();
        String output = yaml.dump(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);

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

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

@@ -253,16 +253,58 @@
 Dumper;%0A
+import org.yaml.snakeyaml.JavaBeanLoader;%0A
 import o
@@ -2630,20 +2630,401 @@
 ut.trim());%0A
+        // load%0A        JavaBeanLoader%3CDataSources%3E beanLoader = new JavaBeanLoader%3CDataSources%3E(DataSources.class);%0A        DataSources bean = beanLoader.load(output);%0A        Iterator%3CDataSource%3E iter = bean.getDataSources().iterator();%0A        assertTrue(iter.next() instanceof JDBCDataSource);%0A        assertFalse(%22Must be DataSource.%22, iter.next() instanceof JDBCDataSource);%0A
     %7D%0A%7D%0A

improve BasicDumpTest