1   package org.apache.maven.doxia.linkcheck;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import org.apache.maven.doxia.linkcheck.model.LinkcheckFile;
28  import org.apache.maven.doxia.linkcheck.model.LinkcheckModel;
29  import org.codehaus.plexus.PlexusTestCase;
30  
31  /**
32   * @author Ben Walding
33   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
34   * @version $Id: LinkCheckTest.java 1030039 2010-11-02 13:33:03Z ltheussl $
35   */
36  public class LinkCheckTest
37      extends PlexusTestCase
38  {
39      /**
40       * @throws Exception
41       */
42      public void testScan()
43          throws Exception
44      {
45          LinkCheck lc = (LinkCheck) lookup( LinkCheck.ROLE );
46          assertNotNull( lc );
47  
48          lc.setOnline( true ); // TODO: check if online
49  
50          lc.setBasedir( new File( getBasedir(), "src/test/resources" ) ); // TODO
51  
52          lc.setReportOutput( new File( getBasedir(), "target/linkcheck/linkcheck.xml" ) );
53  
54          lc.setReportOutputEncoding( "UTF-8" );
55  
56          lc.setLinkCheckCache( new File( getBasedir(), "target/linkcheck/linkcheck.cache" ) ); // TODO
57  
58          String[] excludes = new String[] {
59              "http://cvs.apache.org/viewcvs.cgi/maven-pluginszz/",
60              "http://cvs.apache.org/viewcvs.cgi/mavenzz/" };
61  
62          lc.setExcludedLinks( excludes );
63  
64          LinkcheckModel result = lc.execute();
65  
66          Iterator iter = result.getFiles().iterator();
67  
68          Map map = new HashMap();
69  
70          while ( iter.hasNext() )
71          {
72              LinkcheckFile ftc = (LinkcheckFile) iter.next();
73              map.put( ftc.getRelativePath(), ftc );
74          }
75  
76          assertEquals( "files.size()", 10, result.getFiles().size() );
77  
78          check( map, "nolink.html", 0 );
79          check( map, "test-resources/nolink.html", 0 );
80          check( map, "test-resources/test1/test1.html", 2 );
81          check( map, "test-resources/test1/test2.html", 0 );
82          check( map, "test1/test1.html", 1 );
83          check( map, "testA.html", 3 );
84          check( map, "anchorTest/testAnchor.html", 1 );
85          check( map, "linkincomment.html", 1 );
86  
87          /* test excludes */
88          String fileName = "testExcludes.html";
89          check( map, fileName, 2 );
90  
91          LinkcheckFile ftc = (LinkcheckFile) map.get( fileName );
92          assertEquals( "Excluded links", 2, ftc.getSuccessful() );
93  
94          // index-all.html should get parsed, but is currently having problems.
95          // There are 805 distinct links in this page
96          check( map, "index-all.html", 805 );
97  
98          // FIXME: this fails because the local (anchor) link is reported as valid, why?
99          //ftc = (LinkcheckFile) map.get( "testA.html" );
100         //assertEquals( "Non-existent links", 0, ftc.getSuccessful() );
101     }
102 
103     private void check( Map map, String name, int linkCount )
104     {
105         LinkcheckFile ftc = (LinkcheckFile) map.get( name );
106 
107         assertNotNull( name + " = null!", ftc );
108 
109         assertEquals( name + ".getResults().size()", linkCount, ftc.getResults().size() );
110     }
111 }