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

fitnesse.fixtures.PagesRunInSuite Maven / Gradle / Ivy

package fitnesse.fixtures;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PagesRunInSuite {
  private String prefix;
  private String testSystemID;

  public PagesRunInSuite(String prefix, String testSystemID) {
    this.prefix = prefix;
    this.testSystemID = testSystemID;
  }

  public List query() throws Exception {
    return buildQueryResponse(getPagesRunInSuite());

  }

  private List getPagesRunInSuite() throws Exception {
    List testPages = new ArrayList<>();

    String content = FitnesseFixtureContext.sender.sentData();
    if (content.contains("Testing was interrupted and results are incomplete.")) {
      throw new Exception("Test result page says: Testing was interrupted and results are incomplete.");
    }
    String[] testSystems = content.split("slim:");
    for (String testSystem : testSystems) {
      addPagesForThisTestSystem(testPages, testSystem);
    }
    return testPages;
  }

  private void addPagesForThisTestSystem(List testPages, String testSystem) {
    if (testSystem.startsWith(testSystemID)) {
      String[] pageString = testSystem.split("test_name\">");
      addPages(testPages, pageString);
    }
  }

  private void addPages(List testPages, String[] pageString) {
    for (int i = 1; i < pageString.length; i++) {
      int fragmentEnd = pageString[i].indexOf("<");
      String testPage = pageString[i].substring(0, fragmentEnd);
      if (testPage.startsWith(prefix)) {
        testPages.add(testPage);
      }
    }
  }

  private List buildQueryResponse(List testPages) {
    List rows = new ArrayList<>();
    for (String testPage : testPages) {
      List columns = new ArrayList<>();
      List pageNameCell = Arrays.asList("page name", testPage);
      columns.add(pageNameCell);
      rows.add(columns);
    }
    return rows;
  }
}