Skip to content

Commit 4772976

Browse files
lsoaresKevinGilmore
authored andcommitted
Exercise snippets only in tests (eugenp#982)
* Exercise snippets only in tests * Increase Jsoup version
1 parent a0de46e commit 4772976

3 files changed

Lines changed: 83 additions & 101 deletions

File tree

‎jsoup/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
<maven.compiler.source>1.8</maven.compiler.source>
2626
<maven.compiler.target>1.8</maven.compiler.target>
2727

28-
<jsoup.version>1.10.1</jsoup.version>
28+
<jsoup.version>1.10.2</jsoup.version>
2929
</properties>
3030
</project>

‎jsoup/src/main/java/com/baeldung/jsoup/JsoupParser.java‎

Lines changed: 0 additions & 91 deletions
This file was deleted.

‎jsoup/src/test/java/com/baeldung/jsoup/JsoupParserTest.java‎

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,108 @@
22

33
import java.io.IOException;
44
import org.jsoup.HttpStatusException;
5+
import org.jsoup.Jsoup;
6+
import org.jsoup.nodes.Document;
7+
import org.jsoup.nodes.Element;
8+
import org.jsoup.parser.Tag;
9+
import org.jsoup.select.Elements;
510
import static org.junit.Assert.assertEquals;
611
import static org.junit.Assert.assertTrue;
712
import org.junit.Before;
813
import org.junit.Test;
914

1015
public class JsoupParserTest {
1116

12-
JsoupParser jsoupParser;
17+
Document doc;
1318

1419
@Before
15-
public void setUp() {
16-
jsoupParser = new JsoupParser();
20+
public void setUp() throws IOException {
21+
doc = Jsoup.connect("https://spring.io/blog").get();
1722
}
1823

1924
@Test
20-
public void test404() throws IOException {
25+
public void loadDocument404() throws IOException {
2126
try {
22-
jsoupParser.loadDocument("https://spring.io/will-not-be-found");
27+
doc = Jsoup.connect("https://spring.io/will-not-be-found").get();
2328
} catch (HttpStatusException ex) {
2429
assertEquals(404, ex.getStatusCode());
2530
}
2631
}
2732

2833
@Test
29-
public void testChange() throws IOException {
30-
jsoupParser.loadDocument("http://spring.io/blog");
34+
public void loadDocumentCustomized() throws IOException {
35+
doc = Jsoup.connect("https://spring.io/blog")
36+
.userAgent("Mozilla")
37+
.timeout(5000)
38+
.cookie("cookiename", "val234")
39+
.cookie("anothercookie", "ilovejsoup")
40+
.referrer("http://google.com")
41+
.header("headersecurity", "xyz123")
42+
.get();
43+
}
44+
45+
@Test
46+
public void examplesSelectors() {
47+
Elements links = doc.select("a");
48+
Elements logo = doc.select(".spring-logo--container");
49+
Elements pagination = doc.select("#pagination_control");
50+
Elements divsDescendant = doc.select("header div");
51+
Elements divsDirect = doc.select("header > div");
52+
53+
Element pag = doc.getElementById("pagination_control");
54+
Elements desktopOnly = doc.getElementsByClass("desktopOnly");
55+
56+
Elements sections = doc.select("section");
57+
Element firstSection = sections.first();
58+
Elements sectionParagraphs = firstSection.select(".paragraph");
59+
}
60+
61+
@Test
62+
public void examplesTraversing() {
63+
Elements sections = doc.select("section");
64+
65+
Element firstSection = sections.first();
66+
Element lastSection = sections.last();
67+
Element secondSection = sections.get(2);
68+
Elements allParents = firstSection.parents();
69+
Element parent = firstSection.parent();
70+
Elements children = firstSection.children();
71+
Elements siblings = firstSection.siblingElements();
72+
73+
sections.stream().forEach(el -> System.out.println("section: " + el));
74+
}
75+
76+
@Test
77+
public void examplesExtracting() {
78+
Element firstArticle = doc.select("article").first();
79+
Element timeElement = firstArticle.select("time").first();
80+
String dateTimeOfFirstArticle = timeElement.attr("datetime");
81+
Element sectionDiv = firstArticle.select("section div").first();
82+
String sectionDivText = sectionDiv.text();
83+
String articleHtml = firstArticle.html();
84+
String outerHtml = firstArticle.outerHtml();
85+
}
86+
87+
@Test
88+
public void examplesModifying() {
89+
Element firstArticle = doc.select("article").first();
90+
Element timeElement = firstArticle.select("time").first();
91+
Element sectionDiv = firstArticle.select("section div").first();
92+
93+
String dateTimeOfFirstArticle = timeElement.attr("datetime");
94+
timeElement.attr("datetime", "2016-12-16 15:19:54.3");
95+
sectionDiv.text("foo bar");
96+
firstArticle.select("h2").html("<div><span></span></div>");
97+
98+
Element link = new Element(Tag.valueOf("a"), "")
99+
.text("Checkout this amazing website!")
100+
.attr("href", "http://baeldung.com")
101+
.attr("target", "_blank");
102+
firstArticle.appendChild(link);
31103

32-
jsoupParser.examplesModifying();
104+
doc.select("li.navbar-link").remove();
105+
firstArticle.select("img").remove();
33106

34-
assertTrue(jsoupParser.getTidyHtml().contains("http://baeldung.com"));
107+
assertTrue(doc.html().contains("http://baeldung.com"));
35108
}
36109
}

0 commit comments

Comments
 (0)