Пример #1
0
  /**
   * Write the data for the given method to the excel sheet
   *
   * @param resource
   * @param methodNameForDataLoad
   * @param data
   * @throws IOException
   */
  private void writeDataToSpreadsheet(
      Resource resource, String methodNameForDataLoad, Map<String, List<Map<String, Object>>> data)
      throws IOException {

    LOG.debug("writeDataToSpreadsheet started" + resource.toString() + data);

    Workbook workbook;
    try {

      workbook = WorkbookFactory.create(new POIFSFileSystem(resource.getInputStream()));

    } catch (Exception e) {
      LOG.error("Error creating WorkbookFactory for resource " + resource.toString(), e);
      throw new IOException();
    }

    Sheet sheet = workbook.getSheetAt(0);

    Integer recordNum = getMethodRowNumFromExcel(sheet, methodNameForDataLoad);
    // if record doesn't exist then return without writing any thing
    if (recordNum == null) {
      LOG.error("Method doesn't exist in the excel:" + methodNameForDataLoad);
      return;
    }
    int columnNum = sheet.getRow(recordNum).getLastCellNum();
    int rowNum = 0;
    boolean isActualResultHeaderWritten = false;
    boolean isTestDurationHeaderWritten = false;
    boolean isHeaderRowNumIncremented = false;

    for (Map<String, Object> methodData : data.get(methodNameForDataLoad)) {
      // rowNum increment by one to proceed with next record of the method.
      rowNum++;

      Object testDuration = methodData.get(DURATION);
      if (testDuration != null) {

        if (!isTestDurationHeaderWritten && recordNum != null) {
          // Write the test duration header.
          writeDataToCell(sheet, recordNum, columnNum, DURATION);
          // increment the rowNum
          rowNum = rowNum + recordNum;
          isTestDurationHeaderWritten = true;
        }

        // Write the actual result and test status values.
        if (isTestDurationHeaderWritten) {
          LOG.debug("testDuration:" + testDuration.toString());
          writeDataToCell(sheet, rowNum, columnNum, testDuration.toString());
        }
      }
      if (!isHeaderRowNumIncremented
          && (isTestDurationHeaderWritten || isActualResultHeaderWritten)) {

        isHeaderRowNumIncremented = true;
      }

      Object actualResult = methodData.get(ACTUAL_RESULT);
      Object testStatus = methodData.get(TEST_STATUS);
      if (actualResult != null) {

        if (!isActualResultHeaderWritten && recordNum != null) {
          // Write the actual result and test status headers.
          writeDataToCell(sheet, recordNum, columnNum + 1, ACTUAL_RESULT);
          if (testStatus != null) writeDataToCell(sheet, recordNum, columnNum + 2, TEST_STATUS);
          // rowNum = rowNum + recordNum;
          isActualResultHeaderWritten = true;
        }
        LOG.debug("rowNum:" + rowNum);

        // Write the actual result and test status values.
        if (isActualResultHeaderWritten) {
          LOG.debug("actualResult:" + actualResult.toString());
          // trim actual result to 30KB if it is more than that
          actualResult = trimActualResult(actualResult.toString());
          writeDataToCell(sheet, rowNum, columnNum + 1, actualResult.toString());

          if (testStatus != null) {
            // Check against trimmed actual result
            Object expectedResult = methodData.get(EXPECTED_RESULT);
            testStatus =
                expectedResult.toString().equals(actualResult.toString())
                    ? Loader.TEST_PASSED
                    : Loader.TEST_FAILED;
            LOG.debug("testStatus:" + testStatus.toString());
            writeDataToCell(sheet, rowNum, columnNum + 2, testStatus.toString());
          }
        }
      }
    }

    // Write the output to a file
    workbook.write(resource.getOutputStream());
    LOG.debug("writeDataToSpreadsheet finished");
  }