package fitnesse.testrunner;

import fitnesse.wiki.*;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertEquals;

import fitnesse.wiki.mem.InMemoryPage;
import org.junit.Before;
import org.junit.Test;

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

public class PageListSetUpTearDownSurrounderTest {
  private WikiPage root;
  private WikiPage suite;
  private WikiPage testPage;

  private PageListSetUpTearDownSurrounder surrounder;

  @Before
  public void setUp() throws Exception {
    root = InMemoryPage.makeRoot("RooT");
    PageData data = root.getData();
    root.commit(data);
    suite = WikiPageUtil.addPage(root, PathParser.parse("SuitePageName"), "The is the test suite\n");
    testPage = WikiPageUtil.addPage(suite, PathParser.parse("TestPage"), "My test and has some content");
    surrounder = new PageListSetUpTearDownSurrounder(root);
  }

  @Test
  public void testPagesForTestSystemAreSurroundedByRespectiveSuiteSetupAndTeardown() throws Exception {
    WikiPage slimPage = WikiPageUtil.addPage(testPage, PathParser.parse("SlimPageTest"));
    WikiPage setUp = WikiPageUtil.addPage(root, PathParser.parse("SuiteSetUp"));
    WikiPage tearDown = WikiPageUtil.addPage(root, PathParser.parse("SuiteTearDown"));
    WikiPage setUp2 = WikiPageUtil.addPage(slimPage, PathParser.parse("SuiteSetUp"));
    WikiPage tearDown2 = WikiPageUtil.addPage(slimPage, PathParser.parse("SuiteTearDown"));

    List<WikiPage> testPages = surrounder.surroundGroupsOfTestPagesWithRespectiveSetUpAndTearDowns(makeTestPageList());

    assertEquals(6, testPages.size());

    assertEquals(setUp2, testPages.get(0));
    assertEquals(slimPage, testPages.get(1));
    assertEquals(tearDown2, testPages.get(2));
    assertEquals(setUp, testPages.get(3));
    assertEquals(testPage, testPages.get(4));
    assertEquals(tearDown, testPages.get(5));
  }

    private ArrayList<WikiPage> makeTestPageList() throws Exception {
        SuiteContentsFinder finder = new SuiteContentsFinder(suite, null, root);
        ArrayList<WikiPage> testPages = new ArrayList<WikiPage>();
        for (WikiPage page : finder.getAllPagesToRunForThisSuite()) testPages.add(page);
        return testPages;
    }

  @Test
  public void testSetUpAndTearDown() throws Exception {
    WikiPage setUp = WikiPageUtil.addPage(root, PathParser.parse("SuiteSetUp"), "suite set up");
    WikiPage tearDown = WikiPageUtil.addPage(root, PathParser.parse("SuiteTearDown"), "suite tear down");

    List<WikiPage> testPages = surrounder.surroundGroupsOfTestPagesWithRespectiveSetUpAndTearDowns(makeTestPageList());
    assertEquals(3, testPages.size());
    assertEquals(setUp, testPages.get(0));
    assertEquals(testPage, testPages.get(1));
    assertEquals(tearDown, testPages.get(2));
  }
}

@@ -1593,25 +1593,24 @@
 es.size());%0A
-%0A
     assertEq
@@ -1611,105 +1611,40 @@
 sert
-Equals(setUp2, testPages.get(0));%0A    assertEquals(slimPage, testPages.get(1));%0A    assertEquals(
+PageIsBetween(slimPage, setUp2, 
 tear
@@ -1655,31 +1655,24 @@
 2, testPages
-.get(2)
 );%0A    asser
@@ -1676,104 +1676,39 @@
 sert
-Equals(setUp, testPages.get(3));%0A    assertEquals(testPage, testPages.get(4));%0A    assertEquals(
+PageIsBetween(testPage, setUp, 
 tear
@@ -1726,15 +1726,8 @@
 ages
-.get(5)
 );%0A 
@@ -2635,11 +2635,271 @@
 2));%0A  %7D
+%0A%0A  private void assertPageIsBetween(WikiPage curr, WikiPage prev, WikiPage next,%0A          List%3CWikiPage%3E pages) %7B%0A    int currIndex = pages.indexOf(curr);%0A    assertEquals(prev, pages.get(currIndex - 1));%0A    assertEquals(next, pages.get(currIndex + 1));%0A  %7D
 %0A%7D%0A

Fix PageListSetUpTearDownSurrounderTest Assert that pages are surrounded by suite setup/teardown without expecting pre-defined ordering of all pages (since the latter is not guaranteed)