KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > FunctionalTests


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/FunctionalTests.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:33 $
10
// $Revision: 1.56 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Locale JavaDoc;
32
33 import junit.framework.TestSuite;
34
35 import org.htmlparser.Node;
36 import org.htmlparser.Parser;
37 import org.htmlparser.PrototypicalNodeFactory;
38 import org.htmlparser.tags.ImageTag;
39 import org.htmlparser.util.DefaultParserFeedback;
40 import org.htmlparser.util.NodeIterator;
41 import org.htmlparser.util.ParserException;
42
43 public class FunctionalTests extends ParserTestCase {
44
45     static
46     {
47         System.setProperty ("org.htmlparser.tests.FunctionalTests", "FunctionalTests");
48     }
49
50     public FunctionalTests(String JavaDoc arg0) {
51         super(arg0);
52     }
53
54     /**
55      * Based on a suspected bug report by Annette Doyle,
56      * to check if the no of image tags are correctly
57      * identified by the parser
58      */

59     public void testNumImageTagsInYahooWithoutRegisteringScanners() throws ParserException {
60         // First count the image tags as is
61
int imgTagCount;
62         int parserImgTagCount = countImageTagsWithHTMLParser();
63         imgTagCount = findImageTagCount(getParser ());
64         assertEquals("Image Tag Count",imgTagCount,parserImgTagCount);
65     }
66
67     public int findImageTagCount(Parser parser) {
68         int imgTagCount = 0;
69         parser.reset ();
70         try
71         {
72             imgTagCount = countImageTagsWithoutHTMLParser(parser);
73         }
74         catch (IOException JavaDoc e)
75         {
76             System.err.println ("IO Exception occurred while counting tags");
77         }
78         return imgTagCount;
79     }
80
81     public int countImageTagsWithHTMLParser() throws ParserException {
82         Parser parser = new Parser("http://education.yahoo.com/",new DefaultParserFeedback());
83         parser.setNodeFactory (new PrototypicalNodeFactory (new ImageTag ()));
84         setParser (parser);
85         int parserImgTagCount = 0;
86         Node node;
87         for (NodeIterator e= parser.elements();e.hasMoreNodes();) {
88             node = e.nextNode();
89             if (node instanceof ImageTag) {
90                 parserImgTagCount++;
91             }
92         }
93         return parserImgTagCount;
94     }
95
96     public int countImageTagsWithoutHTMLParser (Parser parser) throws IOException JavaDoc
97     {
98         BufferedReader JavaDoc lines;
99         String JavaDoc line;
100         int imgTagCount;
101         
102         imgTagCount = 0;
103         lines = new BufferedReader JavaDoc (parser.getLexer ().getPage ().getSource ());
104         do {
105             line = lines.readLine();
106             if (line!=null) {
107                 // Check the line for image tags
108
String JavaDoc newline = line.toUpperCase (Locale.ENGLISH);
109                 int fromIndex = -1;
110                 do {
111                     fromIndex = newline.indexOf("<IMG",fromIndex+1);
112                     if (fromIndex!=-1) {
113                         imgTagCount++;
114                     }
115                 }
116                 while (fromIndex!=-1);
117             }
118         }
119         while (line!=null);
120         return imgTagCount;
121     }
122
123     public static TestSuite suite() {
124         return new TestSuite(FunctionalTests.class);
125     }
126 }
127
Popular Tags