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

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


/** Utilities for testing.
 *    <p>The <b><a href="TestUtil.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 TestUtil {

  public static final int NUM_FIELDS = 4;

  public static final String[] TESTDATA = new String[] {
    "0","1","2","3","4","5","6","7","8","9",
    "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","u","r","s","t","u","v","w","x","y","z"
  };


  public static void loadData( LineListener pLN, int pSeed ) {
    pLN.startProcess();
    int      fI = 0;
    String[] d  = new String[ NUM_FIELDS ];
    String   ol = "";
    long     lc = 1;
    for( int cI = 0; cI < TESTDATA.length; cI += pSeed ) {
      d[fI] = TESTDATA[cI];
      ol += d[fI]+(fI==NUM_FIELDS-1?"":",");
      fI++;
      if( fI >= NUM_FIELDS ) {
        fI = 0;
        pLN.handleLine( d, NUM_FIELDS, lc++, ol );
        d = new String[ NUM_FIELDS ];
        ol = "";
      }
    }
    pLN.endProcess();
  }


  public static boolean verifyTableModel( TableModel pTableModel, int pSeed ) {
    int      fI = 0;
    int      rI = 0;
    for( int cI = 0; cI < TESTDATA.length && rI < pTableModel.getRowCount(); cI += pSeed ) {
      if( !TESTDATA[cI].equals( pTableModel.getValueAt(rI,fI) ) ) {
        System.out.println( "no match at row:"+rI+" field:"+fI+", expected:"+TESTDATA[cI]+" but was:"+pTableModel.getValueAt(rI,fI) );
        return false;
      };
      fI++;
      if( fI >= NUM_FIELDS ) {
        fI = 0;
        rI++;
      }
    }
    return true;
  }

  
  public static File getTextCsvFile( String pName ) throws Exception {
    File csvf = null;

    if( null == csvf ) {
      String[] files = new String[] { 
          pName+".csv",
          "/ricebridge/dev/csvman/trunk/src/com/ricebridge/csvman/test/csvfiles/"+pName+".csv",
          "src/com/ricebridge/csvman/test/csvfiles/"+pName+".csv",
          "com/ricebridge/csvman/test/csvfiles/"+pName+".csv",
          "src/"+pName+".csv" };
    
      for( int fI = 0; fI < files.length; fI++ ) {
        csvf = new File( files[fI] );
        if( csvf.exists() ) {
          break;
        }
      }
    }

    //System.out.println("csvf:"+csvf);

    if( null == csvf ) {
      throw new Exception("csv file not found:"+pName);
    }
    
    return csvf;
  }


  public static void displayLists( List pLA, List pLB ) {
    int as = pLA.size();
    int bs = pLB.size();
    int max = as >= bs ? as : bs ;
    for( int rI = 0; rI < max; rI++ ) {
      if( rI < as ) {
        System.out.print( pLA.get(rI) );
      }
      if( rI < bs ) {
        System.out.println( TextUtil.right( String.valueOf(pLB.get(rI)), 20 ) );
      }
    }
  }


  public static void displayTimes( CsvManager pCsvManager ) {
    System.out.println( "tl:"+pCsvManager.getLineCount()+" tt:"+pCsvManager.getTimeTakenInSeconds()+
                        " atpl:"+pCsvManager.getAverageTimePerLineInSeconds() );
  }



  public void writeData( File pFile, byte[] pData ) throws Exception {
    FileOutputStream fout = new FileOutputStream( pFile );
    fout.write( pData );
    fout.close();
  }


  public static String normalizeAsLists( List pData ) {
    StringBuffer sb = new StringBuffer();
    for( Iterator dT = pData.iterator(); dT.hasNext(); ) {
      List r = (List) dT.next();
      for( Iterator rT = r.iterator(); rT.hasNext(); ) {
        String v = (String) rT.next();
        sb.append( v ).append( "," );
      }
      sb.append( "\n" );
    }
    
    return TextUtil.replace( sb.toString(), ",\n", "\n" ); 
  }



  public static File findFile( String pName ) throws Exception {
    File file = null;
    try {
      file = FileUtil.findFile( pName );
    }

    // FIX: this makes ant system-test work
    catch( Exception e ) {
      file = new File( "/ricebridge/dev/csvman/trunk/src", pName );
    }
    return file;
  }

}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Wednesday, June 20 2007 at 22:16