1   /*
2    * Copyright 1999,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.feedparser.locate;
17  
18  import java.util.Map;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.feedparser.network.ResourceRequest;
23  import org.apache.commons.feedparser.network.ResourceRequestFactory;
24  
25  /**
26   *
27   * @author <a href="mailto:burton@openprivacy.org">Kevin A. Burton</a>
28   */
29  public class TestAnchorParser extends TestCase {
30  
31      public TestAnchorParser( String name ) {
32          super( name );
33      }
34      
35      private void doTest( int linkCount, String resource ) throws Exception {
36  
37          ResourceRequest request = ResourceRequestFactory.getResourceRequest( resource );
38          String content = request.getInputStreamAsString();
39  
40          TestAnchorParserListener listener = new TestAnchorParserListener();
41  
42          AnchorParser.parse( content, listener );
43  
44          System.out.println( "Found N anchors: " + listener.anchorCount );
45  
46          assertEquals( linkCount, listener.anchorCount );
47  
48      }
49      
50      private void assertAnchorCount( int linkCount, String content ) throws Exception {
51  
52          TestAnchorParserListener listener = new TestAnchorParserListener();
53  
54          AnchorParser.parse( content, listener );
55  
56          assertEquals( linkCount, listener.anchorCount );
57          
58      }
59  
60      public void testGetAttributes() {
61  
62          Map map = DiscoveryLocator.getAttributes( "<a rel=\"linux\" href=\"http://peerfear.org\" title=\"linux\" >\n\nadf</a>" );
63  
64          assertNotNull( map.get( "rel" ) );
65          
66      }
67      
68      public void testBasic() throws Exception {
69  
70          assertAnchorCount( 1, "<a rel=\"linux\" href=\"http://peerfearadsf.org/?thisisit\" title=\"linuxfoo\" >\n\nadf</a>" );
71  
72          assertAnchorCount( 1, "<a rel=\"linux\" href=\"http://peerfear.org\" title=\"linux\" >adf</a>" );
73  
74          assertAnchorCount( 1, "<a href=\"http://peerfear.org\" rel='linux' title='linux' >adf</a>" );
75  
76          assertAnchorCount( 1, "<a href='http://peerfear.org' rel='linux' title='linux' >adf</a>" );
77  
78          assertAnchorCount( 1,  "<a title=\"linux\" rel=\"linux\" href=\"http://peerfear.org\" >adf</a>" );
79  
80          assertAnchorCount( 1,  "<a href=\"http://peerfear.org\" rel=\"linux\" title=\"linux\" >adf</a>" );
81  
82          //FIXME: this won't work because it has an image
83          assertAnchorCount( 1, "<a href='mailto:burton@rojo.com' rel='linux' title='linux' ><img src='' /></a>" );
84  
85          //FIXME: what about unit tests which have multiple lines ?
86  
87      }
88  
89      public void testContent() throws Exception {
90  
91          doTest( 1, "file:tests/anchor/anchor4.html" );
92          doTest( 1, "file:tests/anchor/anchor1.html" );
93          doTest( 1, "file:tests/anchor/anchor2.html" );
94          doTest( 1, "file:tests/anchor/anchor3.html" );
95          doTest( 418, "file:tests/anchor/anchor5.html" );
96  
97          //FIXME: don't find anchors in comments.
98          //doTest( 0, "file:tests/anchor/anchor6.html" );
99          
100     }
101     
102     public static void main( String[] args ) throws Exception {
103 
104         //FIXME: won't work with <a />
105 
106         TestAnchorParser test = new TestAnchorParser( null );
107 
108         test.testBasic();
109         test.testGetAttributes();
110         test.testContent();
111         
112     }
113 
114 }
115 
116 class TestAnchorParserListener implements AnchorParserListener {
117 
118     public int anchorCount;
119     
120     public boolean onAnchor( String href, String rel, String title ) {
121         
122         ++anchorCount;
123 
124         System.out.println( "href: " + href );
125         
126         return true;
127     }
128     
129     public Object getResult() { return null; }
130     public void setContext( Object context ) {}
131     
132 }
133