/* Copyright (c) 2005 Ricebridge. All Rights Reserved.
 *
 * This file is available under the terms and conditions of the
 * Ricebridge "Open Source API" policy; Ricebridge grants use of this
 * copyrighted work under the terms of a BSD-style license only. See
 * http://www.opensource.org/licenses/bsd-license.php for more
 * information.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *
 *  - Neither the name of the Ricebridge nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.  
 */

package com.ricebridge.xmlman.test;


import com.ricebridge.xmlman.*;
import com.ricebridge.xmlman.in.*;
import com.ricebridge.xmlman.in.test.*;
import com.ricebridge.data.sc.*;

import com.ricebridge.data.BeanSpec;

import org.jostraca.util.*;

import junit.framework.*;
import junit.textui.*;

import org.xml.sax.InputSource;

import java.util.*;
import java.io.*;
import java.text.*;


/** Test cases for {@link com.ricebridge.xmlman.XmlManager} Beans handling.
 *    <p>The <b><a href="SaveBeansTest.java.html">Source Code</a></b> of this Java class 
 *    is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p>
 */
public class SaveBeansTest extends TestCase {

  // test framework
  
  public SaveBeansTest( String pName ) {
    super( pName );
  }

  public static TestSuite suite() {
    return new TestSuite( SaveBeansTest.class );
  }

  public static void main( String[] pArgs ) {
    TestRunner.run( suite() );
  }



  // test cases

  public void testExample() throws Exception {
    File xmlFile = null;

    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
    XmlManager xmlManager = new XmlManager();

    RecordSpec rs = new RecordSpec( "/root/record", 
                                    new String[] {"@name","foo","bar"},
                                    new String[] {"Name","Foo","Bar"} );

    BeanSpec bs = new BeanSpec( BeanRecord.class );

    ArrayList beans = new ArrayList();
    beans.add( new BeanRecord("r1","f1","b1") );
    beans.add( new BeanRecord("r2","f2","b2") );
    beans.add( new BeanRecord("r3","f3","b3") );

    xmlManager.saveBeans( xmlFile, rs, bs, beans );

    assertEquals( "<root>\n  <record name=\"r1\">\n    <foo>f1</foo>\n    <bar>b1</bar>\n  </record>\n  <record name=\"r2\">\n    <foo>f2</foo>\n    <bar>b2</bar>\n  </record>\n  <record name=\"r3\">\n    <foo>f3</foo>\n    <bar>b3</bar>\n  </record>\n</root>", 
                  FileUtil.readFile(xmlFile) );

  }



  public void testConversions() throws Exception {
    File xmlFile = null;

    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
    XmlManager xmlManager = new XmlManager();

    RecordSpec rs = new RecordSpec( "/root/record", 
                                    new String[] {"@name","int","short","long","float","double","boolean","char","byte","string","date"},
                                    new String[] {"Name","Int","Short","Long","Float","Double","Boolean","Char","Byte","String","Date"} );

    HashMap scm = new HashMap();
    scm.put( "Date", new DateConverter() );

    BeanSpec bs = new BeanSpec( ConvRecord.class, scm );

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    List beans = new ArrayList();
    beans.add( new ConvRecord( "r1",1,(short)10,100L,(float)1.1,(double)1.01,true,'a',(byte)1,"s1",sdf.parse("2005-01-01") ) );
    beans.add( new ConvRecord( "r2",2,(short)20,200L,(float)2.2,(double)2.02,true,'a',(byte)2,"s2",sdf.parse("2005-02-02") ) );
    beans.add( new ConvRecord( "r3",3,(short)30,300L,(float)3.3,(double)3.03,true,'a',(byte)3,"s3",sdf.parse("2005-03-03") ) );

    XmlSpec xs = xmlManager.getXmlSpec();
    xs.setIndent(false);

    xmlManager.saveBeans( xmlFile, rs, bs, beans );

    assertEquals( "<root><record name=\"r1\"><int>1</int><short>10</short><long>100</long><float>1.1</float><double>1.01</double><boolean>true</boolean><char>a</char><byte>1</byte><string>s1</string><date>2005-01-01</date></record><record name=\"r2\"><int>2</int><short>20</short><long>200</long><float>2.2</float><double>2.02</double><boolean>true</boolean><char>a</char><byte>2</byte><string>s2</string><date>2005-02-02</date></record><record name=\"r3\"><int>3</int><short>30</short><long>300</long><float>3.3</float><double>3.03</double><boolean>true</boolean><char>a</char><byte>3</byte><string>s3</string><date>2005-03-03</date></record></root>", FileUtil.readFile(xmlFile) );
  }



