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

fitnesse.testrunner.run.PageListSetUpTearDownSurrounder Maven / Gradle / Ivy

There is a newer version: 20241026
Show newest version
package fitnesse.testrunner.run;

import fitnesse.wiki.PageData;
import fitnesse.wiki.WikiPage;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Groups tests that have same SuiteSetUp and SuiteTearDown.
 * Ensure each of these groups are added to the final list of tests to run, directly surrounded by the appropriate
 * SuiteSetUp and -TearDown. This can mean a single SuiteSetUp or -TearDown is added multiple times: once for each
 * group that requires it.
 */
public class PageListSetUpTearDownSurrounder implements PageListSetUpTearDownProcessor {
  private final Map setUpsAndTearDowns = new HashMap<>();

  @Override
  public List addSuiteSetUpsAndTearDowns(List pageList) {
    Map> pageSetUpTearDownGroups = createPageSetUpTearDownGroups(pageList);
    return reinsertPagesViaSetUpTearDownGroups(pageSetUpTearDownGroups);
  }

  private Map> createPageSetUpTearDownGroups(List pageList) {
    Map> pageSetUpTearDownGroups = new LinkedHashMap<>();
    for (WikiPage page : pageList) {
      makeSetUpTearDownPageGroupForPage(page, pageSetUpTearDownGroups);
    }
    return pageSetUpTearDownGroups;
  }

  private void makeSetUpTearDownPageGroupForPage(WikiPage page, Map> pageSetUpTearDownGroups) {
    String group = getSetUpTearDownGroup(page);
    pageSetUpTearDownGroups.computeIfAbsent(group, g -> new LinkedList<>()).add(page);
  }

  private String getSetUpTearDownGroup(WikiPage page) {
    String setUpPath = getPathForSetUpTearDown(page, PageData.SUITE_SETUP_NAME);
    String tearDownPath = getPathForSetUpTearDown(page, PageData.SUITE_TEARDOWN_NAME);
    return setUpPath + "," + tearDownPath;
  }

  private String getPathForSetUpTearDown(WikiPage page, String setUpTearDownName) {
    String path = null;
    WikiPage suiteSetUpTearDown = page.getPageCrawler().getClosestInheritedPage(setUpTearDownName);
    if (suiteSetUpTearDown != null) {
      path = suiteSetUpTearDown.getFullPath().toString();
      setUpsAndTearDowns.putIfAbsent(path, suiteSetUpTearDown);
    }
    return path;
  }

  private List reinsertPagesViaSetUpTearDownGroups(Map> pageSetUpTearDownGroups) {
    List pageList = new LinkedList<>();
    for (Map.Entry> entry : pageSetUpTearDownGroups.entrySet()) {
      pageList.addAll(insertSetUpTearDownPageGroup(entry.getKey(), entry.getValue()));
    }
    return pageList;
  }

  private List insertSetUpTearDownPageGroup(String setUpAndTearDownGroupKey, List pageGroup) {
    List pageList = new LinkedList<>();
    pageList.addAll(setUpForThisGroup(setUpAndTearDownGroupKey));
    pageList.addAll(pageGroup);
    pageList.addAll(tearDownForThisGroup(setUpAndTearDownGroupKey));
    return pageList;
  }

  private List setUpForThisGroup(String setUpAndTearDown) {
    String setUpPath = setUpAndTearDown.split(",")[0];
    WikiPage setUpPage = setUpsAndTearDowns.get(setUpPath);
    return setUpPage != null ? Collections.singletonList(setUpPage) : Collections.emptyList();
  }

  private List  tearDownForThisGroup(String setUpAndTearDownGroupKey) {
    String tearDownPath = setUpAndTearDownGroupKey.split(",")[1];
    WikiPage tearDownPage = setUpsAndTearDowns.get(tearDownPath);
    return tearDownPage != null ? Collections.singletonList(tearDownPage) : Collections.emptyList();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy