All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codeborne.xlstest.matchers.ContainsRow Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.codeborne.xlstest.matchers;

import com.codeborne.xlstest.XLS;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.hamcrest.Description;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

import static java.util.Arrays.asList;

public class ContainsRow extends XLSMatcher {
  private final String[] cellTexts;

  public ContainsRow(String... cellTexts) {
    this.cellTexts = cellTexts;
  }

  @Override
  protected boolean matchesSafely(XLS item) {
    for (int i = 0; i < item.excel.getNumberOfSheets(); i++) {
      Sheet sheet = item.excel.getSheetAt(i);
      for (Row row : sheet) {
        Queue expectedTexts = new LinkedList<>(asList(cellTexts));
        for (Cell cell : row) {
          String expectedText = expectedTexts.peek();
          if (getFormattedCellValue(cell).contains(expectedText)) {
            expectedTexts.remove();
          }
          if (expectedTexts.isEmpty()) return true;
        }
      }
    }
    return false;
  }

  @Override
  public void describeTo(Description description) {
    description.appendText("a XLS containing row ").appendValue(Arrays.toString(cellTexts));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy