package org.codehaus.xsite.extractors;

import static org.junit.Assert.assertEquals;

import org.codehaus.xsite.extractors.sitemesh.rules.AddFirstChildClassToHeader;
import org.codehaus.xsite.io.CommonsFileSystem;
import org.codehaus.xsite.model.Page;
import org.junit.Test;

import com.opensymphony.module.sitemesh.html.TagRule;
import com.opensymphony.module.sitemesh.html.TextFilter;

/**
 * @author J&ouml;rg Schaible
 */
public class SiteMeshPageExtractorTest {

    private SiteMeshPageExtractor pageExtractor = new SiteMeshPageExtractor(
                new TagRule[]{new AddFirstChildClassToHeader()}, new TextFilter[0], new CommonsFileSystem());

    @Test
    public void canExtractPageBodyStartingWithHeader() {
        final String html = "<html><head><title>JUnit</title></head><body><h1>Header</h1></body></html>";
        final Page page = pageExtractor.extractPage("JUnit.html", html);
        assertEquals("JUnit", page.getTitle());
        assertEquals("<h1 class=\"FirstChild\">Header</h1>", page.getBody());
    }
    
    @Test
    public void canExtractPagesWithoutSharingLinksBetweenPages() {
        final String html1 = "<html><head><title>JUnit</title></head><body><a href=\"foo.html\">Header</a></body></html>";
        final String html2 = "<html><head><title>Test</title></head><body><p>Header</p></body></html>";
        final Page page1 = pageExtractor.extractPage("JUnit.html", html1);
        final Page page2 = pageExtractor.extractPage("Test.html", html2);
        assertEquals(1, page1.getLinks().size());
        assertEquals(0, page2.getLinks().size());
    }
}

@@ -379,16 +379,83 @@
 tFilter;
+%0Aimport com.opensymphony.module.sitemesh.html.rules.TagReplaceRule;
 %0A%0A/**%0A *
@@ -635,16 +635,34 @@
 gRule%5B%5D%7B
+%0A                %09
 new AddF
@@ -685,16 +685,82 @@
 Header()
+,%0A                %09new TagReplaceRule(%22ul%22, %22ol%22)%0A                
 %7D, new T
@@ -1742,10 +1742,415 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void canExtractPageWithReplacedParagraphTags() %7B%0A        final String html = %22%3Chtml%3E%3Chead%3E%3Ctitle%3EJUnit%3C/title%3E%3C/head%3E%3Cbody%3E%3Cdiv%3E%3Cul%3E%3Cli%3EHeader%3C/li%3E%3C/ul%3E%3C/div%3E%3C/body%3E%3C/html%3E%22;%0A        final Page page = pageExtractor.extractPage(%22JUnit.html%22, html);%0A        assertEquals(%22JUnit%22, page.getTitle());%0A        assertEquals(%22%3Cdiv%3E%3Col%3E%3Cli%3EHeader%3C/li%3E%3C/ol%3E%3C/div%3E%22, page.getBody());%0A    %7D%0A
 %7D%0A

Test TagReplaceRule.