Skip to content

Commit b237ca8

Browse files
committed
complete test #10
1 parent b025953 commit b237ca8

File tree

7 files changed

+262
-16
lines changed

7 files changed

+262
-16
lines changed

src/main/java/us/codecraft/xsoup/Xsoup.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package us.codecraft.xsoup;
22

33
import org.jsoup.Jsoup;
4+
import org.jsoup.nodes.Document;
45
import org.jsoup.nodes.Element;
56
import us.codecraft.xsoup.w3c.NodeAdaptors;
67
import us.codecraft.xsoup.xevaluator.XElements;
@@ -32,4 +33,8 @@ public static org.w3c.dom.Element convertElement(Element element) {
3233
return NodeAdaptors.getElement(element);
3334
}
3435

36+
public static org.w3c.dom.Document convertDocument(Document document) {
37+
return NodeAdaptors.getDocument(document);
38+
}
39+
3540
}

src/main/java/us/codecraft/xsoup/w3c/AttributeAdaptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public short compareDocumentPosition(Node other) throws DOMException {
130130
return 0;
131131
}
132132

133+
@Override
134+
public String getTextContent() throws DOMException {
135+
return attribute.getValue();
136+
}
137+
133138
@Override
134139
public boolean isSameNode(Node other) {
135140
return false;

src/main/java/us/codecraft/xsoup/w3c/ElementAdaptor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ public Node getNextSibling() {
100100

101101
@Override
102102
public NamedNodeMap getAttributes() {
103-
// try{
104-
//
105-
// throw new RuntimeException();
106-
// } catch (Exception e){
107-
// e.printStackTrace();
108-
// }
109103
return NodeAdaptors.getNamedNodeMap(NodeAdaptors.getAttributes(element.attributes(), element));
110104
}
111105

106+
@Override
107+
public String getTextContent() throws DOMException {
108+
return element.text();
109+
}
110+
112111
@Override
113112
public Document getOwnerDocument() {
114113
return NodeAdaptors.getDocument(element.ownerDocument());

src/main/java/us/codecraft/xsoup/w3c/HtmlDocumentType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public short compareDocumentPosition(Node other) throws DOMException {
8888
return 0;
8989
}
9090

91+
@Override
92+
public String getTextContent() throws DOMException {
93+
return document.text();
94+
}
95+
9196
@Override
9297
public boolean isSameNode(Node other) {
9398
return false;

src/main/java/us/codecraft/xsoup/w3c/NodeAdaptor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ public String getBaseURI() {
6767
return null;
6868
}
6969

70-
@Override
71-
public String getTextContent() throws DOMException {
72-
return null;
73-
}
74-
7570
@Override
7671
public void setTextContent(String textContent) throws DOMException {
7772
throw new UnsupportedOperationException();

src/test/java/us/codecraft/xsoup/w3c/DocumentAdaptorTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.w3c.dom.Document;
66

77
import javax.xml.xpath.XPath;
8-
import javax.xml.xpath.XPathConstants;
98
import javax.xml.xpath.XPathExpression;
109
import javax.xml.xpath.XPathFactory;
1110

@@ -27,9 +26,5 @@ public void testDocumentAdaptor() throws Exception {
2726
String result = xPathExpression.evaluate(document);
2827
assertThat(result).isEqualTo("https://github.com");
2928

30-
xPathExpression = target.compile("//div/a");
31-
Object evaluate = xPathExpression.evaluate(document, XPathConstants.NODE);
32-
System.out.println(evaluate);
33-
3429
}
3530
}
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package us.codecraft.xsoup.w3c;
2+
3+
import org.jsoup.Jsoup;
4+
import org.junit.Test;
5+
import org.w3c.dom.Node;
6+
import org.w3c.dom.NodeList;
7+
import us.codecraft.xsoup.Xsoup;
8+
9+
import javax.xml.xpath.*;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
/**
16+
* @author code4crafer@gmail.com
17+
*/
18+
public class W3cEvaluatorTest {
19+
20+
private String html = "<html><body><div id='test'>aaa<div><a href=\"https://github.com\">github.com</a></div></div></body></html>";
21+
22+
private String htmlClass = "<html><body><div class='a b c'><div><a href=\"https://github.com\">github.com</a></div></div><div>b</div></body></html>";
23+
24+
@Test
25+
public void testSelect() throws XPathExpressionException {
26+
27+
String html = "<html><div><a href='https://github.com'>github.com</a></div>" +
28+
"<table><tr><td>a</td><td>b</td></tr></table></html>";
29+
30+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html));
31+
32+
assertThat(getStringValue(document, "//div/a/@href")).isEqualTo("https://github.com");
33+
34+
List<String> nodeListValue = getNodeListValue(document, "//tr/td");
35+
assertThat(nodeListValue.get(0)).isEqualTo("<td>a</td>");
36+
assertThat(nodeListValue.get(1)).isEqualTo("<td>b</td>");
37+
}
38+
39+
private String getStringValue(org.w3c.dom.Document document, String expression) throws XPathExpressionException {
40+
XPathExpression xPathExpression = newXPathExpression(expression);
41+
return xPathExpression.evaluate(document);
42+
}
43+
44+
private XPathExpression newXPathExpression(String expression) throws XPathExpressionException {
45+
XPathExpression xPathExpression;
46+
XPathFactory xPathfactory = XPathFactory.newInstance();
47+
XPath target = xPathfactory.newXPath();
48+
xPathExpression = target.compile(expression);
49+
return xPathExpression;
50+
}
51+
52+
private String getNodeValue(org.w3c.dom.Document document, String expression) throws XPathExpressionException {
53+
XPathExpression xPathExpression = newXPathExpression(expression);
54+
Object evaluate = xPathExpression.evaluate(document, XPathConstants.NODE);
55+
if (evaluate == null) {
56+
return null;
57+
}
58+
Node node = (Node) evaluate;
59+
return node.getNodeValue();
60+
}
61+
62+
private List<String> getNodeListValue(org.w3c.dom.Document document, String expression) throws XPathExpressionException {
63+
XPathExpression xPathExpression = newXPathExpression(expression);
64+
Object evaluate = xPathExpression.evaluate(document, XPathConstants.NODESET);
65+
if (evaluate == null) {
66+
return null;
67+
}
68+
NodeList nodeList = (NodeList) evaluate;
69+
List<String> nodeStrings = new ArrayList<String>(nodeList.getLength());
70+
for (int i = 0; i < nodeList.getLength(); i++) {
71+
nodeStrings.add(nodeList.item(i).getNodeValue());
72+
}
73+
return nodeStrings;
74+
}
75+
76+
@Test
77+
public void testParent() throws XPathExpressionException {
78+
79+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html));
80+
81+
assertThat(getNodeValue(document, "/html/body/div/div/a")).isEqualTo("<a href=\"https://github.com\">github.com</a>");
82+
assertThat(getNodeValue(document, "/html//div/div/a")).isEqualTo("<a href=\"https://github.com\">github.com</a>");
83+
84+
assertThat(getNodeValue(document, "/html/div/div/a")).isNull();
85+
86+
}
87+
88+
@Test
89+
public void testByAttribute() throws XPathExpressionException {
90+
91+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html));
92+
93+
assertThat(getNodeValue(document, "//a[@href]")).isEqualTo("<a href=\"https://github.com\">github.com</a>");
94+
95+
assertThat(getNodeValue(document, "//a[@id]")).isNull();
96+
97+
String expectedDiv = "<div id=\"test\">\n" +
98+
" aaa\n" +
99+
" <div>\n" +
100+
" <a href=\"https://github.com\">github.com</a>\n" +
101+
" </div>\n" +
102+
"</div>";
103+
104+
105+
//TODO: illegal
106+
//assertThat(getNodeValue(document,"//div[@id=test]")).isEqualTo(expectedDiv);
107+
108+
assertThat(getNodeValue(document, "//div[@id='test']")).isEqualTo(expectedDiv);
109+
assertThat(getNodeValue(document, "//div[@id=\"test\"]")).isEqualTo(expectedDiv);
110+
}
111+
112+
@Test
113+
public void testClass() throws XPathExpressionException {
114+
115+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(htmlClass));
116+
117+
118+
assertThat(getNodeListValue(document,"//div[@class='a b c']").get(0)).isEqualTo("<div class=\"a b c\">\n" +
119+
" <div>\n" +
120+
" <a href=\"https://github.com\">github.com</a>\n" +
121+
" </div>\n" +
122+
"</div>");
123+
124+
assertThat(getNodeListValue(document, "//div[@class='b']")).isNullOrEmpty();
125+
126+
assertThat(getNodeListValue(document, "//div[@class='d']")).isNullOrEmpty();
127+
128+
129+
}
130+
131+
@Test
132+
public void testNth() throws XPathExpressionException {
133+
134+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(htmlClass));
135+
136+
assertThat(getNodeValue(document, "//body/div[1]")).isEqualTo("<div class=\"a b c\">\n" +
137+
" <div>\n" +
138+
" <a href=\"https://github.com\">github.com</a>\n" +
139+
" </div>\n" +
140+
"</div>");
141+
142+
assertThat(getNodeValue(document, "//body/div[2]")).isEqualTo("<div>\n" +
143+
" b\n" +
144+
"</div>");
145+
146+
String htmlSVG = "<div><svg>1</svg><svg>2</svg></div>";
147+
148+
document = Xsoup.convertDocument(Jsoup.parse(htmlSVG));
149+
assertThat(getNodeValue(document, "//div/svg[1]")).isEqualTo("<svg>\n" +
150+
" 1\n" +
151+
"</svg>");
152+
assertThat(getNodeValue(document, "//div/svg[2]")).isEqualTo("<svg>\n" +
153+
" 2\n" +
154+
"</svg>");
155+
}
156+
157+
@Test
158+
public void testAttribute() throws XPathExpressionException {
159+
160+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(htmlClass));
161+
162+
assertThat(getStringValue(document,"//a/@href")).isEqualTo("https://github.com");
163+
164+
//TODO: not support
165+
//assertThat(getStringValue(document,"//a/text()")).isEqualTo("github.com");
166+
167+
//TODO: not support
168+
//assertThat(getStringValue(document,"//div[@class=a]/html()")).isEqualTo("<div>\n" +
169+
// " <a href=\"https://github.com\">github.com</a>\n" +
170+
// "</div>");
171+
172+
}
173+
174+
@Test
175+
public void testLogicOperation() throws XPathExpressionException {
176+
177+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html));
178+
179+
String expectedDiv = "<div id=\"test\">\n" +
180+
" aaa\n" +
181+
" <div>\n" +
182+
" <a href=\"https://github.com\">github.com</a>\n" +
183+
" </div>\n" +
184+
"</div>";
185+
186+
assertThat(getNodeValue(document, "//*[@id='te' or @id='test']")).isEqualTo(expectedDiv);
187+
188+
assertThat(getNodeValue(document, "//*[@id='te' and @id='test']")).isNullOrEmpty();
189+
190+
assertThat(getNodeValue(document, "//*[@id='te' and @id='test']")).isNullOrEmpty();
191+
192+
assertThat(getNodeValue(document,"//*[(@id='te' or @id='test') and @id='test']")).isEqualTo(expectedDiv);
193+
194+
assertThat(getNodeValue(document,"//*[@id='te' or (@id='test' and @id='id')]")).isNull();
195+
}
196+
197+
@Test
198+
public void testContains() throws XPathExpressionException {
199+
200+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html));
201+
202+
assertThat(getNodeValue(document,"//div[contains(@id,'te')]")).isEqualTo("<div id=\"test\">\n" +
203+
" aaa\n" +
204+
" <div>\n" +
205+
" <a href=\"https://github.com\">github.com</a>\n" +
206+
" </div>\n" +
207+
"</div>");
208+
209+
}
210+
211+
@Test
212+
public void testCombingXPath() throws XPathExpressionException {
213+
214+
String html2 = "<html><div id='test2'>aa<a href='https://github.com'>github.com</a></div>";
215+
216+
String expectedDiv1 = "<div id=\"test\">\n" +
217+
" aaa\n" +
218+
" <div>\n" +
219+
" <a href=\"https://github.com\">github.com</a>\n" +
220+
" </div>\n" +
221+
"</div>";
222+
223+
String expectedDiv2 = "<div id=\"test2\">\n" +
224+
" aa\n" +
225+
" <a href=\"https://github.com\">github.com</a>\n" +
226+
"</div>";
227+
228+
org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html));
229+
230+
assertThat(getNodeValue(document, "//div[@id='test'] | //div[@id='test2']")).isEqualTo(expectedDiv1);
231+
232+
document = Xsoup.convertDocument(Jsoup.parse(html2));
233+
234+
assertThat(getNodeValue(document, "//div[@id='test'] | //div[@id='test2']")).isEqualTo(expectedDiv2);
235+
236+
document = Xsoup.convertDocument(Jsoup.parse(html+html2));
237+
238+
assertThat(getNodeListValue(document, "//div[@id='test'] | //div[@id='test2']")).contains(expectedDiv1,expectedDiv2);
239+
240+
}
241+
242+
}

0 commit comments

Comments
 (0)