Examples of BeanWriter


Examples of org.apache.commons.betwixt.io.BeanWriter

    public void testBeanWriter() throws Exception {
        Object bean = createBean();
       
        System.out.println( "Now trying pretty print" );
       
        BeanWriter writer = new BeanWriter();
        writer.setWriteEmptyElements(true);
        writer.enablePrettyPrint();
        writer.write( bean );
    }
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

   
   
    public void testLooping() throws Exception {
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        BeanWriter writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
       
        // logging for debugging jsut this method
//        SimpleLog log = new SimpleLog("[testLooping:BeanWriter]");
//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
//        writer.setLog(log);
       
//        log = new SimpleLog("[testLooping:AbstractBeanWriter]");
//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
//        writer.setAbstractBeanWriterLog(log);
       
//        log = new SimpleLog("[testLooping]");
//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
       
        writer.enablePrettyPrint();
        writer.write( LoopBean.createNoLoopExampleBean() );   
       
        String xml ="<?xml version='1.0'?><LoopBean><name>Root</name><friend><name>level1</name>"
                + "<friend><name>level2</name><friend><name>level3</name><friend><name>level4</name>"
                + "<friend><name>level5</name></friend></friend></friend></friend></friend></LoopBean>";
     
        xmlAssertIsomorphicContent(
                            "Test no loop",
                            parseString(out.getBuffer().toString()),
                            parseString(xml),
                            true);       
       
        out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
        writer.write( LoopBean.createLoopExampleBean() )
        xml ="<?xml version='1.0'?><LoopBean><name>Root</name><friend><name>level1</name>"
                + "<friend><name>level2</name><friend><name>level3</name><friend><name>level4</name>"
                + "<friend><name>level5</name><friend idref='1'/></friend></friend></friend>"
                + "</friend></friend></LoopBean>";
        xmlAssertIsomorphicContent(
                            "Test loop",
                            parseString(out.getBuffer().toString()),
                            parseString(xml),
                            true)
       
        // test not writing IDs

       
//        log.info("Writing LoopBean.createNoLoopExampleBean...");
       
        out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
        writer.setWriteIDs(false);
        writer.write( LoopBean.createNoLoopExampleBean() );
        xml ="<?xml version='1.0'?><LoopBean><name>Root</name><friend><name>level1</name><friend>"
            + "<name>level2</name><friend><name>level3</name><friend><name>level4</name><friend>"
            + "<name>level5</name></friend></friend>"
            + "</friend></friend></friend></LoopBean>";
               
        xmlAssertIsomorphicContent
                            "Test no loop, no ids",
                            parseString(out.getBuffer().toString()),
                            parseString(xml),
                            true);
       
