/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

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());
    }

}
