/* 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.BeanSpec;
import com.ricebridge.data.sc.DefaultStringConverter;

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>Test files are <a href="load.xml">load.xml</a>, <a href="load02.xml">load02.xml</a>, <a href="load03.xml">load03.xml</a>,
 *  <a href="beans.xml">beans.xml</a> and <a href="example.xml">example.xml</a>.</p>
 *    <p>The <b><a href="LoadBeansTest.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 LoadBeansTest extends TestCase {

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

  public static TestSuite suite() {
    return new TestSuite( LoadBeansTest.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/example.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 );

    List beans = xmlManager.loadBeans( xmlFile, rs, bs );
    assertEquals( "[r1:f1:b1, r2:f2:b2, r3:f3:b3]", beans.toString() );
  }


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

    xmlFile = TestUtil.findFile( "com/ricebridge/xmlman/test/beans.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 );

    List beans = xmlManager.loadBeans( xmlFile, rs, bs );
    assertEquals( "[r1:1:10:100:1.1:1.01:true:a:1:s1:Sat Jan 01 00:00:00 GMT 2005, r2:2:20:200:2.2:2.02:true:b:2:s2:Wed Feb 02 00:00:00 GMT 2005, r3:3:30:300:3.3:3.03:true:c:3:s3:Thu Mar 03 00:00:00 GMT 2005]", beans.toString() );
  }


  public void testSingle() throws Exception {
    String loadcanon = "[b1:q1, b2:q2]";

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

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

    List beans = xmlManager.loadBeans( xmlFile, rs, bs );
    assertEquals( loadcanon, canon(beans) );

    beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), rs, bs );
    assertEquals( loadcanon, canon(beans) );

    InputStream is = new FileInputStream( xmlFile );
    beans = xmlManager.loadBeans( is, rs, bs );
    assertEquals( loadcanon, canon(beans) );
    is.close();

    InputStream fis  = new FileInputStream( xmlFile );
    InputSource isrc = new InputSource( fis );
    beans = xmlManager.loadBeans( isrc, rs, bs );
    assertEquals( loadcanon, canon(beans) );
    fis.close();

    beans = xmlManager.loadBeansFromURI( TestUtil.makeURI(xmlFile), rs, bs );
    assertEquals( loadcanon, canon(beans) );

    beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), rs, bs );
    assertEquals( loadcanon, canon(beans) );
  }



  public void testRepeat() throws Exception {
    String loadheads  = "Name,Que,";
    String fileprefix = "com/ricebridge/xmlman/test/";

    File xmlFile = null;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    RecordSpec rs = new RecordSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );

    XmlManager xmlManager = new XmlManager(rs);

    String loadcanon1 = "[b1:q1, b2:q2]";
    String loadcanon2 = "[2b1:2q1, 2b2:2q2, 2b3:2q3]";
    String loadcanon3 = "[3b1:3q1, 3b2:3q2, 3b3:3q3, 3b4:3q4]";

    BeanSpec bs = new BeanSpec( SingleBean.class );

    // file
    String loadcanon  = loadcanon1;
    List beans = xmlManager.loadBeans( xmlFile, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    beans = xmlManager.loadBeans( xmlFile, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon2;
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    beans = xmlManager.loadBeans( xmlFile, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon3;
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    beans = xmlManager.loadBeans( xmlFile, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );


    // file path
    loadcanon  = loadcanon1;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon2;
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon3;
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    beans = xmlManager.loadBeans( xmlFile.getAbsolutePath(), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );


    // inputstream
    loadcanon  = loadcanon1;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    InputStream is = new FileInputStream( xmlFile );
    beans = xmlManager.loadBeans( is, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    is.close();

    is = new FileInputStream( xmlFile );
    beans = xmlManager.loadBeans( is, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    is.close();

    loadcanon = loadcanon2;
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    is = new FileInputStream( xmlFile );
    beans = xmlManager.loadBeans( is, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );
    is.close();

    loadcanon = loadcanon3;
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    is = new FileInputStream( xmlFile );
    beans = xmlManager.loadBeans( is, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );
    is.close();


    // inputsource
    loadcanon  = loadcanon1;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    FileInputStream fis = new FileInputStream( xmlFile );
    InputSource isrc = new InputSource( fis );
    beans = xmlManager.loadBeans( isrc, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    fis.close();

    fis = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    beans = xmlManager.loadBeans( isrc, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );
    fis.close();

    loadcanon = loadcanon2;
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    fis = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    beans = xmlManager.loadBeans( isrc, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );
    fis.close();

    loadcanon = loadcanon3;
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    fis = new FileInputStream( xmlFile );
    isrc = new InputSource( fis );
    beans = xmlManager.loadBeans( isrc, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );
    fis.close();


    // uri
    loadcanon  = loadcanon1;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon2;
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon3;
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    beans = xmlManager.loadBeansFromURI( TestUtil.makeURI( xmlFile ), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );


    // string
    loadcanon  = loadcanon1;
    xmlFile = TestUtil.findFile( fileprefix+"load.xml" );
    beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon2;
    xmlFile = TestUtil.findFile( fileprefix+"load02.xml" );
    beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, xmlManager.getStats().getTotalRecords() );

    loadcanon = loadcanon3;
    xmlFile = TestUtil.findFile( fileprefix+"load03.xml" );
    beans = xmlManager.loadBeansFromString( FileUtil.readFile(xmlFile), bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, xmlManager.getStats().getTotalRecords() );
  }




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




  public static class BeanRecord {
    private String iName;
    private String iFoo;
    private String iBar;
    
    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 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 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