import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Javatest93 {
    /**
     * 反射
     */
    public static void main(String[] args) {
        List<TestCase> list = new ArrayList<>();
        File file = new File("D:\\testjavaIO\\test11\\testcase.xlsx");
        String sheetName = "Sheet1";
        try {
            //XSSFWorkbook 是读取2007以上版本的表格,及.xlsx结尾,所以使用XSSFWorkbook
            //HSSFWorkbook 是读取2003版本的表格,及.xls结尾
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
            //获取表
            Sheet sheet = xssfWorkbook.getSheet(sheetName);
            //遍历表格
            //起始行,第一行是标题栏不需要读取
            int firstRowIndex = sheet.getFirstRowNum() + 1;
            //结束行
            int lastRowIndex = sheet.getLastRowNum();
            //Row title = sheet.getRow(0);
            //遍历行
            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
                Row row = sheet.getRow(rIndex);
                if(row != null){
                    //存储遍历的每行用例的元素值
                    String[] listcase = new String[row.getLastCellNum() - row.getFirstCellNum()];
                    //起始列
                    int firstCellIndex = row.getFirstCellNum();
                    //结束列
                    int lastCellIndex = row.getLastCellNum();
                    for (int cIndex = firstCellIndex; cIndex <= lastCellIndex; cIndex++) {
                        Cell cell = row.getCell(cIndex);
                        if(cell != null){
                            listcase[cIndex] = cell.toString();
                        }
                    }
                    //反射,将每条测试用例存储到一个testcase对象
                    try {
                        //获取类的 Class 对象实例
                        Class clz = Class.forName("TestCase");
                        //根据 Class 对象实例获取 Constructor 对象
                        Constructor TestCaseConstructor = clz.getConstructor();
                        //使用 Constructor 对象的 newInstance 方法获取反射类对象
                        Object cls = TestCaseConstructor.newInstance();
                        //按方法名,获取类中的方法
                        Method setCaseIdMethod = clz.getMethod("setCaseId", String.class);
                        //利用 invoke 方法调用方法
                        setCaseIdMethod.invoke(cls, listcase[0]);
                        Method setDescribeMethod = clz.getMethod("setDescribe", String.class);
                        setDescribeMethod.invoke(cls,listcase[1]);
                        Method setUrlMethod = clz.getMethod("setUrl", String.class);
                        setUrlMethod.invoke(cls,listcase[2]);
                        Method setMethod = clz.getMethod("setMethod", String.class);
                        setMethod.invoke(cls,listcase[3]);
                        Method setParametersMethod = clz.getMethod("setParameters", String.class);
                        setParametersMethod.invoke(cls,listcase[4]);
                        list.add((TestCase) cls);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (TestCase s:list) {
            System.out.println(s.toString());
        }
    }
}