1 27 package org.htmlparser.tests; 28 29 import java.io.BufferedReader ; 30 import java.io.IOException ; 31 import java.util.Locale ; 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 arg0) { 51 super(arg0); 52 } 53 54 59 public void testNumImageTagsInYahooWithoutRegisteringScanners() throws ParserException { 60 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 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 97 { 98 BufferedReader lines; 99 String line; 100 int imgTagCount; 101 102 imgTagCount = 0; 103 lines = new BufferedReader (parser.getLexer ().getPage ().getSource ()); 104 do { 105 line = lines.readLine(); 106 if (line!=null) { 107 String 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 |