package org.kohsuke.stapler.export;

import java.io.IOException;
import java.io.StringWriter;
import junit.framework.TestCase;

public class XMLDataWriterTest extends TestCase {

    public XMLDataWriterTest(String n) {
        super(n);
    }

    private static <T> String serialize(T bean, Class<T> clazz) throws IOException {
        StringWriter w = new StringWriter();
        Model<T> model = new ModelBuilder().get(clazz);
        model.writeTo(bean, Flavor.XML.createDataWriter(bean, w));
        return w.toString();
    }

    @ExportedBean public static class X {
        @Exported public String a = "aval";
        public String b = "bval";
        @Exported public String getC() {return "cval";}
        public String getD() {return "dval";}
    }
    public void testSimpleUsage() throws Exception {
        assertEquals("<x><a>aval</a><c>cval</c></x>",
                serialize(new X(), X.class));
    }

    @ExportedBean(defaultVisibility=2) public static abstract class Super {
        @Exported public String basic = "super";
        @Exported public abstract String generic();
    }
    public static class Sub extends Super {
        public String generic() {return "sub";}
        @Exported public String specific() {return "sub";}
    }
    @ExportedBean public static class Container {
        @Exported public Super polymorph = new Sub();
    }
    public void testInheritance() throws Exception {
        assertEquals("<container><polymorph><basic>super</basic><generic>sub</generic>" +
                "<specific>sub</specific></polymorph></container>",
                serialize(new Container(), Container.class));
    }

}

@@ -99,31 +99,144 @@
 rt j
-unit.framework.TestCase
+ava.io.StringReader;%0A%0Aimport junit.framework.TestCase;%0A%0Aimport javax.xml.stream.XMLInputFactory;%0Aimport javax.xml.stream.XMLStreamReader
 ;%0A%0Ap
@@ -1758,10 +1758,445 @@
 %0A    %7D%0A%0A
+    private void assertValidXML(String s) throws Exception %7B%0A        XMLStreamReader r = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(s));%0A        while (r.hasNext())%0A            r.next();%0A        r.close();%0A    %7D%0A%0A    /**%0A     * Can we write out anonymous classes as the root object?%0A     */%0A    public void testAnonymousClass() throws Exception %7B%0A        assertValidXML(serialize(new X() %7B%7D,X.class));%0A    %7D%0A%0A
 %7D%0A

test case for anonymous class handling.