View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.data;
20  
21  import net.sf.jameleon.event.DataDrivableEventHandler;
22  
23  import java.io.IOException;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /***
28   * Ties DataDriver and DataDrivable together.
29   */
30  public class DataExecuter{
31  
32      protected DataDriver driver;
33  
34      /***
35       * Default constructor. At the moment this does nothing.
36       */
37      public DataExecuter(){}
38  
39      /***
40       * Uses the given <code>driver</code> to drive the DataDrivable.
41       * @param driver The drive used to drive the DataDrivable.
42       */
43      public DataExecuter(DataDriver driver){
44          this.driver = driver;
45      }
46  
47      /***
48       * Gets the DataDriver used by this class.
49       * @return the DataDriver used by this class.
50       */
51      public DataDriver getDataDriver(){
52          return driver;
53      }
54  
55      /***
56       * When it is time to make the class data-driven, this method should be called.
57       * @param drivable - The class to be data-driven, for example, a csv tag class
58       * @param noExecute - If set to true, then the driver's methods will not actually get called
59       *                    and the executeDrivableRow will get called with an empty data set.
60       * @throws IOException the datasource handle cannot be opened or the datasource
61       *                     cannot be read from.
62       */
63      public void executeData(DataDrivable drivable, boolean noExecute) throws IOException{
64          int row = 1;
65          if (noExecute) {
66              drivable.addVariablesToRowData(new HashMap());
67              drivable.executeDrivableRow(row);
68              drivable.destroyVariables(new HashMap().keySet());
69          }else{
70              DataDrivableEventHandler eventHandler = DataDrivableEventHandler.getInstance();
71              try{
72                  eventHandler.openEvent(drivable);
73                  driver.open();
74                  Map vars;
75                  while (driver.hasMoreRows()) {
76                      vars = driver.getNextRow();
77                      eventHandler.executeRowEvent(vars, drivable, row);
78                      drivable.addVariablesToRowData(vars);
79                      drivable.executeDrivableRow(row++);
80                      drivable.destroyVariables(vars.keySet());
81                  }
82              }finally{
83                  eventHandler.closeEvent(drivable);
84                  driver.close();
85              }
86          }
87      }
88  
89  }