Method to get a table row as a Hashtable

First post of the new year… I hope it’s coherant. And it’s to do with dealing with rows in tables. Another of RFT’s weak points.

This post contains code that will return a hashtable with a specified row’s data. The hashtable’s keys are the column names, and the values are the values in the cell. The value type is whatever the object type of the cell is.

To use it, paste the code into wherever you use tables and want to get row data from them. Call the method and pass in the table to interrogate and the row number to fetch. Easy!

I know, I know… the code could be much improved. Please send in your fixes. Thanks!


/*
* Copyright (c) 2006, Nathaniel Ritmeyer
* All rights reserved.
*
* 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 natontesting.com 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.
*/

/*
* www.natontesting.com
*/
public Hashtable getObjectsForRow(GuiSubitemTestObject tblTargetTable, int iRowNumber) throws Exception
{
//set up a hashtable to store our results
Hashtable htKeyValuePairs = new Hashtable();

//get the test data from the table so that we can use it
ITestDataTable itdt = (ITestDataTable)tblTargetTable.getTestData(“contents”);

//get the number of columns; we’re going to use this for the loop
int iNumberOfColumns = itdt.getColumnCount();

//loop through each column
for(int iColumnCounter = 0; iColumnCounter < iNumberOfColumns; iColumnCounter++)
{
//get the column name (as a string) and the corresponding cell value (as an object)
String sColumnName = itdt.getColumnHeader(iColumnCounter).toString();
//with the: iRowNumber – 1, the “-1″ is there because all these methods are 1-based
Object oCellValue = itdt.getCell(iRowNumber – 1, iColumnCounter);

//if either the column name or the cell == null, set them to a blank
//string, or the hashtable won’t be able to deal with it
if(sColumnName == null)
{
sColumnName = “”;
}
if(oCellValue == null)
{
oCellValue = “”;
}

//add the column name and cell value to the hashtable
htKeyValuePairs.put(sColumnName, oCellValue);
}

//return the hashtable
return htKeyValuePairs;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>