package org.kohsuke.stapler;

import junit.framework.TestCase;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import java.util.Collections;

/**
 * Tests the instantiation of nested objects.
 *
 * @author Kohsuke Kawaguchi
 */
public class NestedJsonTest extends TestCase {
    public static final class Foo {
        public Bar bar;

        // we test this with manual .stapler file
        // @DataBoundConstructor
        public Foo(Bar bar) {
            this.bar = bar;
        }
    }

    public static interface Bar {}

    public static final class BarImpl implements Bar {
        public final int i;

        // we test this with manual .stapler file
        // @DataBoundConstructor
        public BarImpl(int i) {
            this.i = i;
        }
    }

    public void testCreateObject() throws Exception {
        Foo o = createRequest().bindJSON(Foo.class, createDataSet());

        assertTrue(o!=null);
        assertTrue(o.bar instanceof BarImpl);
        assertEquals(123, ((BarImpl)o.bar).i);
    }

    public void testInstanceFill() throws Exception {
        Foo o = new Foo(null);
        createRequest().bindJSON(o, createDataSet());
        
        assertTrue(o.bar instanceof BarImpl);
        assertEquals(123, ((BarImpl)o.bar).i);
    }

    private RequestImpl createRequest() throws Exception {
        return new RequestImpl(createStapler(), new MockRequest(), Collections.EMPTY_LIST,null);
    }

    private JSONObject createDataSet() {
        JSONObject bar = new JSONObject();
        bar.put("i",123);
        JSONObject foo = new JSONObject();
        foo.put("bar",bar);
        foo.getJSONObject("bar").put("stapler-class", BarImpl.class.getName());
        return foo;
    }

    private Stapler createStapler() throws ServletException {
        Stapler stapler = new Stapler();
        stapler.init(new ServletConfigImpl());
        return stapler;
    }
}

@@ -56,16 +56,46 @@
 stCase;%0A
+import net.sf.json.JSONArray;%0A
 import n
@@ -117,16 +117,16 @@
 Object;%0A
-
 %0Aimport 
@@ -186,16 +186,39 @@
 ections;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -982,20 +982,17 @@
 sert
-True(o!=n
+NotN
 ull
+(o
 );%0A 
@@ -1276,32 +1276,32 @@
 nceof BarImpl);%0A
-
         assertEq
@@ -1330,32 +1330,735 @@
 bar).i);%0A    %7D%0A%0A
+    public void testCreateList() throws Exception %7B%0A        // Just one%0A        List%3CFoo%3E list = createRequest().bindJSONToList(Foo.class, createDataSet());%0A        assertNotNull(list);%0A        assertEquals(1, list.size());%0A        assertTrue(list.get(0).bar instanceof BarImpl);%0A        assertEquals(123, ((BarImpl)list.get(0).bar).i);%0A%0A        // Longer list%0A        JSONArray data = new JSONArray();%0A        data.add(createDataSet());%0A        data.add(createDataSet());%0A        data.add(createDataSet());%0A        list = createRequest().bindJSONToList(Foo.class, data);%0A        assertNotNull(list);%0A        assertEquals(3, list.size());%0A        assertEquals(123, ((BarImpl)list.get(2).bar).i);%0A    %7D%0A%0A
     private Requ

add testCreateList