01: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
02: package com.canoo.webtest.plugins.exceltest;
03:
04: import org.apache.poi.hssf.usermodel.HSSFSheet;
05:
06: import com.canoo.webtest.engine.StepExecutionException;
07:
08: /**
09: * Base class for steps that deal with an individual sheet of an Excel spreadsheet.<p>
10: *
11: * @author Rob Nielsen
12: */
13: public abstract class AbstractExcelSheetStep extends AbstractExcelStep {
14: private String fSheetName;
15: private String fSheetIndex;
16:
17: protected void setCurrentSheet(final HSSFSheet sheet) {
18: getContext().put(KEY_CURRENT_SHEET, sheet);
19: }
20:
21: protected HSSFSheet getExcelSheet() {
22: final int numberOfSheets = getExcelWorkbook()
23: .getNumberOfSheets();
24: if (numberOfSheets == 0) {
25: throw new StepExecutionException(
26: "This spreadsheet has no sheets", this );
27: }
28: HSSFSheet sheet = null;
29: if (fSheetName != null) {
30: sheet = getExcelWorkbook().getSheet(fSheetName);
31: if (sheet == null) {
32: throw new StepExecutionException("A sheet named '"
33: + fSheetName + "' was not found in the file.",
34: this );
35: }
36: }
37: if (sheet == null && fSheetIndex != null) {
38: final int sheetIndex = Integer.parseInt(fSheetIndex);
39: if (sheetIndex < 0 || sheetIndex >= numberOfSheets) {
40: throw new StepExecutionException(
41: "Invalid sheet index: "
42: + fSheetIndex
43: + ". This workbook contains sheets with indexes from 0 to "
44: + (numberOfSheets - 1) + ".", this );
45: }
46: sheet = getExcelWorkbook().getSheetAt(sheetIndex);
47: }
48: if (sheet == null) {
49: sheet = (HSSFSheet) getContext().get(KEY_CURRENT_SHEET);
50: }
51: if (sheet == null) {
52: sheet = getExcelWorkbook().getSheetAt(0);
53: }
54: return sheet;
55: }
56:
57: /**
58: * @param name The Sheet Name
59: * @webtest.parameter required="no"
60: * description="The name of the sheet to select. If no sheet is selected, the value of the last <em>excelSelectSheet</em> call is used, or defaults to the first sheet."
61: */
62: public void setSheetName(final String name) {
63: fSheetName = name;
64: }
65:
66: public String getSheetName() {
67: return fSheetName;
68: }
69:
70: /**
71: * @param index The index of the sheet to select
72: * @webtest.parameter required="no"
73: * description="The index of the sheet to select, starting at zero. If no sheet is selected, the value of the last <em>excelSelectSheet</em> call is used, or defaults to the first sheet."
74: */
75: public void setSheetIndex(final String index) {
76: fSheetIndex = index;
77: }
78:
79: public String getSheetIndex() {
80: return fSheetIndex;
81: }
82:
83: protected void verifyParameters() {
84: super .verifyParameters();
85: optionalIntegerParamCheck(fSheetIndex, "sheetIndex", true);
86: }
87: }
|