/*
 * Created on 03-Mar-2005
 */
package org.bodington.util;

import org.bodington.server.BuildingServer;
import org.bodington.server.BuildingServerException;
import org.bodington.server.BuildingServerTest;
import org.bodington.server.resources.Resource;
import org.bodington.server.resources.ResourceTreeManager;

/**
 * @author buckett
 */
public class BodingtonURLTest extends BuildingServerTest
{

    BodingtonURL bodingtonURL;
    /*
     * @see BuildingServerTest#setUp()
     */
    protected void setUp() throws Exception
    {
        super.setUp();

        bodingtonURL = new BodingtonURL("http", "localhost", 8080, "/bodington/site/");
    }



    /*
     * Class under test for Resource getResource(PrimaryKey)
     */
    public void testGetResourcePrimaryKey()
    {
        Resource root = bodingtonURL.getResource(resource.getPrimaryKey());
        assertTrue(root.equals(resource));     
    }
    
    /*
     * Class under test for Resource getResource(String)
     */
    public void testGetResourceString() throws Exception
    {
       Resource root = bodingtonURL.getResource("http://localhost:8080/bodington/site/");
       assertNotNull(root);
       assertTrue((root.equals(resource)));
    }
    
    /*
     * Class under test for String getResourceUrl(Resource)
     */
    public void testGetResourceUrlResource()
    {
        String url = bodingtonURL.getResourceUrl(resource);
        assertTrue(url.equals("http://localhost:8080/bodington/site/"));
    }

    public void testIsBodingtonUrl()
    {
        assertTrue(bodingtonURL.isBodingtonUrl("http://localhost:8080/bodington/site/"));
    }

    public void testGetResourceName() throws BuildingServerException
    {
        String name = bodingtonURL.getResourceName("http://localhost:8080/bodington/site/");
        assertTrue(name.equals("/"));
        name = bodingtonURL.getResourceName("http://localhost:8080/bodington/");
        assertNull(name);
        name = bodingtonURL.getResourceName("http://localhost:8080");
        assertNull(name);
        name = bodingtonURL.getResourceName("http://localhost");
        assertNull(name);
        
        BuildingServer.getInstance().getContext().setUser(user);
        Resource child = new Resource("name", "title", "introduction", "description");
        ResourceTreeManager.getInstance().addResource(resource, child);
        
        name = bodingtonURL.getResourceName("http://localhost:8080/bodington/site/name/");
        assertEquals("/name/", name);

        
    }
    
    public void testIsParseWarningNull()
    {
        bodingtonURL.parseHtml(null);
        assertFalse(bodingtonURL.isParseWarning());
    }
    
    public void testParseHtml()
    {
        assertEquals ("hello", bodingtonURL.parseHtml("hello"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }
    
    public void testParseHtmlGoodLink()
    {
        assertEquals ("<a href='http://localhost:8080/bodington/site/?res_id="+resource.getResourceId()+ "'>hello</a>",
            bodingtonURL.parseHtml("<a href='http://localhost:8080/bodington/site/'>hello</a>"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }
    
    public void testParseHtmlLink()
    {
        assertEquals ("<a href='http://www.google.com'>Google</a>", bodingtonURL.parseHtml("<a href='http://www.google.com'>Google</a>"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }
    
    public void testParseHtmlLocalLink()
    {
        assertEquals("<a href='page'>page</a>", bodingtonURL.parseHtml("<a href='page'>page</a>"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }
    
    public void testParseHtmlCaseDifference()
    {
        assertEquals("<a hREf='page'>page</a>", bodingtonURL.parseHtml("<a hREf='page'>page</a>"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }
    
    public void testParseHtmlWithWhitespace()
    {
        assertEquals("<a href =   'page'>page</a>", bodingtonURL.parseHtml("<a href =   'page'>page</a>"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }
    
    public void testParseHtmlHref()
    {
        assertEquals("Use an href for linking", bodingtonURL.parseHtml("Use an href for linking"));
        assertEquals(0, bodingtonURL.getParseWarning().size());
    }

}