package com.fasterxml.jackson.jr.ob;

import java.util.*;

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

public class WriteBeansTest extends TestBase
{
    static class TestBean {
        public int getX() { return 1; }
        public void setX(int x) { }

        public int getY() { return 3; }
    }

    static class BinaryBean {
        protected final static byte[] STUFF = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
        
        public byte[] getStuff() {
            return STUFF;
        }
    }

    static class BeanBase {
        int _value;

        public int getValue() { return _value; }
        public void setValue(int v) { _value = v; }
    }

    static class BaseImpl extends BeanBase {
        int _extra;

        protected BaseImpl() { }
        public BaseImpl(int v, int x) {
            _value = v;
            _extra = x;
        }

        public int getExtra() { return _extra; }
        public void setExtra(int v) { _extra = v; }
        
    }
    
    public void testBinary() throws Exception
    {
        String json = JSON.std.asString(new BinaryBean());
        Map<String,Object> map = JSON.std.mapFrom(json);
        if (1 != map.size()) {
            fail("Wrong Map contents: "+json);
        }
        String base64 = (String) map.get("stuff");
        assertNotNull(base64);
        assertEquals("AQIDBAUGBw==", base64);
    }
    
    public void testSimpleMap() throws Exception
    {
        // first: with default settings, should serialize 2 props
        String json = JSON.std.asString(new TestBean());

        Map<String,Object> map = JSON.std.mapFrom(json);
        if ((2 != map.size())
                || !Integer.valueOf(1).equals(map.get("x"))
                || !Integer.valueOf(3).equals(map.get("y"))
                ){
            fail("Wrong Map contents (expected 'x' and 'y' for JSON: "+json);
        }

        // and then different configs
        json = JSON.std.without(Feature.WRITE_READONLY_BEAN_PROPERTIES)
                .asString(new TestBean());
        map = JSON.std.mapFrom(json);
        if ((1 != map.size())
                || !Integer.valueOf(1).equals(map.get("x"))
                ){
            fail("Wrong Map contents (expected just 'x' for JSON: "+json);
        }
    }

    // Make sure we handle stuff from base class too
    public void testMethodsFromSuperclass() throws Exception
    {
        String json = JSON.std.asString(new BaseImpl(1, 2));
        Map<String,Object> map = JSON.std.mapFrom(json);
        if ((2 != map.size())
                || !Integer.valueOf(1).equals(map.get("value"))
                || !Integer.valueOf(2).equals(map.get("extra"))
                ){
            fail("Wrong Map contents (expected 'value' and 'extra' for JSON: "+json);
        }

        BaseImpl result = JSON.std.beanFrom(BaseImpl.class,
                aposToQuotes("{ 'extra':5, 'value':-245 }"));
        assertEquals(5, result.getExtra());
        assertEquals(-245, result.getValue());
    }
}

@@ -966,24 +966,294 @@
       %0A    %7D
+%0A%0A    static class StringBean %7B%0A        public String value;%0A%0A        public StringBean(String v) %7B value = v; %7D%0A    %7D%0A%0A    static class StringBeanBean %7B%0A        public StringBean bean;%0A%0A        public StringBeanBean(StringBean b) %7B%0A            bean = b;%0A        %7D%0A    %7D
 %0A    %0A    pu
@@ -3257,11 +3257,867 @@
 );%0A    %7D
+%0A%0A    public void testBeanNulls() throws Exception%0A    %7B%0A        final JSON withNulls = JSON.std.with(JSON.Feature.WRITE_NULL_PROPERTIES);%0A        %0A        // by default, no nulls for either %22String%22 property%0A        assertEquals(%22%7B%7D%22, JSON.std.asString(new StringBean(null)));%0A        assertEquals(%22%7B%7D%22, JSON.std.asString(new StringBeanBean(null)));%0A        assertEquals(a2q(%22%7B'bean':%7B%7D%7D%22),%0A                JSON.std.asString(new StringBeanBean(new StringBean(null))));%0A%0A        // but we can make them appear%0A        assertEquals(a2q(%22%7B'value':null%7D%22),%0A                withNulls.asString(new StringBean(null)));%0A        assertEquals(a2q(%22%7B'bean':null%7D%22),%0A                withNulls.asString(new StringBeanBean(null)));%0A        assertEquals(a2q(%22%7B'bean':%7B'value':null%7D%7D%22),%0A                withNulls.asString(new StringBeanBean(new StringBean(null))));%0A    %7D
 %0A%7D%0A

Add bit more testing