/* Copyright (c) 2005-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 javax.swing.table.TableModel;

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

import java.io.*;


/** Test cases for {@link com.ricebridge.csvman.CsvManager} {@link javax.swing.table.TableModel} loading.
 *    <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>
 *  and <a href="csvfiles/example.csv">example.csv</a>.</p>
 *    <p>The <b><a href="LoadTableModelTest.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 LoadTableModelTest extends TestCase {

  // standard test methods
  
  public LoadTableModelTest( String pName ) {
    super( pName );
  }

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

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


  // test cases

  public void testMissingCells() {
    String src = "h1,h2,h3\n11,12,13\n21,22\n31";
    CsvManager csvman = new CsvManager();
    TableModel tm = csvman.loadTableModelFromString( src, true );
    assertEquals( "CsvTableModel[head:true,edit:false, data:[\n|11|12|13|\n|21|22||\n|31|||\n]]", tm.toString() );

    tm = csvman.loadTableModelFromString( src, false );
    assertEquals( "CsvTableModel[head:false,edit:false, data:[\n|h1|h2|h3|\n|11|12|13|\n|21|22||\n|31|||\n]]", tm.toString() );
  }



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

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

    TableModel         tm         = csvManager.loadTableModel( csvFile, false );
    int                numCols    = tm.getColumnCount();
    int                numRows    = tm.getRowCount();

    StringBuffer sb = new StringBuffer();
    for( int col = 0; col < numCols; col++ ) {
      sb.append( tm.getColumnName(col) + (col<numCols-1?", ":"\n") );
    }
    sb.append( "---\n" );
    for( int row = 0; row < numRows; row++ ) {
      for( int col = 0; col < numCols; col++ ) {
        sb.append( tm.getValueAt(row,col) + (col<numCols-1?", ":"\n") );
      }
    }

    assertEquals( "Column 1, Column 2, Column 3\n---\nr1, f1, b1\nr2, f2, b2\nr3, f3, b3\n", sb.toString() );

    CsvSpec xs = csvManager.getCsvSpec();
    xs.setProperty("TableModel.dataHasHeaders", true);
    tm  = csvManager.loadTableModel( csvFile );
    assertEquals( "r2,f2,b2|r3,f3,b3|", canon(tm) );
    assertEquals( "r1,f1,b1,", heads(tm) );
  }


  /*

  public void testSingle() throws Exception {
    String loadcanon = "b1,q1|b2,q2|";
    String loadheads = "Name,Que,";

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

    LineSpec rs = new LineSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );

    TableModel tm = csvManager.loadTableModel( csvFile, rs );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );

    tm = csvManager.loadTableModel( csvFile.getAbsolutePath(), rs );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );

    InputStream is = new FileInputStream( csvFile );
    tm = csvManager.loadTableModel( is, rs );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    is.close();

    InputStream fis  = new FileInputStream( csvFile );
    InputSource isrc = new InputSource( fis );
    tm = csvManager.loadTableModel( isrc, rs );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    fis.close();

    tm = csvManager.loadTableModelFromURI( TestUtil.makeURI(csvFile), rs );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );

    tm = csvManager.loadTableModelFromString( FileUtil.readFile(csvFile), rs );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
  }





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

    File csvFile = null;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    LineSpec rs = new LineSpec( "/foo/bar", 
                                    new String[] {"@name","que"},
                                    new String[] {"Name","Que"} );

    CsvManager csvManager = new CsvManager(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|";


    // file
    String loadcanon  = loadcanon1;
    TableModel tm = csvManager.loadTableModel( csvFile );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    tm = csvManager.loadTableModel( csvFile );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    tm = csvManager.loadTableModel( csvFile );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 3, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    tm = csvManager.loadTableModel( csvFile );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 4, csvManager.getStats().getTotalLines() );


    // file path
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    tm = csvManager.loadTableModel( csvFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    tm = csvManager.loadTableModel( csvFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    tm = csvManager.loadTableModel( csvFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 3, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    tm = csvManager.loadTableModel( csvFile.getAbsolutePath() );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 4, csvManager.getStats().getTotalLines() );


    // inputstream
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    InputStream is = new FileInputStream( csvFile );
    tm = csvManager.loadTableModel( is );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );
    is.close();

    is = new FileInputStream( csvFile );
    tm = csvManager.loadTableModel( is );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );
    is.close();

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    is = new FileInputStream( csvFile );
    tm = csvManager.loadTableModel( is );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 3, csvManager.getStats().getTotalLines() );
    is.close();

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    is = new FileInputStream( csvFile );
    tm = csvManager.loadTableModel( is );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 4, csvManager.getStats().getTotalLines() );
    is.close();


    // inputsource
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    FileInputStream fis = new FileInputStream( csvFile );
    InputSource isrc = new InputSource( fis );
    tm = csvManager.loadTableModel( isrc );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );
    fis.close();

    fis = new FileInputStream( csvFile );
    isrc = new InputSource( fis );
    tm = csvManager.loadTableModel( isrc );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );
    fis.close();

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    fis = new FileInputStream( csvFile );
    isrc = new InputSource( fis );
    tm = csvManager.loadTableModel( isrc );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 3, csvManager.getStats().getTotalLines() );
    fis.close();

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    fis = new FileInputStream( csvFile );
    isrc = new InputSource( fis );
    tm = csvManager.loadTableModel( isrc );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 4, csvManager.getStats().getTotalLines() );
    fis.close();


    // uri
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    tm = csvManager.loadTableModelFromURI( TestUtil.makeURI( csvFile ) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    tm = csvManager.loadTableModelFromURI( TestUtil.makeURI( csvFile ) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    tm = csvManager.loadTableModelFromURI( TestUtil.makeURI( csvFile ) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 3, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    tm = csvManager.loadTableModelFromURI( TestUtil.makeURI( csvFile ) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 4, csvManager.getStats().getTotalLines() );


    // string
    loadcanon  = loadcanon1;
    csvFile = TestUtil.getTextCsvFile( "load01" );
    tm = csvManager.loadTableModelFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    tm = csvManager.loadTableModelFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 2, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon2;
    csvFile = TestUtil.getTextCsvFile( "load02" );
    tm = csvManager.loadTableModelFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 3, csvManager.getStats().getTotalLines() );

    loadcanon = loadcanon3;
    csvFile = TestUtil.getTextCsvFile( "load03" );
    tm = csvManager.loadTableModelFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon, canon(tm) );
    assertEquals( loadheads, heads(tm) );
    assertEquals( 4, csvManager.getStats().getTotalLines() );
  }
  */


  private String canon( TableModel pTableModel ) throws Exception {
    StringBuffer sb      = new StringBuffer();
    int          numCols = pTableModel.getColumnCount();    
    int          numRows = pTableModel.getRowCount();    
    for( int row = 0; row < numRows; row++ ) {
      for( int col = 0; col < numCols; col++ ) {
        sb.append( pTableModel.getValueAt(row,col) + (col<numCols-1?",":"|") );
      }
    }
    return sb.toString();
  }

  private String heads( TableModel pTableModel ) throws Exception {
    StringBuffer sb      = new StringBuffer();
    int          numCols = pTableModel.getColumnCount();    
    for( int col = 0; col < numCols; col++ ) {
      sb.append( pTableModel.getColumnName(col) + "," );
    }
    return sb.toString();
  }

}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Monday, November 06 2006 at 00:00