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 junit.framework.TestCase;

import org.dom4j.Document;
import org.dom4j.Node;

import org.dom4j.io.SAXReader;

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


/**
 * NavBean Tests class.
 *
 * @author <a href="mailto:bwalding@jakarta.org">Ben Walding</a>
 * @version $Id$
 */
public class NavBeanTest extends TestCase
{
  //~ Instance fields ----------------------------------------------------------

  /** Test resources file. */
  private File testResources;

  /** NavBean instance to test */
  private NavBean nb;

  //~ Methods ------------------------------------------------------------------

  /**
   * Tests settings.
   *
   * @throws Exception If a problem occurs
   */
  public void setUp()
    throws Exception
  {
    testResources =
      new File(System.getProperty("basedir") + "/src/test-resources");
    nb = new NavBean();
    nb.setDocument(getTestDocument());
  }

  /**
   * Test.
   *
   * @param href href
   * @param location location
   * @param expected Result expected
   */
  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));
  }

  /**
   * tests suite.
   *
   * @throws Exception DOCUMENT ME!
   */
  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);
  }

  /**
   * Get the navigation.xml to test.
   *
   * @return XML Document
   *
   * @throws Exception If a problem occurs.
   */
  protected Document getTestDocument()
    throws Exception
  {
    SAXReader reader = new SAXReader();

    return reader.read(
      new FileInputStream(new File(
          testResources,
          "navigation.xml")));
  }

  //~ Inner Classes ------------------------------------------------------------

  /**
   * Test
   */
  class Test
  {
    //~ Instance fields --------------------------------------------------------

    /** Href. */
    private String href;

    /** Collapsed. */
    private boolean collapsed;

    //~ Constructors -----------------------------------------------------------

    /**
     * Creates a new Test object.
     *
     * @param newHref HREF.
     * @param newCollapsed Status.
     */
    public Test(
      String  newHref,
      boolean newCollapsed)
    {
      this.href        = newHref;
      this.collapsed   = newCollapsed;
    }
  }
}
