/* 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 org.jostraca.util.*;

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

import java.util.*;
import java.io.*;
import java.sql.*;


/** Test cases for {@link com.ricebridge.csvman.CsvManager} StringArray load 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>
 *  and <a href="example.csv">example.csv</a>.</p>
 *    <p>The <b><a href="LoadAsListsTest.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 LoadAsListsTest extends TestCase {

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

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

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



  // test cases

  public void testExample() throws Exception {
    File         csvFile    = TestUtil.getTextCsvFile( "example" );
    CsvManager   csvManager = new CsvManager();
    List         data       = csvManager.loadAsLists( csvFile );
    StringBuffer sb         = new StringBuffer();

    for( int record = 0; record < data.size(); record++ ) {
      sb.append( "record: "+record+"\n" );
      List fields = (List) data.get(record);
      for( int field = 0; field < fields.size(); field++ ) {
        sb.append( "field "+field+" has value: "+fields.get(field)+"\n" );
      }
    }

    String out = "record: 0\nfield 0 has value: r1\nfield 1 has value: f1\nfield 2 has value: b1\nrecord: 1\nfield 0 has value: r2\nfield 1 has value: f2\nfield 2 has value: b2\nrecord: 2\nfield 0 has value: r3\nfield 1 has value: f3\nfield 2 has value: b3\n";
    assertEquals( out, sb.toString() );
  }


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

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

    List data = csvManager.loadAsLists( csvFile);
    assertEquals( loadcanon, canon(data) );

    data = csvManager.loadAsLists( csvFile.getAbsolutePath());
    assertEquals( loadcanon, canon(data) );

    InputStream is = new FileInputStream( csvFile );
    data = csvManager.loadAsLists( is);
    assertEquals( loadcanon, canon(data) );
    is.close();

    Reader fr = new FileReader( csvFile );
    data = csvManager.loadAsLists( fr );
    assertEquals( loadcanon, canon(data) );
    fr.close();

    data = csvManager.loadAsListsFromString( FileUtil.readFile(csvFile));
    assertEquals( loadcanon, canon(data) );
  }



  public void testRepeat() throws Exception {

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

    CsvManager csvManager = new CsvManager();

    String loadcanon01  = "[[b1, q1], [b2, q2]]";
    String loadcanon02 = "[[2b1, 2q1], [2b2, 2q2], [2b3, 2q3]]";
    String loadcanon03 = "[[3b1, 3q1], [3b2, 3q2], [3b3, 3q3], [3b4, 3q4]]";

    // file

    List data = csvManager.loadAsLists( csvFile );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );

    data = csvManager.loadAsLists( csvFile );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );

    csvFile = TestUtil.getTextCsvFile(  "load02" );
    data = csvManager.loadAsLists( csvFile );
    assertEquals( loadcanon02, canon(data) );
    assertEquals( 3, csvManager.getLineCount() );

    csvFile = TestUtil.getTextCsvFile(  "load03" );
    data = csvManager.loadAsLists( csvFile );
    assertEquals( loadcanon03, canon(data) );
    assertEquals( 4, csvManager.getLineCount() );


    // file path
    csvFile = TestUtil.getTextCsvFile(  "load01" );
    data = csvManager.loadAsLists( csvFile.getAbsolutePath() );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );

    data = csvManager.loadAsLists( csvFile.getAbsolutePath() );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );

    csvFile = TestUtil.getTextCsvFile(  "load02" );
    data = csvManager.loadAsLists( csvFile.getAbsolutePath() );
    assertEquals( loadcanon02, canon(data) );
    assertEquals( 3, csvManager.getLineCount() );

    csvFile = TestUtil.getTextCsvFile(  "load03" );
    data = csvManager.loadAsLists( csvFile.getAbsolutePath() );
    assertEquals( loadcanon03, canon(data) );
    assertEquals( 4, csvManager.getLineCount() );


    // inputstream
    csvFile = TestUtil.getTextCsvFile(  "load01" );
    InputStream is = new FileInputStream( csvFile );
    data = csvManager.loadAsLists( is );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );
    is.close();

    is = new FileInputStream( csvFile );
    data = csvManager.loadAsLists( is );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );
    is.close();

    csvFile = TestUtil.getTextCsvFile(  "load02" );
    is = new FileInputStream( csvFile );
    data = csvManager.loadAsLists( is );
    assertEquals( loadcanon02, canon(data) );
    assertEquals( 3, csvManager.getLineCount() );
    is.close();

    csvFile = TestUtil.getTextCsvFile(  "load03" );
    is = new FileInputStream( csvFile );
    data = csvManager.loadAsLists( is );
    assertEquals( loadcanon03, canon(data) );
    assertEquals( 4, csvManager.getLineCount() );
    is.close();


    // reader
    csvFile = TestUtil.getTextCsvFile(  "load01" );
    Reader fr = new FileReader( csvFile );
    data = csvManager.loadAsLists( fr );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );
    fr.close();

    fr = new FileReader( csvFile );
    data = csvManager.loadAsLists( fr );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );
    fr.close();

    csvFile = TestUtil.getTextCsvFile(  "load02" );
    fr = new FileReader( csvFile );
    data = csvManager.loadAsLists( fr );
    assertEquals( loadcanon02, canon(data) );
    assertEquals( 3, csvManager.getLineCount() );
    fr.close();

    csvFile = TestUtil.getTextCsvFile(  "load03" );
    fr = new FileReader( csvFile );
    data = csvManager.loadAsLists( fr );
    assertEquals( loadcanon03, canon(data) );
    assertEquals( 4, csvManager.getLineCount() );
    fr.close();

    // string
    csvFile = TestUtil.getTextCsvFile( "load01" );
    data = csvManager.loadAsListsFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );

    data = csvManager.loadAsListsFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon01, canon(data) );
    assertEquals( 2, csvManager.getLineCount() );

    csvFile = TestUtil.getTextCsvFile( "load02" );
    data = csvManager.loadAsListsFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon02, canon(data) );
    assertEquals( 3, csvManager.getLineCount() );

    csvFile = TestUtil.getTextCsvFile( "load03" );
    data = csvManager.loadAsListsFromString( FileUtil.readFile(csvFile) );
    assertEquals( loadcanon03, canon(data) );
    assertEquals( 4, csvManager.getLineCount() );
  }


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





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