1 27 package org.htmlparser.tests.tagTests; 28 29 import java.util.Hashtable ; 30 import junit.framework.TestSuite; 31 import org.htmlparser.Node; 32 33 import org.htmlparser.PrototypicalNodeFactory; 34 import org.htmlparser.Tag; 35 import org.htmlparser.tags.BodyTag; 36 import org.htmlparser.tags.Html; 37 import org.htmlparser.tags.TitleTag; 38 import org.htmlparser.tests.ParserTestCase; 39 import org.htmlparser.util.NodeIterator; 40 import org.htmlparser.util.ParserException; 41 42 public class BodyTagTest extends ParserTestCase { 43 44 static 45 { 46 System.setProperty ("org.htmlparser.tests.tagTests.BodyTagTest", "BodyTagTest"); 47 } 48 49 private BodyTag bodyTag; 50 private String html = "<body>Yahoo!</body>"; 51 52 public BodyTagTest(String name) { 53 super(name); 54 } 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 createParser("<html><head><title>body tag test</title></head>" + html + "</html>"); 59 parseAndAssertNodeCount(1); 60 assertTrue("Only node should be an HTML node",node[0] instanceof Html); 61 Html html = (Html)node[0]; 62 assertTrue("HTML node should have two children",2 == html.getChildCount ()); 63 assertTrue("Second node should be an BODY tag",html.getChild(1) instanceof BodyTag); 64 bodyTag = (BodyTag)html.getChild(1); 65 } 66 67 public void testToPlainTextString() throws ParserException { 68 assertEquals("Body","Yahoo!",bodyTag.toPlainTextString()); 70 } 71 72 public void testToHTML() throws ParserException { 73 assertStringEquals("Raw String", html, bodyTag.toHtml()); 74 } 75 76 public void testToString() throws ParserException { 77 assertEquals("Body","BODY: Yahoo!",bodyTag.toString()); 78 } 79 80 public void testAttributes () 81 { 82 NodeIterator iterator; 83 Node node; 84 Hashtable attributes; 85 86 try 87 { 88 createParser("<body style=\"margin-top:4px; margin-left:20px;\" title=\"body\">"); 89 parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ())); 90 iterator = parser.elements (); 91 node = null; 92 while (iterator.hasMoreNodes ()) 93 { 94 node = iterator.nextNode (); 95 if (node instanceof BodyTag) 96 { 97 attributes = ((BodyTag)node).getAttributes (); 98 assertTrue ("no style attribute", attributes.containsKey ("STYLE")); 99 assertTrue ("no title attribute", attributes.containsKey ("TITLE")); 100 } 101 else 102 fail ("not a body tag"); 103 assertTrue ("more than one node", !iterator.hasMoreNodes ()); 104 } 105 assertNotNull ("no elements", node); 106 } 107 catch (ParserException pe) 108 { 109 fail ("exception thrown " + pe.getMessage ()); 110 } 111 } 112 113 public void testSimpleBody() throws ParserException { 114 createParser("<html><head><title>Test 1</title></head><body>This is a body tag</body></html>"); 115 parser.setNodeFactory ( 116 new PrototypicalNodeFactory ( 117 new Tag[] 118 { 119 new BodyTag (), 120 new TitleTag (), 121 })); 122 parseAndAssertNodeCount(6); 123 assertTrue(node[4] instanceof BodyTag); 124 BodyTag bodyTag = (BodyTag) node[4]; 126 assertEquals("Body","This is a body tag",bodyTag.getBody()); 127 assertEquals("Body","<body>This is a body tag</body>",bodyTag.toHtml()); 128 } 129 130 public void testBodywithJsp() throws ParserException { 131 String body = "<body><%=BodyValue%></body>"; 132 createParser("<html><head><title>Test 1</title></head>" + body + "</html>"); 133 parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ())); 134 parseAndAssertNodeCount(8); 135 assertTrue(node[6] instanceof BodyTag); 136 BodyTag bodyTag = (BodyTag) node[6]; 138 assertStringEquals("Body",body,bodyTag.toHtml()); 139 } 140 141 public void testBodyMixed() throws ParserException { 142 String body = "<body>before jsp<%=BodyValue%>after jsp</body>"; 143 createParser("<html><head><title>Test 1</title></head>" + body + "</html>"); 144 parser.setNodeFactory ( 145 new PrototypicalNodeFactory ( 146 new Tag[] 147 { 148 new BodyTag (), 149 new TitleTag (), 150 })); 151 parseAndAssertNodeCount(6); 152 assertTrue(node[4] instanceof BodyTag); 153 BodyTag bodyTag = (BodyTag) node[4]; 155 assertEquals("Body",body,bodyTag.toHtml()); 156 } 157 158 public void testBodyEnding() throws ParserException { 159 String body = "<body>before jsp<%=BodyValue%>after jsp"; 160 createParser("<html>" + body + "</html>"); 161 parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ())); 162 parseAndAssertNodeCount(3); 163 assertTrue(node[1] instanceof BodyTag); 164 BodyTag bodyTag = (BodyTag) node[1]; 166 assertEquals("Body",body + "</body>",bodyTag.toHtml()); 167 } 168 169 public static TestSuite suite() 170 { 171 return new TestSuite(BodyTagTest.class); 172 } 173 } 174
| Popular Tags
|