diff --git a/jsoup/pom.xml b/jsoup/pom.xml
index 343e139b46..25551cb3d6 100644
--- a/jsoup/pom.xml
+++ b/jsoup/pom.xml
@@ -25,6 +25,6 @@
1.8
1.8
- 1.10.1
+ 1.10.2
diff --git a/jsoup/src/main/java/com/baeldung/jsoup/JsoupParser.java b/jsoup/src/main/java/com/baeldung/jsoup/JsoupParser.java
deleted file mode 100644
index cb86b16888..0000000000
--- a/jsoup/src/main/java/com/baeldung/jsoup/JsoupParser.java
+++ /dev/null
@@ -1,91 +0,0 @@
-package com.baeldung.jsoup;
-
-import java.io.IOException;
-import org.jsoup.Jsoup;
-import org.jsoup.nodes.Document;
-import org.jsoup.nodes.Element;
-import org.jsoup.parser.Tag;
-import org.jsoup.select.Elements;
-
-public class JsoupParser {
-
- Document doc;
-
- public void loadDocument(String blogUrl) throws IOException {
- doc = Jsoup.connect(blogUrl).get();
- }
-
- void loadDocumentCustomized(String blogUrl) throws IOException {
- doc = Jsoup.connect(blogUrl)
- .userAgent("Mozilla")
- .timeout(5000)
- .cookie("cookiename", "val234")
- .cookie("anothercookie", "ilovejsoup")
- .referrer("http://google.com")
- .header("headersecurity", "xyz123")
- .get();
- }
-
- void examplesSelectors() {
- Elements links = doc.select("a");
- Elements logo = doc.select(".spring-logo--container");
- Elements pagination = doc.select("#pagination_control");
- Elements divsDescendant = doc.select("header div");
- Elements divsDirect = doc.select("header > div");
-
- Element pag = doc.getElementById("pagination_control");
- Elements desktopOnly = doc.getElementsByClass("desktopOnly");
-
- Elements sections = doc.select("section");
- Element firstSection = sections.first();
- Elements sectionParagraphs = firstSection.select(".paragraph");
- }
-
- void examplesTraversing() {
- Elements sections = doc.select("section");
-
- Element firstSection = sections.first();
- Element lastSection = sections.last();
- Element secondSection = sections.get(2);
- Elements allParents = firstSection.parents();
- Element parent = firstSection.parent();
- Elements children = firstSection.children();
- Elements siblings = firstSection.siblingElements();
-
- sections.stream().forEach(el -> System.out.println("section: " + el));
- }
-
- void examplesExtracting() {
- Element firstArticle = doc.select("article").first();
- Element timeElement = firstArticle.select("time").first();
- String dateTimeOfFirstArticle = timeElement.attr("datetime");
- Element sectionDiv = firstArticle.select("section div").first();
- String sectionDivText = sectionDiv.text();
- String articleHtml = firstArticle.html();
- String outerHtml = firstArticle.outerHtml();
- }
-
- void examplesModifying() {
- Element firstArticle = doc.select("article").first();
- Element timeElement = firstArticle.select("time").first();
- Element sectionDiv = firstArticle.select("section div").first();
-
- String dateTimeOfFirstArticle = timeElement.attr("datetime");
- timeElement.attr("datetime", "2016-12-16 15:19:54.3");
- sectionDiv.text("foo bar");
- firstArticle.select("h2").html("
");
-
- Element link = new Element(Tag.valueOf("a"), "")
- .text("Checkout this amazing website!")
- .attr("href", "http://baeldung.com")
- .attr("target", "_blank");
- firstArticle.appendChild(link);
-
- doc.select("li.navbar-link").remove();
- firstArticle.select("img").remove();
- }
-
- String getTidyHtml() {
- return doc.html();
- }
-}
diff --git a/jsoup/src/test/java/com/baeldung/jsoup/JsoupParserTest.java b/jsoup/src/test/java/com/baeldung/jsoup/JsoupParserTest.java
index 85fd3c3459..aa8b8bad96 100644
--- a/jsoup/src/test/java/com/baeldung/jsoup/JsoupParserTest.java
+++ b/jsoup/src/test/java/com/baeldung/jsoup/JsoupParserTest.java
@@ -2,6 +2,11 @@ package com.baeldung.jsoup;
import java.io.IOException;
import org.jsoup.HttpStatusException;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+import org.jsoup.select.Elements;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
@@ -9,28 +14,96 @@ import org.junit.Test;
public class JsoupParserTest {
- JsoupParser jsoupParser;
+ Document doc;
@Before
- public void setUp() {
- jsoupParser = new JsoupParser();
+ public void setUp() throws IOException {
+ doc = Jsoup.connect("https://spring.io/blog").get();
}
@Test
- public void test404() throws IOException {
+ public void loadDocument404() throws IOException {
try {
- jsoupParser.loadDocument("https://spring.io/will-not-be-found");
+ doc = Jsoup.connect("https://spring.io/will-not-be-found").get();
} catch (HttpStatusException ex) {
assertEquals(404, ex.getStatusCode());
}
}
@Test
- public void testChange() throws IOException {
- jsoupParser.loadDocument("http://spring.io/blog");
+ public void loadDocumentCustomized() throws IOException {
+ doc = Jsoup.connect("https://spring.io/blog")
+ .userAgent("Mozilla")
+ .timeout(5000)
+ .cookie("cookiename", "val234")
+ .cookie("anothercookie", "ilovejsoup")
+ .referrer("http://google.com")
+ .header("headersecurity", "xyz123")
+ .get();
+ }
- jsoupParser.examplesModifying();
+ @Test
+ public void examplesSelectors() {
+ Elements links = doc.select("a");
+ Elements logo = doc.select(".spring-logo--container");
+ Elements pagination = doc.select("#pagination_control");
+ Elements divsDescendant = doc.select("header div");
+ Elements divsDirect = doc.select("header > div");
- assertTrue(jsoupParser.getTidyHtml().contains("http://baeldung.com"));
+ Element pag = doc.getElementById("pagination_control");
+ Elements desktopOnly = doc.getElementsByClass("desktopOnly");
+
+ Elements sections = doc.select("section");
+ Element firstSection = sections.first();
+ Elements sectionParagraphs = firstSection.select(".paragraph");
+ }
+
+ @Test
+ public void examplesTraversing() {
+ Elements sections = doc.select("section");
+
+ Element firstSection = sections.first();
+ Element lastSection = sections.last();
+ Element secondSection = sections.get(2);
+ Elements allParents = firstSection.parents();
+ Element parent = firstSection.parent();
+ Elements children = firstSection.children();
+ Elements siblings = firstSection.siblingElements();
+
+ sections.stream().forEach(el -> System.out.println("section: " + el));
+ }
+
+ @Test
+ public void examplesExtracting() {
+ Element firstArticle = doc.select("article").first();
+ Element timeElement = firstArticle.select("time").first();
+ String dateTimeOfFirstArticle = timeElement.attr("datetime");
+ Element sectionDiv = firstArticle.select("section div").first();
+ String sectionDivText = sectionDiv.text();
+ String articleHtml = firstArticle.html();
+ String outerHtml = firstArticle.outerHtml();
+ }
+
+ @Test
+ public void examplesModifying() {
+ Element firstArticle = doc.select("article").first();
+ Element timeElement = firstArticle.select("time").first();
+ Element sectionDiv = firstArticle.select("section div").first();
+
+ String dateTimeOfFirstArticle = timeElement.attr("datetime");
+ timeElement.attr("datetime", "2016-12-16 15:19:54.3");
+ sectionDiv.text("foo bar");
+ firstArticle.select("h2").html("
");
+
+ Element link = new Element(Tag.valueOf("a"), "")
+ .text("Checkout this amazing website!")
+ .attr("href", "http://baeldung.com")
+ .attr("target", "_blank");
+ firstArticle.appendChild(link);
+
+ doc.select("li.navbar-link").remove();
+ firstArticle.select("img").remove();
+
+ assertTrue(doc.html().contains("http://baeldung.com"));
}
}