use of com.axway.ats.harness.testng.exceptions.DataProviderException in project ats-framework by Axway.
the class ExcelParser method determineTestDataFrame.
/**
* Iterates through the sheet and determines the cells that are between START_TEST_CASE and END_TEST_CASE comments.
* Also determines if the data table is to be returned as a Cartesian product of the rows.
* @param sheet
* @throws DataProviderException
*/
private void determineTestDataFrame(Sheet sheet) throws DataProviderException {
int rows = sheet.getLastRowNum() + 1;
int columns = sheet.getRow(sheet.getLastRowNum()).getLastCellNum();
// iterate throughout the spreadsheet's cells
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
Row rowValue = sheet.getRow(y);
if (rowValue != null) {
if (rowValue.getLastCellNum() > columns) {
columns = rowValue.getLastCellNum();
}
Cell current = rowValue.getCell(x, Row.CREATE_NULL_AS_BLANK);
if (hasComments(current)) {
if (isStartingCell(current)) {
this.startingCell = current;
}
if (isEndingCell(current)) {
this.endingCell = current;
}
if (isMultiplyCell(current)) {
this.isMultipliable = true;
}
}
}
}
}
if (this.startingCell == null) {
throw new DataProviderException(ERROR_LOCATING_STARTING_CELL);
} else if (this.endingCell == null) {
throw new DataProviderException(ERROR_LOCATING_ENDING_CELL);
}
if (this.startingCell.getRowIndex() <= this.endingCell.getRowIndex()) {
if (this.startingCell.getColumnIndex() <= this.endingCell.getColumnIndex()) {
return;
}
}
throw new DataProviderException(WRONG_ORDER);
}
use of com.axway.ats.harness.testng.exceptions.DataProviderException in project ats-framework by Axway.
the class ExcelParser method loadDataBlock.
/**
* Parses the test case data (the data between TEST_CASE_START and TEST_CASE_END commented cells) from the
* excel spreadsheet and loads it into a two-dimensional Object[][] array - workingObjectArray
*/
private void loadDataBlock(Method method) throws DataProviderException {
Sheet sheet = excelFileWorkbook.getSheet(this.sheetName);
if (sheet == null) {
throw new DataProviderException(UNABLE_TO_LOAD_SHEETS + this.sheetName);
}
//Get the starting cell coordinates
Cell startingCell = getStartingCell(sheet);
int startCol = startingCell.getColumnIndex();
int startRow = startingCell.getRowIndex();
//Get the ending cell coordinates
Cell endingCell = getEndingCell(sheet);
int endCol = endingCell.getColumnIndex();
int endRow = endingCell.getRowIndex();
if (method.getParameterTypes().length != (endCol - startCol) + 1) {
throw new DataProviderException(" Expected " + method.getParameterTypes().length + " parameters in the test method while the table has " + ((endCol - startCol) + 1));
}
// If the data table is to be returned as a Cartesian product of the rows (the not empty cells)
if (isMultipliable) {
makeCartesianProductTable(startCol, startRow, endCol, endRow, sheet, method);
} else {
// Initialize the object array
int columns = endCol - startCol + 1;
int rows = endRow - startRow + 1;
this.log.debug(CREATING_DATA_BLOCK + columns + "/" + rows);
this.workingObjectArray = new Object[rows][columns];
//Fill the object array iterating the sheet column by column
for (int col = startCol, parameterIndex = 0; col <= endCol; col++, parameterIndex++) {
//Get the type of the method parameter at the current position
Class<?> parameterType = getParameterTypeAt(method, parameterIndex);
//Iterate over the current column to load the cells according to the parameter type
for (int row = startRow; row <= endRow; row++) {
Row rowValue = sheet.getRow(row);
if (rowValue != null) {
Cell currentCell = rowValue.getCell(col, Row.CREATE_NULL_AS_BLANK);
this.workingObjectArray[row - startRow][parameterIndex] = parseCellContents(currentCell, parameterType);
}
}
}
}
}
use of com.axway.ats.harness.testng.exceptions.DataProviderException in project ats-framework by Axway.
the class ExcelParser method parseCellContents.
/**
* Reads the cell content and try to convert it to the specified Java type
* @param cell the cell which content will be parsed
* @param methodParameterType the type to which the cell content should be converted
* @return Object of type methodParameterType, null if the cell contents equals "NULL" or SkipObject if ...
*/
private Object parseCellContents(Cell cell, Class<?> methodParameterType) throws DataProviderException {
String cellValue = "";
if (!methodParameterType.equals(Date.class)) {
cell.setCellType(Cell.CELL_TYPE_STRING);
cellValue = cell.getStringCellValue();
}
// the type was determined based on cell comments and type instead of the method parameter type
if (hasComments(cell) && checkCellComment(cell, STRING_TOKEN)) {
if (methodParameterType.equals(String.class)) {
return cellValue;
} else {
throw new DataProviderException("The cell has 'STRING' comment but the method parameter is of type '" + methodParameterType.getSimpleName());
}
}
// Return null if the cell value is "NULL"
if (cellValue.equalsIgnoreCase(STRING_NULL)) {
if (methodParameterType.isPrimitive()) {
throw new DataProviderException("Can not pass 'null' to parameter of primitive type '" + methodParameterType.getSimpleName() + "'");
} else {
return null;
}
}
if (isMultipliable) {
// will be skipped
if (!(hasComments(cell) && checkCellComment(cell, STRING_TOKEN)) && "".equals(cellValue)) {
return new SkipObject();
}
}
// Try to convert the cell value to the methodParameterType Java type
try {
// DATE
if (methodParameterType.equals(Date.class)) {
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue();
} else {
throw new DataProviderException("Can not parse '" + cell.getStringCellValue() + "' as a valid date as the cell is not formatted as type DATE.");
}
}
// BYTE
if (methodParameterType.equals(byte.class) || methodParameterType.equals(Byte.class)) {
return Byte.parseByte(cellValue.trim());
}
// CHAR
if (methodParameterType.equals(char.class) || methodParameterType.equals(Character.class)) {
if (cellValue.length() > 1) {
throw new ParseException("The value '" + cellValue + "' can't be converted to Char as it is longer than 1.", 2);
}
return cellValue.charAt(0);
}
// SHORT
if (methodParameterType.equals(short.class) || methodParameterType.equals(Short.class)) {
return Short.parseShort(cellValue.trim());
}
// INT
if (methodParameterType.equals(int.class) || methodParameterType.equals(Integer.class)) {
return Integer.parseInt(cellValue.trim());
}
// LONG
if (methodParameterType.equals(long.class) || methodParameterType.equals(Long.class)) {
return Long.parseLong(cellValue.trim());
}
//FLOAT
if (methodParameterType.equals(float.class) || methodParameterType.equals(Float.class)) {
return Float.parseFloat(cellValue.trim().replace(',', '.'));
}
//DOUBLE
if (methodParameterType.equals(double.class) || methodParameterType.equals(Double.class)) {
return Double.parseDouble(cellValue.trim().replace(',', '.'));
}
//BOOLEAN
if (methodParameterType.equals(boolean.class) || methodParameterType.equals(Boolean.class)) {
if (cellValue.trim().equalsIgnoreCase(Boolean.TRUE.toString()) || cellValue.trim().equalsIgnoreCase(Boolean.FALSE.toString())) {
return Boolean.parseBoolean(cellValue.trim());
} else {
throw new ParseException("The value '" + cellValue.trim() + "' can't be converted to type Boolean.", 1);
}
}
//NUMBER - If the parameter type is Number then convert the value to Double
if (methodParameterType.equals(Number.class)) {
return Double.parseDouble(cellValue.trim().replace(',', '.'));
}
} catch (NumberFormatException nfe) {
throw new DataProviderException(nfe);
} catch (ParseException pe) {
throw new DataProviderException(pe);
}
// The method parameter type was different from the examined types so return the String value of the cell
return cellValue;
}
use of com.axway.ats.harness.testng.exceptions.DataProviderException in project ats-framework by Axway.
the class BasicDataProvider method getDataFileInputStream.
/**
* Returns the {@link InputStream} to the method input data file. The folder holding the file can be specified by TestOptions
* annotation in the test class, method or in the harness properties file. If not specified in any of these
* locations, search for the data file in the classpath.
*
* The name of data file can be specified by TestOptions annotation in the test method. If not - use the test class
* name
*
* @param m the test method
* @param fileExtension a specific file extension for each data provider
* @return data file {@link InputStream}
* @throws DataProviderException
* @throws ConfigurationException
* @throws NoSuchPropertyException
*/
protected InputStream getDataFileInputStream(Method m) throws DataProviderException, NoSuchPropertyException, ConfigurationException {
// Search data file folder in:
// - test method TestOptions annotation
// - test class TestOptions annotation
// - test harness properties file
String dataFileFolder = getDataFileFolder(m);
// Search data file in(it will never return null):
// - test class TestOptions annotation
// - use the class name
String dataFileName;
if (dataFileFolder != null) {
dataFileName = getDataFileName(m, XLSX);
// We know the data file folder. Check if the file exists.
File dataFile = new File(new File(dataFileFolder), dataFileName);
if (!dataFile.exists()) {
dataFileName = getDataFileName(m, XLS);
dataFile = new File(new File(dataFileFolder), dataFileName);
}
try {
return new FileInputStream(dataFile);
} catch (FileNotFoundException fnfe) {
throw new DataProviderException("Data file does not exist. Neither " + dataFile + " nor same name .xlsx file was found. ");
}
} else {
dataFileName = getDataFileName(m, XLSX);
// Data file folder is not specified.
// Try to find the data file in the classpath.
InputStream fileInputStream = m.getDeclaringClass().getResourceAsStream(dataFileName);
if (fileInputStream != null) {
return fileInputStream;
}
dataFileName = getDataFileName(m, XLS);
// Data file folder is not specified.
// Try to find the data file in the classpath.
fileInputStream = m.getDeclaringClass().getResourceAsStream(dataFileName);
if (fileInputStream == null) {
throw new DataProviderException("Data file does not exist. Neither " + dataFileName + " nor same name .xlsx was found in classpath");
}
return fileInputStream;
}
}
Aggregations