//        log.info("Writing LoopBean.createIdOnlyLoopExampleBean...");
       
        out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
        writer.setWriteIDs(false);
        writer.write( LoopBean.createIdOnlyLoopExampleBean() );
        xml = "<?xml version='1.0'?><LoopBean><name>Root</name><friend><name>level1</name>"
            + "<friend><name>level2</name><friend><name>level3</name><friend><name>level4</name>"
            + "<friend><name>level5</name><friend><name>Root</name></friend></friend>"
            + "</friend></friend></friend></friend></LoopBean>";
               
        xmlAssertIsomorphicContent
                            "Test id only loop",
                            parseString(out.getBuffer().toString()),
                            parseString(xml),
                            true);
       
        try {  
//            log.info("Writing LoopBean.createLoopExampleBean...")
            out = new StringWriter();
            out.write("<?xml version='1.0'?>");
            writer = new BeanWriter(out);
            writer.setWriteEmptyElements( true );
            writer.setWriteIDs(false);
            writer.write( LoopBean.createLoopExampleBean() );  
            fail("CyclicReferenceException not thrown!");
           
        } catch (CyclicReferenceException e) {
            // everything's fine
        }
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

    public void testSimpleBean() throws Exception {
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0' encoding='UTF-8'?>");
//        SimpleLog log = new SimpleLog("[testSimpleBean:XMLIntrospector]");
//        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
        BeanWriter writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
//        writer.getXMLIntrospector().setLog(log);
       
//        log = new SimpleLog("[testSimpleBean:XMLIntrospectorHelper]");
//        XMLIntrospectorHelper.setLog(log);
   
        writer.setWriteIDs(false);
  SimpleTestBean bean = new SimpleTestBean("alpha-value","beta-value","gamma-value");
        writer.write(bean);
        out.flush();
        String xml = out.toString();
       
        xmlAssertIsomorphicContent(
                    parseFile("src/test/org/apache/commons/betwixt/dotbetwixt/simpletestone.xml"),
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

    public void testBadDotBetwixtNames() throws Exception {
        // this will work by testing that the output is well formed
       
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0' encoding='UTF-8'?>");
        BeanWriter writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
        writer.write(new BadDotBetwixtNamesBean("one", "two"));
       
//        System.out.println(out.toString());
       
        // this should fail if the output is not well formed
        parseString(out.toString());
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

    }
   
    public void testEscaping() throws Exception {
        //XXX find a way to automatically verify test
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BeanWriter writer = new BeanWriter(out);
        writer.setWriteEmptyElements( true );
        writer.enablePrettyPrint();
        XMLIntrospector introspector = new XMLIntrospector();
        introspector.setAttributesForPrimitives(true);
        writer.setXMLIntrospector(introspector);
        writer.write(new LoopBean("Escape<LessThan"));
        writer.write(new LoopBean("Escape>GreaterThan"));
        writer.write(new LoopBean("Escape&amphersand"));
        writer.write(new LoopBean("Escape'apostrophe"));
        writer.write(new LoopBean("Escape\"Quote"));
       
        CustomerBean bean = new CustomerBean();
        bean.setEmails( new String[] {
                                        "Escape<LessThan",
                                        "Escape>GreaterThan",
                                        "Escape&amphersand",
                                        "Escape'apostrophe",
                                        "Escape\"Quote"} );
                                       
        // The attribute value escaping needs test too..
        bean.setName("Escape<LessThan");
        AddressBean address = new AddressBean();
        address.setCode("Escape>GreaterThan");
        address.setCountry("Escape&amphersand");
        address.setCity("Escape'apostrophe");
        address.setStreet("Escape\"Quote");
        bean.setAddress(address);
       
        writer.write(bean);
        out.flush();
        String result = "<?xml version='1.0'?><beans>" + out.toString() + "</beans>";
       
        // check for the elemant content..
        assertTrue(result.indexOf("<email>Escape&lt;LessThan</email>") > -1 );
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

        Element element = new Element();
        element.setValue("<greeting>What Ho Jeeves!</greeting>");
       
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        BeanWriter writer = new BeanWriter(out);
        writer.getXMLIntrospector().setAttributesForPrimitives(false);
        writer.write(element);
       
        String expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
        String xml = out.getBuffer().toString();
        
        assertEquals(expected,xml);
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

        Element element = new Element();
        element.setValue("<greeting>What Ho Jeeves!</greeting>");
       
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        BeanWriter writer = new BeanWriter(out);
        writer.getXMLIntrospector().setAttributesForPrimitives(false);
        XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class);
        elementInfo.getElementDescriptor().getElementDescriptors()[0]
            .getOptions().addOption(MixedContentEncodingStrategy.ENCODING_OPTION_NAME, "CDATA")
        
        writer.write(element);
       
        String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
        String xml = out.getBuffer().toString();
        
        assertEquals(expected,xml);
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

   
    /** Test output of bean with mixed content */
    public void testMixedContent() throws Exception {
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0' encoding='UTF-8'?>");
        BeanWriter writer = new BeanWriter( out );
        writer.write( new MixedContentBean("First", "Last", "Always") );
       
        String xml = "<?xml version='1.0' encoding='UTF-8'?><foo version='1.0'>"
            + "<bar version='First'>Fiddle sticks<baa>Last</baa>Always</bar></foo>";
       
        xmlAssertIsomorphicContent(
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

        Element element = new Element();
        element.setValue("<greeting>What Ho Jeeves!</greeting>");
       
        StringWriter out = new StringWriter();
        out.write("<?xml version='1.0'?>");
        BeanWriter writer = new BeanWriter(out);
        writer.getXMLIntrospector().setAttributesForPrimitives(false);
        XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class);
        elementInfo.getElementDescriptor().getElementDescriptors()[0]
            .getOptions().addOption("org.apache.commons.betwixt.mixed-content-encoding", "escaped");
        writer.write(element);
       
        String expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
        String xml = out.getBuffer().toString();
        
        assertEquals(expected,xml);
View Full Code Here

Examples of org.apache.commons.betwixt.io.BeanWriter

    /**
     * Testing valid endofline characters.
     * It tests if there is a warning on System.err
     */
    public void testValidEndOfLine() throws Exception {
        BeanWriter writer = new BeanWriter();
        writer.setWriteEmptyElements(true);
       
        // store the system err
        PrintStream errStream = System.err;
        ByteArrayOutputStream warning = new ByteArrayOutputStream();
        System.setErr(new PrintStream(warning));
       
        // force logging to go to System.err
        writer.setLog( new SimpleLog( "test.betwixt" ) );
       
       
        writer.setEndOfLine("X");
        warning.flush();
        assertTrue(warning.toString().startsWith("[WARN]"));
        warning.reset();
        writer.setEndOfLine("\tX");
        warning.flush();
        assertTrue(warning.toString().startsWith("[WARN]"));
        warning.reset();
        // now test a valid value..
        writer.setEndOfLine(" ");
        warning.flush();
        assertTrue(warning.toString().equals(""));
        warning.reset();
        // set the System.err back again..
        System.setErr(errStream);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.