package org.apache.maven;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   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.
 * ====================================================================
 */

import java.io.File;
import java.io.FileInputStream;

import junit.framework.TestCase;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * @author <a href="bwalding@jakarta.org">Ben Walding</a>
 * @version $Id: NavBeanTest.java,v 1.5 2004/03/02 15:36:15 evenisse Exp $
 */
public class NavBeanTest extends TestCase
{

    private File testResources;
    NavBean nb;

    public void setUp() throws Exception
    {
        testResources = new File(System.getProperty("basedir") + "/src/test-resources");
        nb = new NavBean();
        nb.setDocument(getTestDocument());
    }

    class Test
    {
        String href;
        boolean collapsed;
        public Test(String href, boolean collapsed)
        {
            this.href = href;
            this.collapsed = collapsed;
        }
    }

 
    protected Document getTestDocument() throws Exception
    {
        SAXReader reader = new SAXReader();
        return reader.read(new FileInputStream(new File(testResources, "navigation.xml")));
    }

    public void testCollapsed() throws Exception
    {
        testCollapsed("/alpha/index.html", "/alpha/index.html", false);
        testCollapsed("/alpha/one/index.html", "/alpha/index.html", false);
        testCollapsed("/alpha/two/index.html", "/alpha/index.html", false);
        testCollapsed("/beta/index.html", "/alpha/index.html", true);

        testCollapsed("/alpha/index.html", "/beta/index.html", false);
        testCollapsed("/alpha/one/index.html", "/beta/index.html", false);
        testCollapsed("/alpha/two/index.html", "/beta/index.html", false);
        testCollapsed("/beta/index.html", "/beta/index.html", false);
        testCollapsed("/beta/one/index.html", "/beta/index.html", true);
        testCollapsed("/beta/two/index.html", "/beta/index.html", true);

        testCollapsed("/alpha/index.html", "beta/index.html", false);
        testCollapsed("/alpha/one/index.html", "beta/index.html", false);
        testCollapsed("/alpha/two/index.html", "beta/index.html", false);
        testCollapsed("/beta/index.html", "beta/index.html", false);
        testCollapsed("/beta/one/index.html", "beta/index.html", true);
        testCollapsed("/beta/two/index.html", "beta/index.html", true);
    }

    public void testCollapsed(String href, String location, boolean expected)
    {
        nb.setLocation(location);

        Node node = nb.getFirstNodeByHREF(href);
        assertNotNull(node);
        
        String s = "At location " + location + ", looking to generate link to " + href + ", expecting collapse = " + expected;
        assertEquals(s, expected, nb.isCollapsed(node));
    }
}
