Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/EaseTech/easytest
Browse files Browse the repository at this point in the history
  • Loading branch information
anujgandharv committed Sep 24, 2012
2 parents 9074ab5 + c0a9c79 commit f569a81
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 43 deletions.
65 changes: 26 additions & 39 deletions src/main/java/org/easetech/easytest/loader/ExcelDataLoader.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package org.easetech.easytest.loader;

import java.io.FileNotFoundException;
Expand Down Expand Up @@ -102,7 +101,6 @@ private Map<String, List<Map<String, Object>>> loadFromSpreadsheet(final InputSt

data = new HashMap<String, List<Map<String, Object>>>();
Sheet sheet = workbook.getSheetAt(0);
int numberOfColumns = countNonEmptyColumns(sheet);

Map<String, List<Map<String, Object>>> finalData = new HashMap<String, List<Map<String, Object>>>();

Expand All @@ -113,9 +111,9 @@ private Map<String, List<Map<String, Object>>> loadFromSpreadsheet(final InputSt
boolean keyRow = false;

Map<String, Object> actualData = new HashMap<String, Object>();
for (int column = 0; column < numberOfColumns; column++) {

Cell cell = row.getCell(column);
int column = 0;
for (Cell cell:row) {
Object cellData = objectFrom(workbook, cell);
if (isFirstColumn && cellData != null) {
// Indicates that this is a new set of test data.
Expand All @@ -134,7 +132,11 @@ private Map<String, List<Map<String, Object>>> loadFromSpreadsheet(final InputSt

}
isFirstColumn = false;

LOG.debug("column"+column);
if(cellData != null){
LOG.debug("column"+column+", cellData:"+cellData.toString());
}
column++;
}
if (!keyRow) {
dataValues.add(actualData);
Expand All @@ -143,33 +145,7 @@ private Map<String, List<Map<String, Object>>> loadFromSpreadsheet(final InputSt
return finalData;
}

/**
* Count the number of columns, using the number of non-empty cells in the first row.
*
* @param sheet the excel sheet that contains the data
* @return number of non empty columns.
*/
private int countNonEmptyColumns(final Sheet sheet) {
Row firstRow = sheet.getRow(0);
return firstEmptyCellPosition(firstRow);
}

/**
* Get the first empty cell position in the sheet
*
* @param cells the row in the sheet
* @return the first empty cell position in the sheet
*/
private int firstEmptyCellPosition(final Row cells) {
int columnCount = 0;
for (Cell cell : cells) {
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
break;
}
columnCount++;
}
return columnCount;
}

/**
* Get the cell value from the workbook and the specified cell within the workbook.
Expand Down Expand Up @@ -209,6 +185,10 @@ private Object getNumericCellValue(final Cell cell) {
cellValue = new Date(cell.getDateCellValue().getTime());
} else {
cellValue = cell.getNumericCellValue();
// below is the work around to remove suffix .0 from numeric fields
if(cellValue!= null && cellValue.toString().endsWith(".0")){
cellValue = cellValue.toString().replace(".0", "");
}
}
return cellValue;
}
Expand Down Expand Up @@ -347,24 +327,31 @@ private void writeDataToSpreadsheet(ResourceLoader resource, Map<String, List<Ma
// rowNum increment by one to proceed with next record of the method.
rowNum++;

Object outputValue = methodData.get(ACTUAL_RESULT);
if (outputValue != null) {
Object actualResult = methodData.get(ACTUAL_RESULT);
if (actualResult != null) {
// getting no.of columns in the record
int columnNum = methodData.size();
if (!isActualResultHeaderWritten) {
Integer recordNum = getMethodRowNumFromExcel(sheet, methodName);
if (recordNum != null) {
// Write the actual result and test status headers.
writeDataToCell(sheet, recordNum, columnNum, ACTUAL_RESULT);
writeDataToCell(sheet,recordNum,columnNum+1,TEST_STATUS);
rowNum = rowNum + recordNum;
isActualResultHeaderWritten = true;
}
}
LOG.debug("rowNum:" + rowNum);

// column no is incremented by 2 because first column is null as per test data method structure
// and we need to write data next to last non-empty column
LOG.info("rowNum:" + rowNum);

// Write the actual result and test status values.
if(isActualResultHeaderWritten){
writeDataToCell(sheet, rowNum, columnNum, outputValue.toString());
LOG.debug("actualResult:" + actualResult.toString());
writeDataToCell(sheet, rowNum, columnNum, actualResult.toString());
Object testStatus = methodData.get(TEST_STATUS);
if(testStatus != null){
LOG.debug("testStatus:" + testStatus.toString());
writeDataToCell(sheet,rowNum,columnNum+1,testStatus.toString());
}
}

}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/easetech/easytest/loader/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ public interface Loader {
*/
String ACTUAL_RESULT = "ActualResult";

/**
* The key identifying the expected output that needs to be compared with actual result
*/
String EXPECTED_RESULT = "ExpectedResult";

/**
* The key identifying the Test Status either PASSED/FAILED
* determined after comparing expected and actual results, and written to the file.
*/
String TEST_STATUS = "TestStatus";

/**
* The constants for test status PASSED/FAILED
*/
final String TEST_PASSED = "PASSED";
final String TEST_FAILED = "FAILED";

/**
* Method responsible to Load the test data from the list of files passed as parameter
* @param filePaths the list of files from which to load the data
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/org/easetech/easytest/runner/DataDrivenTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package org.easetech.easytest.runner;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -507,9 +506,12 @@ public Object createTest() throws Exception {
* <ol>
* After the method has been invoked explosively, the returned value is checked. If there is a return value:
* <li>We get the name of the method that is currently executing,
* <li>We find teh exact place in the test input data for which this method was executed,
* <li>We find the exact place in the test input data for which this method was executed,
* <li>We put the returned result in the map of input test data. The entry in the map has the key :
* {@link Loader#ACTUAL_RESULT} and the value is the returned value by the test method.
* <li>If expected result{@link Loader#EXPECTED_RESULT} exist in user input data then we compare it with actual result and
* put the test status either passed/failed. The entry in the map has the key :
* {@link Loader#TEST_STATUS} and the value is the either PASSED or FAILED.
*
* We finally write the test data to the file.
*
Expand All @@ -528,17 +530,37 @@ public void evaluate() throws Throwable {
Object returnObj = method.invokeExplosively(freshInstance, values);
if (returnObj != null) {
LOG.debug("returnObj:" + returnObj);
//checking and assigning the map method name.
if (!mapMethodName.equals(method.getMethod().getName())) {
// if mapMethodName is not same as the current executing method name
// then assign that to mapMethodName to write to writableData
mapMethodName = method.getMethod().getName();
// initialize the row number.
rowNum = 0;
}
LOG.debug("mapMethodName:" + mapMethodName + " ,rowNum:" + rowNum);
if (writableData.get(mapMethodName) != null) {
LOG.debug("writableData.get(mapMethodName)" + writableData.get(mapMethodName)
+ " ,rowNum:" + rowNum);
writableData.get(mapMethodName).get(rowNum++).put(Loader.ACTUAL_RESULT, returnObj);
Map<String,Object> writableRow = writableData.get(mapMethodName).get(rowNum);
writableRow.put(Loader.ACTUAL_RESULT, returnObj);

Object expectedResult = writableRow.get(Loader.EXPECTED_RESULT);
// if expected result exist in user input test data,
// then compare that with actual output result
// and write the status back to writable map data.
if(expectedResult != null) {
LOG.debug("Expected result exists");
if(expectedResult.toString().equals(returnObj.toString())){
writableRow.put(Loader.TEST_STATUS,Loader.TEST_PASSED);
} else {
writableRow.put(Loader.TEST_STATUS,Loader.TEST_FAILED);
}
}
rowNum++;
}


}
} catch (CouldNotGenerateValueException e) {
// ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* An extension of {@link RunAfters} method to write
Expand All @@ -19,6 +21,11 @@
public class RunAftersWithOutputData extends RunAfters {

/**
* An instance of logger associated with the test framework.
*/
protected static final Logger LOG = LoggerFactory.getLogger(RunAftersWithOutputData.class);

/**
* An instance of {@link Loader} responsible for writing the data to the file.
*/
private final Loader loader;
Expand Down Expand Up @@ -78,6 +85,7 @@ public RunAftersWithOutputData(Statement next, List<FrameworkMethod> afters, Obj
*/
@Override
public void evaluate() throws Throwable {
LOG.info("evaluate started");
List<Throwable> errors = new ArrayList<Throwable>();
try {
fNext.evaluate();
Expand All @@ -92,10 +100,13 @@ public void evaluate() throws Throwable {
}
}
MultipleFailureException.assertEmpty(errors);
//Write any output test data to the file only if there is a write data associated with the test method.
// Write any output test data to the file only if there is a write data associated with the test method.
if (loader != null && filePath.length > 0) {
LOG.debug("Loader:"+loader+", filePath:"+filePath[0]);
LOG.debug("writableData:"+writableData);
loader.writeData(filePath[0], writableData);
}
LOG.info("evaluate finished");
}

}

0 comments on commit f569a81

Please sign in to comment.