1   package org.apache.maven.plugin.linkcheck;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import org.apache.log4j.BasicConfigurator;
22  import org.apache.log4j.Level;
23  import org.apache.log4j.Logger;
24  
25  import java.io.File;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * @author Ben Walding
34   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
35   */
36  public class LinkCheckTest extends TestCase
37  {
38      String baseDir;
39  
40      /*
41       * (non-Javadoc)
42       * 
43       * @see junit.framework.TestCase#setUp()
44       */
45      protected void setUp() throws Exception
46      {
47          super.setUp();
48          this.baseDir = System.getProperty( "basedir" );
49          // Setup log4J (We are in a forked JVM for tests)
50          BasicConfigurator.configure();
51          Logger.getLogger( "org.apache.maven.plugin.linkcheck" ).setLevel( Level.DEBUG );
52      }
53  
54      public void testScan() throws Exception
55      {
56          File f = new File( this.baseDir + "/src/test-resources" );
57          LinkCheck lc = new LinkCheck();
58          lc.setBasedir( f );
59          lc.setOutput( new File( this.baseDir + "/target/linkcheck.xml" ) );
60          lc.setOutputEncoding( "ISO8859-1" );
61          lc.setCache( System.getProperty( "maven.linkcheck.cache" ) );
62          lc.setExclude( "http://cvs.apache.org/viewcvs.cgi/maven-pluginszz/,"
63                          + "http://cvs.apache.org/viewcvs.cgi/mavenzz/" );
64          lc.doExecute();
65  
66          Iterator iter = lc.getFiles().iterator();
67          Map map = new HashMap();
68          while ( iter.hasNext() )
69          {
70              FileToCheck ftc = (FileToCheck) iter.next();
71              map.put( ftc.getName(), ftc );
72          }
73  
74          assertEquals( "files.size()", 8, lc.getFiles().size() );
75  
76          check( map, "nolink.html", 0 );
77          check( map, "test-resources/nolink.html", 0 );
78          check( map, "test-resources/test1/test1.html", 2 );
79          check( map, "test-resources/test1/test2.html", 0 );
80          check( map, "test1/test1.html", 1 );
81          check( map, "testA.html", 3 );
82  
83          /* test excludes */
84          String fileName = "testExcludes.html";
85          check( map, fileName, 2 );
86  
87          FileToCheck ftc = (FileToCheck) map.get( fileName );
88          assertEquals( "Excluded links", 2, ftc.getSuccessful() );
89  
90          // index-all.html should get parsed, but is currently having problems.
91          // There are 805 distinct links in this page
92          check( map, "index-all.html", 805 );
93  
94      }
95  
96      private void check( Map map, String name, int linkCount )
97      {
98          FileToCheck ftc;
99  
100         ftc = (FileToCheck) map.get( name );
101         assertNotNull( name, ftc );
102 
103         assertEquals( name + ".getLinks().size()", linkCount, ftc.getResults().size() );
104     }
105 
106 }