01: package com.mockrunner.test.util;
02:
03: import java.io.FileInputStream;
04: import java.util.List;
05:
06: import junit.framework.TestCase;
07:
08: import org.jdom.Document;
09: import org.jdom.Element;
10:
11: import com.mockrunner.util.web.XmlUtil;
12:
13: public class XmlUtilTest extends TestCase {
14: private String source;
15:
16: private void prepareHTMLFile() throws Exception {
17: FileInputStream stream = new FileInputStream(
18: "src/com/mockrunner/test/util/test.html");
19: StringBuffer output = new StringBuffer();
20: int nextChar = stream.read();
21: while (-1 != nextChar) {
22: output.append((char) nextChar);
23: nextChar = stream.read();
24: }
25: source = output.toString();
26: }
27:
28: private void prepareHTML() {
29: StringBuffer output = new StringBuffer();
30: output.append("<html>\n");
31: output.append("<head>\n");
32: output.append("<meta http-equiv=\"refresh\" content=\"");
33: output.append("0;URL=http://www.mockrunner.com>\"\n");
34: output.append("</head>\n");
35: output.append("<body>\n");
36: output.append("<h3>");
37: output.append("You will be redirected to ");
38: output.append("<a href=\"http://www.mockrunner.com\">");
39: output.append("http://www.mockrunner.com</a>");
40: output.append("</h3>\n");
41: output.append("</body>\n");
42: output.append("</html>\n");
43: source = output.toString();
44: }
45:
46: public void testParseHTML() throws Exception {
47: prepareHTML();
48: Document document = XmlUtil.createJDOMDocument(XmlUtil
49: .parseHTML(source));
50: Element rootElement = document.getRootElement();
51: assertEquals("html", rootElement.getName());
52: List children = rootElement.getChildren();
53: assertTrue(children.size() == 2);
54: Element headElement = (Element) children.get(0);
55: assertEquals("head", headElement.getName());
56: Element metaElement = headElement.getChild("meta");
57: assertEquals("refresh", metaElement
58: .getAttributeValue("http-equiv"));
59: assertEquals("0;URL=http://www.mockrunner.com>", metaElement
60: .getAttributeValue("content"));
61: Element bodyElement = (Element) children.get(1);
62: Element h3Element = bodyElement.getChild("h3");
63: assertEquals("You will be redirected to ", h3Element.getText());
64: Element linkElement = h3Element.getChild("a");
65: assertEquals("http://www.mockrunner.com", linkElement
66: .getAttributeValue("href"));
67: assertEquals("http://www.mockrunner.com", linkElement.getText());
68: }
69:
70: public void testParseHTMLFile() throws Exception {
71: prepareHTMLFile();
72: Document document = XmlUtil.createJDOMDocument(XmlUtil
73: .parseHTML(source));
74: Element rootElement = document.getRootElement();
75: assertEquals("html", rootElement.getName());
76: List children = rootElement.getChildren();
77: assertTrue(children.size() == 2);
78: Element headElement = (Element) children.get(0);
79: assertEquals("head", headElement.getName());
80: Element bodyElement = (Element) children.get(1);
81: assertEquals("body", bodyElement.getName());
82: Element table = XmlUtil
83: .getBodyFragmentFromJDOMDocument(document);
84: assertEquals("table", table.getName());
85: }
86: }
|