public Object[][] getCsvData()
{
CsvConfiguration conf = new CsvConfiguration( 1 ); //row 1 is the column names
DataContext csvContext = DataContextFactory.createCsvDataContext( csvFile, conf );
Schema schema = csvContext.getDefaultSchema();
Table[] tables = schema.getTables();
Table table = tables[0]; // table is basically the name of the file as a String
setColumnNames( table.getColumnNames() );
DataSet dataSet = csvContext.query()
.from( table )
.selectAll()
.where( "runRow" ).eq( "Y" )
.execute();
List<Row> rows = dataSet.toRows();
Object[][] myArray = getRowsAsObjectArray( rows );
return myArray;
}
/**
* Gets a set of Map objects that represent the Rows of a CSV file.
* Requires first column be Y or N
* Expects second column be a browser string value. Parse it outside this class with parseBrowser method.
* @param rows
* @return
*/
public Object[][] getRowsAsObjectArray( List<Row> rows ) {
Object[][] myArray = new Object[rows.size()][2];
int i = 0;
Object[] cols = this.getColumnNames();
String[] colsArray = Arrays.copyOf( cols, cols.length, String[].class );
log.logInfo( "Processing rows with data provider..." );
for ( Row r : rows ) {
Object[] data = r.getValues();
for ( int j = 0; j < colsArray.length; j++ ) {
if ( data[j] == null ) {
data[j] = ""; // force empty string where there are NULL values
} else {
data[j] = data[j].toString().trim();
}
}
String[] dataArray = Arrays.copyOf( data, data.length, String[].class );
Map<String, String> paramMap = new HashMap<String,String>();
Assert.assertTrue( colsArray.length == dataArray.length, "Number of columns doesn't match data on row index: " + i );
for ( int s = 0; s < colsArray.length; s++ ) {
for ( int t = 0; t < dataArray.length; t++ ) {
//
log.info( "Add arg [" + colsArray[t] + "|" + dataArray[t] + "]" );
paramMap.put( colsArray[t], dataArray[t] );
}
}
myArray[i][0] = parseBrowser( paramMap.get( "browser" ) ); // 1st param
myArray[i][1] = paramMap; // 2nd param
log.logInfo( "----- Row " + (i+1) + " -----" );
for ( String key: paramMap.keySet() ) {
log.logInfo( "[" + key + "|" + paramMap.get( key ) + "]" );
}
log.logInfo( "-----------------------------" );
i++;
}
return myArray;
}