1 27 package org.htmlparser.tests.visitorsTests; 28 29 import org.htmlparser.tests.ParserTestCase; 30 import org.htmlparser.visitors.StringFindingVisitor; 31 32 public class StringFindingVisitorTest extends ParserTestCase { 33 34 static 35 { 36 System.setProperty ("org.htmlparser.tests.visitorsTests.StringFindingVisitorTest", "StringFindingVisitorTest"); 37 } 38 39 private static final String HTML = 40 "<HTML><HEAD><TITLE>This is the Title</TITLE>" + 41 "</HEAD><BODY>Hello World, this is an excellent parser</BODY></HTML>"; 42 43 private static final String HTML_TO_SEARCH = 44 "<HTML><HEAD><TITLE>test</TITLE></HEAD>\n"+ 45 "<BODY><H1>This is a test page</H1>\n"+ 46 "Writing tests is good for code. Testing is a good\n"+ 47 "philosophy. Test driven development is even better.\n"; 48 49 public StringFindingVisitorTest(String name) { 50 super(name); 51 } 52 53 public void testSimpleStringFind() throws Exception { 54 createParser(HTML); 55 StringFindingVisitor visitor = new StringFindingVisitor("Hello"); 56 parser.visitAllNodesWith(visitor); 57 assertTrue("Hello found", visitor.stringWasFound()); 58 } 59 60 public void testStringNotFound() throws Exception { 61 createParser(HTML); 62 StringFindingVisitor visitor = new StringFindingVisitor("industrial logic"); 63 parser.visitAllNodesWith(visitor); 64 assertTrue("industrial logic should not have been found", !visitor.stringWasFound()); 65 } 66 67 public void testStringInTagNotFound() throws Exception { 68 createParser(HTML); 69 StringFindingVisitor visitor = new StringFindingVisitor("HTML"); 70 parser.visitAllNodesWith(visitor); 71 assertTrue("HTML should not have been found", !visitor.stringWasFound()); 72 } 73 74 public void testStringFoundInSingleStringNode() throws Exception { 75 createParser("this is some text!"); 76 StringFindingVisitor visitor = new StringFindingVisitor("text"); 77 parser.visitAllNodesWith(visitor); 78 assertTrue("text should be found", visitor.stringWasFound()); 79 } 80 81 public void testStringFoundCount() throws Exception { 82 createParser(HTML); 83 StringFindingVisitor visitor = new StringFindingVisitor("is"); 84 parser.visitAllNodesWith(visitor); 85 assertEquals("# times 'is' was found", 2, visitor.stringFoundCount()); 86 87 visitor = new StringFindingVisitor("and"); 88 parser.visitAllNodesWith(visitor); 89 assertEquals("# times 'and' was found", 0, visitor.stringFoundCount()); 90 } 91 92 public void testStringFoundMultipleTimes() throws Exception { 93 createParser(HTML_TO_SEARCH); 94 StringFindingVisitor visitor = new StringFindingVisitor("TEST"); 95 visitor.doMultipleSearchesWithinStrings(); 96 parser.visitAllNodesWith(visitor); 97 assertEquals("TEST found", 5, visitor.stringFoundCount()); 98 } 99 100 101 102 } 103 | Popular Tags |