Java tutorial
package nl.rabobank.fixtures.aiep.testdata.traffic; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.google.common.collect.Lists; /** LicensePlateDataLoader * @author K.Y.Cheung@rn.rabobank.nl, J.J.B.Dieten@rn.rabobank.nl * */ public class LicensePlateDataLoader implements TestDataLoader { private String testStubObject = null; private String licenseplate = null; private String brand = null; private String model = null; private String type = null; private String gear = null; private String fuel = null; private String productionYear = null; private String purchaseYear = null; private String sideCarValue = null;//For motor only private String chassisNumber = null;//For Trailer only private String weight = null;//For Camper only private String purchaseValue = null;//For Camper only private String insuredInventory = null;//For Camper only private String sameLicensePlateAsCar = null; //For Trailer only private String catalogueValue = null; private String bodyShape = null; public boolean isTest = false; public String getSameLicensePlateAsCar() { return sameLicensePlateAsCar; } public void setSameLicensePlateAsCar(String sameLicensePlateAsCar) { this.sameLicensePlateAsCar = sameLicensePlateAsCar; } public String getCatalogueValue() { return catalogueValue; } public void setCatalogueValue(String catalogueValue) { this.catalogueValue = catalogueValue; } public String getBodyShape() { return bodyShape; } public void setBodyShape(String bodyShape) { this.bodyShape = bodyShape; } public String getChassisNumber() { return chassisNumber; } public void setChassisNumber(String chassisNumber) { this.chassisNumber = chassisNumber; } public String getSideCarValue() { return sideCarValue; } public void setSideCarValue(String sideCarValue) { this.sideCarValue = sideCarValue; } public void setTestStubObject(String testname) throws MalformedURLException, IOException { this.testStubObject = testname; fetchTestData(); } public String getBrand() { return this.brand; } public void setBrand(String brand) { this.brand = brand; } public String getModel() { return this.model; } public void setModel(String model) { this.model = model; } public String getLicenseplate() { return licenseplate; } public void setLicenseplate(String licenseplate) { this.licenseplate = licenseplate; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getGear() { return gear; } public void setGear(String gear) { this.gear = gear; } public String getProductionYear() { return productionYear; } public void setProductionYear(String productionYear) { this.productionYear = productionYear; } public String getFuel() { return fuel; } public void setFuel(String fuel) { this.fuel = fuel; } public String getPurchaseYear() { return purchaseYear; } public void setPurchaseYear(String purchaseYear) { this.purchaseYear = purchaseYear; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } public String getPurchaseValue() { return purchaseValue; } public void setPurchaseValue(String purchaseValue) { this.purchaseValue = purchaseValue; } public String getInsuredInventory() { return insuredInventory; } public void setInsuredInventory(String insuredInventory) { this.insuredInventory = insuredInventory; } public void fetchTestData() throws MalformedURLException, IOException { InputStream excelFileToRead = null; if (isTest) { excelFileToRead = getClass().getResourceAsStream("/aiep_testdata_fitnesse.xlsx"); } else { excelFileToRead = new URL(TestDataLoader.TESTDATAPATH).openStream(); } XSSFWorkbook wb = new XSSFWorkbook(excelFileToRead); XSSFSheet sheet = wb.getSheetAt(0); Iterator<Row> rows = sheet.rowIterator(); boolean newRow = true; List<Row> myRowList = Lists.newArrayList(rows); if (this.testStubObject != null) { for (Row row : myRowList) { Iterator<Cell> cells = row.cellIterator(); List<Cell> myRowCellsList = Lists.newArrayList(cells); for (Cell cell : myRowCellsList) { cell.setCellType(Cell.CELL_TYPE_STRING); if (cell.getStringCellValue().equals(this.testStubObject) && cell.getColumnIndex() == 0) { newRow = false; } if (cell.getColumnIndex() == 1 && !newRow) { setLicenseplate(cell.getStringCellValue()); } if (cell.getColumnIndex() == 2 && !newRow) { setBrand(cell.getStringCellValue()); } if (cell.getColumnIndex() == 3 && !newRow) { setModel(cell.getStringCellValue()); } if (cell.getColumnIndex() == 4 && !newRow) { setType(cell.getStringCellValue()); } if (cell.getColumnIndex() == 5 && !newRow) { setGear(cell.getStringCellValue()); } if (cell.getColumnIndex() == 6 && !newRow) { setFuel(cell.getStringCellValue()); } if (cell.getColumnIndex() == 7 && !newRow) { setProductionYear(cell.getStringCellValue()); } if (cell.getColumnIndex() == 8 && !newRow) { setPurchaseYear(cell.getStringCellValue()); } if (cell.getColumnIndex() == 9 && this.testStubObject.startsWith("Motor") && !newRow) { setSideCarValue(cell.getStringCellValue()); } if (cell.getColumnIndex() == 10 && this.testStubObject.startsWith("Trailer")) { setChassisNumber(cell.getStringCellValue()); } if (cell.getColumnIndex() == 11 && !newRow) { setWeight(cell.getStringCellValue()); } if (cell.getColumnIndex() == 12 && !newRow) { setPurchaseValue(cell.getStringCellValue()); } if (cell.getColumnIndex() == 13 && !newRow) { setInsuredInventory(cell.getStringCellValue()); } if (cell.getColumnIndex() == 14 && !newRow) { setSameLicensePlateAsCar(cell.getStringCellValue()); } if (cell.getColumnIndex() == 15 && !newRow) { setCatalogueValue(cell.getStringCellValue()); } if (cell.getColumnIndex() == 16 && !newRow) { setBodyShape(cell.getStringCellValue()); } } if (!newRow) { break; } } } excelFileToRead.close(); } }