tags:

views:

77

answers:

2

Hi guys,

My problem here is i cant able to convert the Hashtable array into object array. here content reading reads each and every row from the xls file and saves it in the hashtable array in this format:

roleName=testRole981, admin=admin, projectName=Automateme, userPassword=aspiresys12, roleDesc=grant[testplan_execute],grant[testplan_create_build],grant[testplan_metrics], adminPass=admin, userName=mur.

I want to convert that hashTable array into an object array so that i can pass those in my testNG test cases as a DataProvider.

public class DriverScript {
 //public boolean isExecuted = true;
 Object[][] Obj = new Object[100][100];
 @SuppressWarnings("unchecked")
  @DataProvider(name="Test")
  public Object[][] ValidDataProvider() {
  Utilities util = new Utilities();
  String pathValue = Utilities.LocatingXls("Data.xls");
  Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1"); 
   Object[][] Obj = new Object[100][100];
   for(int i=0;i<hashDrv.length;i++)
   {
    System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
    Obj[i][0] = hashDrv[i];
   }
  System.out.println("outsideloop" + Obj[0][0]); 
  return  Obj;
  }

 @SuppressWarnings("unchecked")
 @Test(dataProvider = "Test")
    public  void methodtest(Hashtable <String, String> a)

 {
/* Utilities util = new Utilities();
 String pathValue = Utilities.LocatingXls("Data.xls");
 Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[]) util.contentReading(pathValue, "Scenario1"); 
 for(int i=0;i<hashDrv.length;i++)
 {
  System.out.println("cont vector reading" + hashDrv[i].get("projectName"));
  Scenario1 scnTst=new Scenario1(hashDrv[i]);
  scnTst.check1();
 }
  if(!isExecuted)
  {
   Assert.fail("falied");
  }
 }*/

}}
+1  A: 

The Hashtable class implements the Map interface. This interface has a method on it called values() - that returns a Collection. You can then call toArray() on that collection to get an array.

Example:

Hashtable h = // .... initialised and populated somewhere else
Collection c = h.values();
Object[] objectArray = c.toArray();

Issues:

  1. The code above doesn't use generics, so there will be warnings everywhere.
  2. Hashtable is probably not the best class for you to use, try HashMap. HashTable is synchronized, so if you're not looking to be thread safe, using that class will result in slower code.
  3. You should really be programming to an interface: Map map = new HashMap(); (or Hashtable if you prefer.
Noel M
+1  A: 

Since the test method using this data provider has one parameter, the second dimension of the Object[][] array should be 1, not 100. Perhaps something like this (untested) code:

@SuppressWarnings("unchecked")
@DataProvider(name="Test")
public Object[][] validDataProvider() {
    Utilities util = new Utilities();
    String pathValue = Utilities.LocatingXls("Data.xls");
    Hashtable<String, String>[] hashDrv = (Hashtable<String, String>[])
        util.contentReading(pathValue, "Scenario1"); 
    Object[][] obj = new Object[hashDrv.length][1];
    for(int i=0; i<hashDrv.length; i++) {
        System.out.println("cont vector reading" 
            + hashDrv[i].get("projectName"));
        obj[i][0] = hashDrv[i];
    }
    System.out.println("outsideloop" + obj[0][0]); 
    return obj;
}
markusk