/* Copyright (c) 2006 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.csvman.test;


import com.ricebridge.csvman.*;

import com.ricebridge.data.Text;
import com.ricebridge.data.BeanSpec;
import com.ricebridge.data.sc.DefaultStringConverter;

import org.jostraca.util.*;

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

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


/** Test cases for {@link com.ricebridge.csvman.CsvManager} Beans handling.
 *    <p>Test files are <a href="csvfiles/load01.csv">load01.csv</a>, 
 *  <a href="csvfiles/load02.csv">load02.csv</a>, <a href="csvfiles/load03.csv">load03.csv</a>,
 *  <a href="beans01.csv">beans01.csv</a>, <a href="beans02.csv">beans02.csv</a>, 
 *  <a href="beans03.csv">beans03.csv</a> and <a href="example.csv">example.csv</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 csvFile = null;

    csvFile = TestUtil.getTextCsvFile( "beans01" );
    CsvManager csvManager = new CsvManager();

    LineSpec fs = new LineSpec( new String[] {"Name","Foo","Bar"} );
    BeanSpec bs = new BeanSpec( BeanRecord.class );

    try {
      List beans = csvManager.loadBeans( csvFile, fs, bs );
      assertEquals( "[r1:f1:b1, r2:f2:b2, r3:f3:b3]", beans.toString() );
    }
    catch( CsvManagerException ce ) {
      System.out.println( ce );
      fail();
    }
  }



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

    csvFile = TestUtil.getTextCsvFile( "beans02" );
    CsvManager csvManager = new CsvManager();

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

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

    try {
      List beans = csvManager.loadBeans( csvFile, 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() );
    }
    catch( CsvManagerException ce ) {
      System.out.println( ce );
      fail();
    }

  }



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

    File csvFile = null;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    CsvManager csvManager = new CsvManager();

    LineSpec fs = new LineSpec( new String[] {"Name","Que"} );
    BeanSpec bs = new BeanSpec( SingleBean.class );

    List beans = csvManager.loadBeans( csvFile, fs, bs );
    assertEquals( loadcanon, canon(beans) );

    beans = csvManager.loadBeans( csvFile.getAbsolutePath(), fs, bs );
    assertEquals( loadcanon, canon(beans) );

    InputStream is = new FileInputStream( csvFile );
    beans = csvManager.loadBeans( is, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    is.close();

    FileReader fr = new FileReader( csvFile );
    beans = csvManager.loadBeans( fr, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    fr.close();

    beans = csvManager.loadBeans( new Text(FileUtil.readFile(csvFile)), fs, bs );
    assertEquals( loadcanon, canon(beans) );
  }



  public void testRepeat() throws Exception {
    String loadheads  = "Name,Que,";

    File csvFile = null;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    LineSpec fs = new LineSpec( new String[] {"Name","Que"} );

    CsvManager csvManager = new CsvManager();

    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 = csvManager.loadBeans( csvFile, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );

    beans = csvManager.loadBeans( csvFile, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    beans = csvManager.loadBeans( csvFile, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, csvManager.getLineCount() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    beans = csvManager.loadBeans( csvFile, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, csvManager.getLineCount() );


    // file path
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    beans = csvManager.loadBeans( csvFile.getAbsolutePath(), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );

    beans = csvManager.loadBeans( csvFile.getAbsolutePath(), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    beans = csvManager.loadBeans( csvFile.getAbsolutePath(), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, csvManager.getLineCount() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    beans = csvManager.loadBeans( csvFile.getAbsolutePath(), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, csvManager.getLineCount() );


    // inputstream
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    InputStream is = new FileInputStream( csvFile );
    beans = csvManager.loadBeans( is, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );
    is.close();

    is = new FileInputStream( csvFile );
    beans = csvManager.loadBeans( is, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );
    is.close();

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    is = new FileInputStream( csvFile );
    beans = csvManager.loadBeans( is, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, csvManager.getLineCount() );
    is.close();

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    is = new FileInputStream( csvFile );
    beans = csvManager.loadBeans( is, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, csvManager.getLineCount() );
    is.close();


    // reader
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    Reader rdr = new FileReader( csvFile );
    beans = csvManager.loadBeans( rdr, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );
    rdr.close();

    rdr = new FileReader( csvFile );
    beans = csvManager.loadBeans( rdr, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );
    rdr.close();

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    rdr = new FileReader( csvFile );
    beans = csvManager.loadBeans( rdr, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, csvManager.getLineCount() );
    rdr.close();

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    rdr = new FileReader( csvFile );
    beans = csvManager.loadBeans( rdr, fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, csvManager.getLineCount() );
    rdr.close();


    // string
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    beans = csvManager.loadBeans( new Text(FileUtil.readFile(csvFile)), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );

    beans = csvManager.loadBeans( new Text(FileUtil.readFile(csvFile)), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 2, csvManager.getLineCount() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    beans = csvManager.loadBeans( new Text(FileUtil.readFile(csvFile)), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 3, csvManager.getLineCount() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    beans = csvManager.loadBeans( new Text(FileUtil.readFile(csvFile)), fs, bs );
    assertEquals( loadcanon, canon(beans) );
    assertEquals( 4, csvManager.getLineCount() );
  }




  public void testSettings() throws Exception {
    File csvFile = null;

    csvFile = TestUtil.getTextCsvFile( "beans03" );
    CsvManager csvManager = new CsvManager();

    csvManager.getCsvSpec().setProperty( BeanLineListener.PROP_Bean_firstLineFieldsAsNeeded, false );
    BeanSpec bs = new BeanSpec( BeanRecord.class );

    try {
      List beans = csvManager.loadBeans( csvFile, bs );
      fail();
    }
    catch( CsvManagerException ce ) {
      assertEquals( CsvManagerException.CODE_nofieldnames, ce.getCode() );
    }

    csvManager.getCsvSpec().setProperty( BeanLineListener.PROP_Bean_firstLineFieldsAsNeeded, true );
    try {
      List beans = csvManager.loadBeans( csvFile, bs );
      assertEquals("[n1:f1:b1, n2:f2:b2]", beans.toString() );
    }
    catch( CsvManagerException ce ) {
      System.out.println( ce );
      fail();
    }


    csvManager.getCsvSpec().setProperty( BeanLineListener.PROP_Bean_useDefault, false );
    bs = new BeanSpec( TypeBeanRecord.class );
    try {
      List beans = csvManager.loadBeans( csvFile, bs );
      fail();
    }
    catch( CsvManagerException ce ) {
      System.out.println( ce );
      ce.printStackTrace();
      assertEquals( CsvManagerException.CODE_setbeanfield_msg, ce.getCode() );
    }

    csvManager.getCsvSpec().setProperty( BeanLineListener.PROP_Bean_useDefault, true );
    try {
      List beans = csvManager.loadBeans( csvFile, bs );
      assertEquals("[n1:f1:b1:1, n2:f2:b2:0]", beans.toString() );
    }
    catch( CsvManagerException ce ) {
      System.out.println( ce );
      fail();
    }

  }




  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 TypeBeanRecord extends BeanRecord {
    private int iInt;
    
    public void setInt( int pInt ) { iInt = pInt; }
    public int getInt() { return iInt; }

    public String toString() {
      return super.toString()+":"+iInt;
    }
  }


  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.
Monday, November 06 2006 at 00:00