package fr.devoxx.grandmere;

import com.google.common.io.Resources;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static com.google.common.base.Throwables.propagate;
import static org.assertj.core.api.Assertions.assertThat;

public class ExcelTest {
    @Test
    public void should_load_excel() {
        Workbook workbook = getWorkbook("perf.xlsx");
        Sheet perf = workbook.getSheetAt(0);
        for (int rowId = perf.getFirstRowNum(); rowId < perf.getLastRowNum(); rowId++) {
            Row row = perf.getRow(rowId);
            for (int column = 0; column < row.getLastCellNum(); column++) {
                assertThat(row.getCell(column)).isNotNull();
            }
        }
    }

    public static Workbook getWorkbook(String path) {
        try (InputStream input = openStream(path)) {
            if (!path.endsWith(".xls")) {
                return new XSSFWorkbook(input);
            } else {
                return new HSSFWorkbook(input);
            }
        } catch (IOException e) {
            throw propagate(e);
        }
    }

    private static InputStream openStream(String path) throws IOException {
        try {
            return Resources.getResource(path).openStream();
        } catch (IllegalArgumentException e) {
            return new FileInputStream(path);
        }
    }

}

@@ -152,98 +152,9 @@
 del.
-Row;%0Aimport org.apache.poi.ss.usermodel.Sheet;%0Aimport org.apache.poi.ss.usermodel.Workbook
+*
 ;%0Aim
@@ -832,66 +832,1578 @@
     
-assertThat(row.getCell(column)).isNotNull();%0A            %7D
+Cell cell = row.getCell(column);%0A                System.out.println(cell);%0A                assertThat(cell).isNotNull();%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void should_load_execute() %7B%0A        Workbook workbook = getWorkbook(%22perf.xlsx%22);%0A        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();%0A%0A        Sheet perf = workbook.getSheetAt(0);%0A        for (int rowId = perf.getFirstRowNum(); rowId %3C perf.getLastRowNum(); rowId++) %7B%0A            Row row = perf.getRow(rowId);%0A            for (int column = 0; column %3C row.getLastCellNum(); column++) %7B%0A                String cell = cell(row.getCell(column), evaluator);%0A                System.out.println(cell);%0A                assertThat(cell).describedAs(%22Cell %22 + rowId + %22:%22 + column).isNotNull();%0A            %7D%0A        %7D%0A    %7D%0A%0A    private String cell(Cell cell, FormulaEvaluator evaluator) %7B%0A        switch(cell.getCellType()) %7B%0A            case Cell.CELL_TYPE_FORMULA:%0A                return evaluator.evaluate(cell).formatAsString();%0A            case Cell.CELL_TYPE_NUMERIC:%0A                return %22%22 + cell.getNumericCellValue();%0A            case Cell.CELL_TYPE_STRING:%0A                return cell.getStringCellValue();%0A            case Cell.CELL_TYPE_BOOLEAN:%0A                return %22%22 + cell.getBooleanCellValue();%0A            case Cell.CELL_TYPE_BLANK:%0A                return %22%22;%0A            case Cell.CELL_TYPE_ERROR:%0A                return null;%0A            default:%0A                throw new IllegalStateException(%22Could not handle %22 + cell.getCellType());
 %0A   

step5:execute