  public void testSingle() throws Exception {
    ArrayList beans = new ArrayList();
    beans.add( new SingleBean("b1","q1") );
    beans.add( new SingleBean("b2","q2") );

    String savecanon = "<foo><bar name=\"b1\"><que>q1</que></bar><bar name=\"b2\"><que>q2</que></bar></foo>";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );
    XmlManager xmlManager = new XmlManager();
    XmlSpec    xs         = xmlManager.getXmlSpec();
    xs.setIndent(false);

    RecordSpec rs = new RecordSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );
    BeanSpec bs = new BeanSpec( SingleBean.class );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile, rs, bs, beans );
    assertEquals( savecanon, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile.getAbsolutePath(), rs, bs, beans );
    assertEquals( savecanon, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    OutputStream is = new FileOutputStream( xmlFile );
    xmlManager.saveBeans( is, rs, bs, beans );
    assertEquals( savecanon, FileUtil.readFile(xmlFile) );
    is.close();

    String xmlstr = xmlManager.saveBeansToString( rs, bs, beans );
    assertEquals( savecanon, xmlstr );
  }


  
  public void testRepeat() throws Exception {
    ArrayList beans1 = new ArrayList();
    beans1.add( new SingleBean("b1","q1") );
    beans1.add( new SingleBean("b2","q2") );
    String savecanon1 = "<foo><bar name=\"b1\"><que>q1</que></bar><bar name=\"b2\"><que>q2</que></bar></foo>";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/save.xml" );

    RecordSpec rs1 = new RecordSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );
    BeanSpec bs = new BeanSpec( SingleBean.class );
    XmlManager xmlManager = new XmlManager( rs1 );
    xmlManager.getXmlSpec().setIndent(false);

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile, bs, beans1 );
    assertEquals( savecanon1, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile.getAbsolutePath(), bs, beans1 );
    assertEquals( savecanon1, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    OutputStream os = new FileOutputStream( xmlFile );
    xmlManager.saveBeans( os, bs, beans1 );
    assertEquals( savecanon1, FileUtil.readFile(xmlFile) );
    os.close();

    String xmlstr = xmlManager.saveBeansToString( bs, beans1 );
    assertEquals( savecanon1, xmlstr );


    ArrayList beans2 = new ArrayList();
    beans2.add( new SingleBean("2b1","2q1") );
    beans2.add( new SingleBean("2b2","2q2") );
    beans2.add( new SingleBean("2b3","2q3") );
    String savecanon2 = "<foo><bar name=\"2b1\"><que>2q1</que></bar><bar name=\"2b2\"><que>2q2</que></bar>"
      +"<bar name=\"2b3\"><que>2q3</que></bar></foo>";

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile, bs, beans2 );
    assertEquals( savecanon2, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile.getAbsolutePath(), bs, beans2 );
    assertEquals( savecanon2, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    os = new FileOutputStream( xmlFile );
    xmlManager.saveBeans( os, bs, beans2 );
    assertEquals( savecanon2, FileUtil.readFile(xmlFile) );
    os.close();

    xmlstr = xmlManager.saveBeansToString( bs, beans2 );
    assertEquals( savecanon2, xmlstr );


    ArrayList beans3 = new ArrayList();
    beans3.add( new SingleBean("3b1","3q1") );
    beans3.add( new SingleBean("3b2","3q2") );
    beans3.add( new SingleBean("3b3","3q3") );
    beans3.add( new SingleBean("3b4","3q4") );
    String savecanon3 = "<foo><bar name=\"3b1\"><que>3q1</que></bar><bar name=\"3b2\"><que>3q2</que></bar>"
      +"<bar name=\"3b3\"><que>3q3</que></bar><bar name=\"3b4\"><que>3q4</que></bar></foo>";

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile, bs, beans3 );
    assertEquals( savecanon3, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    xmlManager.saveBeans( xmlFile.getAbsolutePath(), bs, beans3 );
    assertEquals( savecanon3, FileUtil.readFile(xmlFile) );

    FileUtil.writeFile(xmlFile,"");
    os = new FileOutputStream( xmlFile );
    xmlManager.saveBeans( os, bs, beans3 );
    assertEquals( savecanon3, FileUtil.readFile(xmlFile) );
    os.close();

    xmlstr = xmlManager.saveBeansToString( bs, beans3 );
    assertEquals( savecanon3, xmlstr );
  }






  private String canon( List pBeans ) throws Exception {
    return pBeans.toString();
  }



  public static class BeanRecord {
    private String iName;
    private String iFoo;
    private String iBar;

    public BeanRecord( String pName, String pFoo, String pBar ) {
      iName = pName;
      iFoo  = pFoo;
      iBar  = pBar;
    }
    
    public void setName( String pName ) { iName = pName; }
    public String getName() { return iName; }
    public void setFoo( String pFoo ) { iFoo = pFoo; }
    public String getFoo() { return iFoo; }
    public void setBar( String pBar ) { iBar = pBar; }
    public String getBar() { return iBar; }

    public String toString() {
      return iName+":"+iFoo+":"+iBar;
    }
  }


  public static class ConvRecord {
    private String  iName;
    private int     iInt;
    private short   iShort;
    private long    iLong;
    private float   iFloat;
    private double  iDouble;
    private boolean iBoolean;
    private char    iChar;
    private byte    iByte;
    private String  iString;
    private Date    iDate;


    public ConvRecord( String  pName, 
                       int     pInt,
                       short   pShort,
                       long    pLong,
                       float   pFloat,
                       double  pDouble,
                       boolean pBoolean,
                       char    pChar,
                       byte    pByte,
                       String  pString,
                       Date    pDate ) {
      iName    = pName;
      iInt     = pInt;
      iShort   = pShort;
      iLong    = pLong;
      iFloat   = pFloat;
      iDouble  = pDouble;
      iBoolean = pBoolean;
      iChar    = pChar;
      iByte    = pByte;
      iString  = pString;
      iDate    = pDate;
    }
    
    public void setName( String pName ) { iName = pName; }
    public String getName() { return iName; }

    public void setInt( int pInt ) { iInt = pInt; }
    public int getInt() { return iInt; }

    public void setShort( short pShort ) { iShort = pShort; }
    public short getShort() { return iShort; }

    public void setLong( long pLong ) { iLong = pLong; }
    public long getLong() { return iLong; }

    public void setFloat( float pFloat ) { iFloat = pFloat; }
    public float getFloat() { return iFloat; }

    public void setDouble( double pDouble ) { iDouble = pDouble; }
    public double getDouble() { return iDouble; }

    public void setBoolean( boolean pBoolean ) { iBoolean = pBoolean; }
    public boolean getBoolean() { return iBoolean; }

    public void setChar( char pChar ) { iChar = pChar; }
    public char getChar() { return iChar; }

    public void setByte( byte pByte ) { iByte = pByte; }
    public byte getByte() { return iByte; }

    public void setString( String pString ) { iString = pString; }
    public String getString() { return iString; }

    public void setDate( Date pDate ) { iDate = pDate; }
    public Date getDate() { return iDate; }

    public String toString() {
      return ""+iName+":"+iInt+":"+iShort+":"+iLong+":"+iFloat+":"+iDouble+":"+iBoolean+":"+iChar+":"+iByte+":"+iString+":"+iDate;
    }
  }


  public static class DateConverter extends DefaultStringConverter {
    private Date iDefault = new Date();
    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    protected Object makeObjectImpl( String pValue ) throws Exception {
      return df.parse(pValue);
    }
    protected Object makeDefaultObjectImpl() {
      return iDefault;
    }
    protected String makeStringImpl( Object pValue ) throws Exception {
      return df.format((Date)pValue);
    }
    protected String makeDefaultStringImpl() {
      return iDefault.toString();
    }
  }


  public static class SingleBean {
    private String iName;
    private String iQue;
    
    
    public SingleBean( String pName, String pQue ) {
      iName = pName;
      iQue  = pQue;
    }


    public void setName( String pName ) { iName = pName; }
    public String getName() { return iName; }
    public void setQue( String pQue ) { iQue = pQue; }
    public String getQue() { return iQue; }

    public String toString() {
      return iName+":"+iQue;
    }
  }

}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Thursday, February 23 2006 at 16:47