package org.yaml.snakeyaml.constructor;

import java.util.LinkedHashMap;
import java.util.TimeZone;

import junit.framework.TestCase;

import org.yaml.snakeyaml.composer.Composer;
import org.yaml.snakeyaml.parser.Parser;
import org.yaml.snakeyaml.parser.ParserImpl;
import org.yaml.snakeyaml.reader.Reader;
import org.yaml.snakeyaml.resolver.Resolver;
import org.yaml.snakeyaml.scanner.Scanner;
import org.yaml.snakeyaml.scanner.ScannerImpl;

public class ConstructorTest extends TestCase {

    public void testGetSingleData() {
        String data = "american:\n  - Boston Red Sox";
        Object map = construct(data);
        assertNotNull(map);
        assertTrue(map.getClass().toString(), map instanceof LinkedHashMap);
    }

    // TODO fix test
    public void qtestGetBean() {
        String data = "--- !java/object:org.yaml.snakeyaml.constructor.Person\nfirstName: Andrey\nage: 99";
        Object obj = construct(data);
        assertNotNull(obj);
        assertTrue(obj.getClass().toString(), obj instanceof Person);
        Person person = (Person) obj;
        assertEquals("Andrey", person.getFirstName());
        assertNull(person.getLastName());
        assertEquals(99, person.getAge());
    }

    // TODO fix test
    public void qtestGetBeanAssumeClass() {
        String data = "--- !org.yaml.snakeyaml.constructor.Person\nfirstName: Andrey\nage: 99";
        Object obj = construct(data);
        assertNotNull(obj);
        assertTrue("Unexpected: " + obj.getClass().toString(), obj instanceof Person);
        Person person = (Person) obj;
        assertEquals("Andrey", person.getFirstName());
        assertNull(person.getLastName());
        assertEquals(99, person.getAge());
    }

    /**
     * create instance from constructor
     */
    // TODO fix test
    public void qtestGetConstructorBean() {
        String data = "--- !java/object:org.yaml.snakeyaml.constructor.Person [ Andrey, Somov, 99 ]";
        Object obj = construct(data);
        assertNotNull(obj);
        assertTrue(obj.getClass().toString(), obj instanceof Person);
        Person person = (Person) obj;
        assertEquals("Andrey", person.getFirstName());
        assertNull(person.getLastName());
        assertEquals(99, person.getAge());
    }

    // TODO fix test
    public void qtestJavaBeanLoad() {
        final java.util.Calendar cal = java.util.Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        cal.clear();
        cal.set(1982, 5 - 1, 3); // Java's months are zero-based...

        final TestBean expected = new TestBean("Ola Bini", 24, cal.getTime());
        assertEquals(
                expected,
                construct("--- !java/object:org.jvyaml.TestBean\nname: Ola Bini\nage: 24\nborn: 1982-05-03\n"));
    }

    private Object construct(String data) {
        Reader reader = new Reader(data);
        Scanner scanner = new ScannerImpl(reader);
        Parser parser = new ParserImpl(scanner);
        Resolver resolver = new Resolver();
        Composer composer = new Composer(parser, resolver);
        Constructor constructor = new Constructor();
        constructor.setComposer(composer);
        Object result = constructor.getSingleData();
        return result;
    }
}

@@ -53,26 +53,77 @@
 va.util.
-LinkedHash
+Iterator;%0D%0Aimport java.util.LinkedHashMap;%0D%0Aimport java.util.
 Map;%0D%0Aim
@@ -549,24 +549,60 @@
 stCase %7B%0D%0A%0D%0A
+    @SuppressWarnings(%22unchecked%22)%0D%0A
     public v
@@ -613,21 +613,16 @@
 test
-GetSingleData
+MapOrder
 () %7B
@@ -650,37 +650,49 @@
  = %22
-american:%5Cn  - Boston Red Sox
+one: zzz%5Cntwo: ccc%5Cnthree: bbb%5Cnfour: aaa
 %22;%0D%0A
@@ -833,24 +833,360 @@
 dHashMap);%0D%0A
+        Map%3CString, String%3E m = (Map%3CString, String%3E) map;%0D%0A        assertEquals(4, m.keySet().size());%0D%0A        Iterator%3CString%3E iter = m.keySet().iterator();%0D%0A        assertEquals(%22one%22, iter.next());%0D%0A        assertEquals(%22two%22, iter.next());%0D%0A        assertEquals(%22three%22, iter.next());%0D%0A        assertEquals(%22four%22, iter.next());%0D%0A
     %7D%0D%0A%0D%0A   

test map order