package com.ricebridge.csvman.test;
import com.ricebridge.csvman.*;
import org.jostraca.util.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
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;
}
}
}
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 );
}
catch( Exception e ) {
file = new File( "/ricebridge/dev/csvman/trunk/src", pName );
}
return file;
}
}