package com.fasterxml.jackson.jr.ob;

import java.io.File;
import java.net.URI;
import java.util.*;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSON.Feature;

public class WriteSimpleTest extends TestBase
{
    final static class POJO {
        public int value = 3;
    }

    enum ABC { A, B, C; }
    
    /*
    /**********************************************************************
    /* Test methdods
    /**********************************************************************
     */
    
    public void testSimpleList() throws Exception
    {
        List<Object> stuff = new LinkedList<Object>();
        stuff.add("x");
        stuff.add(true);
        stuff.add(123);
        assertEquals("[\"x\",true,123]", JSON.std.asString(stuff));
    }

    public void testSimpleMap() throws Exception
    {
        Map<String,Object> stuff = new LinkedHashMap<String,Object>();
        stuff.put("a", 15);
        stuff.put("b", Boolean.TRUE);
        stuff.put("c", "foobar");
        
        assertEquals("{\"a\":15,\"b\":true,\"c\":\"foobar\"}",
                JSON.std.asString(stuff));
    }

    public void testSimpleIntContainers() throws Exception {
        assertEquals("[1,2,3]", JSON.std.asString(new int[] { 1, 2, 3 }));
        assertEquals("[1,2,3]", JSON.std.asString(new Integer[] { 1, 2, 3 }));
        List<Integer> list = new ArrayList<Integer>();
        list.add(4);
        list.add(-8);
        assertEquals("[4,-8]", JSON.std.asString(list));
    }

    public void testSimpleBooleanArray() throws Exception {
        assertEquals("[true,false]", JSON.std.asString(new boolean[] { true, false }));
        assertEquals("[true,false]", JSON.std.asString(new Boolean[] { true, false }));
    }
    
    public void testSimpleStringArray() throws Exception {
        assertEquals(aposToQuotes("['abc','def']"), JSON.std.asString(new String[] { "abc", "def" }));
    }
    
    public void testNest() throws Exception
    {
        Map<String,Object> stuff = new LinkedHashMap<String,Object>();
        List<Integer> list = new ArrayList<Integer>();
        list.add(123);
        list.add(456);
        stuff.put("first", list);
        Map<String,Object> second = new LinkedHashMap<String,Object>();
        stuff.put("second", second);
        second.put("foo", "bar");
        second.put("bar", new ArrayList<Object>());

        assertEquals("{\"first\":[123,456],\"second\":{\"foo\":\"bar\",\"bar\":[]}}",
                JSON.std.asString(stuff));
    }

    public void testKnownSimpleTypes() throws Exception
    {
        final String URL_STR = "http://fasterxml.com";
        assertEquals(quote(URL_STR),
                JSON.std.asString(new URI(URL_STR)));
        final String PATH = "/foo/bar.txt";
        assertEquals(quote(PATH),
                JSON.std.asString(new File(PATH)));

        assertEquals(quote("B"), JSON.std.asString(ABC.B));
        assertEquals("1", JSON.std.with(Feature.WRITE_ENUMS_USING_INDEX).asString(ABC.B));
    }

    public void testUnnownType() throws Exception
    {
        try {
            String json = JSON.std.with(JSON.Feature.FAIL_ON_UNKNOWN_TYPE_WRITE)
                    .without(JSON.Feature.HANDLE_JAVA_BEANS)
                    .asString(new POJO());
            fail("Should have failed: instead got: "+json);
        } catch (Exception e) {
            verifyException(e, "unrecognized type");
            verifyException(e, "POJO");
        }
    }
}

@@ -297,38 +297,223 @@
  3;%0A
-    %7D%0A%0A    enum ABC %7B A, B, C;
+%0A        public POJO() %7B %7D%0A        public POJO(int v) %7B value = v; %7D%0A    %7D%0A%0A    enum ABC %7B A, B, C; %7D%0A%0A    final static class Address %7B%0A        public String name;%0A%0A        public Address(String n) %7B name = n; %7D%0A   
  %7D%0A 
@@ -3220,16 +3220,17 @@
 d testUn
+k
 nownType
@@ -3654,11 +3654,713 @@
  %7D%0A    %7D
+%0A%0A    // For %5BIssue#16%5D%0A    public void testTypedMaps() throws Exception%0A    %7B%0A        final Address from = new Address(%22xyz%22);%0A        final Map%3CString,Set%3CAddress%3E%3E to = new HashMap%3CString,Set%3CAddress%3E%3E();%0A        to.put(%22static_addr%22, new HashSet%3CAddress%3E());%0A        to.get(%22static_addr%22).add(new Address(%22abc%22));%0A%0A        final   Map%3CString,Object%3E temp = new HashMap%3CString,Object%3E();%0A        temp.put(%22from%22, from);%0A        temp.put(%22TO%22, to);%0A%0A        String json = JSON.std.asString(temp);%0A%0A        assertNotNull(json);%0A        %0A        // and sanity check for back direction%0A        Map%3C?,?%3E map = JSON.std.mapFrom(json);%0A        assertNotNull(map);%0A        assertEquals(2, map.size());%0A    %7D
 %0A%7D%0A

Add unit test for #16