package org.apache.maven.linkcheck;

/* ====================================================================
 *   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.util.HashMap;
import java.util.Iterator;
import java.util.Map;



import junit.framework.TestCase;

/**
 * @author Ben Walding
 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
 */
public class LinkCheckTest extends TestCase
{
  String baseDir;

  public void setUp()
  {
     baseDir = System.getProperty("basedir");
  }

  public void testScan() throws Exception
  {
    File f = new File(baseDir + "/src/test-resources");
    LinkCheck lc = new LinkCheck();
    lc.setBasedir(f);
    lc.setOutput(new File(baseDir + "/target/linkcheck.xml"));
    lc.setOutputEncoding("ISO8859-1");
    lc.setCache(baseDir + "/target/linkcheck-cache.xml");
    lc.setExclude("http://cvs.apache.org/viewcvs.cgi/maven-pluginszz/,"
            + "http://cvs.apache.org/viewcvs.cgi/mavenzz/");
    lc.doExecute();

    Iterator iter = lc.getFiles().iterator();
    Map map = new HashMap();
    while (iter.hasNext())
    {
      FileToCheck ftc = (FileToCheck) iter.next();
      map.put(ftc.getName(), ftc);
    }

    assertEquals("files.size()", 8, lc.getFiles().size());

    check(map, "nolink.html", 0);
    check(map, "test-resources/nolink.html", 0);
    check(map, "test-resources/test1/test1.html", 2);
    check(map, "test-resources/test1/test2.html", 0);
    check(map, "test1/test1.html", 1);
    check(map, "testA.html", 3);

    /* test excludes */
    String fileName = "testExcludes.html"; 
    check(map, fileName, 2);
    
    FileToCheck ftc = (FileToCheck) map.get(fileName);
    assertEquals("Excluded links", 2, ftc.getSuccessful());
    
    //index-all.html should get parsed, but is currently having problems.
    //check(map, "index-all.html", 1);

  }

  private void check(Map map, String name, int linkCount)
  {
    FileToCheck ftc;

    ftc = (FileToCheck) map.get(name);
    assertNotNull(name, ftc);

    /*if (ftc.getResults().size() != linkCount) {
        
    }*/

    assertEquals(name + ".getLinks().size()", linkCount, ftc.getResults().size());
  }

}
