|
| 1 | +package com.baeldung.unescapehtml; |
| 2 | + |
| 3 | + |
| 4 | +import org.apache.commons.text.StringEscapeUtils; |
| 5 | +import org.jsoup.nodes.Entities; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.web.util.HtmlUtils; |
| 9 | +import org.unbescape.html.HtmlEscape; |
| 10 | + |
| 11 | +public class HtmlUnescapeUnitTest { |
| 12 | + @Test |
| 13 | + public void givenAStringsWithHTMLCharacters_whenConvertedWithApacheCommons_thenUnescape(){ |
| 14 | + String expectedQuote = "\"Hello\" Baeldung"; |
| 15 | + String escapedQuote = ""Hello" Baeldung"; |
| 16 | + Assert.assertEquals(expectedQuote, StringEscapeUtils.unescapeHtml4(escapedQuote)); |
| 17 | + |
| 18 | + String escapedStringsWithHtmlSymbol = "<p><strong>Test sentence in bold type.</strong></p>"; |
| 19 | + String expectedStringsWithHtmlSymbol = "<p><strong>Test sentence in bold type.</strong></p>"; |
| 20 | + Assert.assertEquals(expectedStringsWithHtmlSymbol, StringEscapeUtils.unescapeHtml4(escapedStringsWithHtmlSymbol)); |
| 21 | + } |
| 22 | + @Test |
| 23 | + public void givenAStringsWithHTMLCharacters_whenConvertedWithSpringHtmlUtil_thenUnescape() { |
| 24 | + String expectedQuote = "\"Code smells\" -Martin Fowler"; |
| 25 | + String escapedQuote = ""Code smells" -Martin Fowler"; |
| 26 | + Assert.assertEquals(expectedQuote, HtmlUtils.htmlUnescape(escapedQuote)); |
| 27 | + |
| 28 | + String escapedStringsWithHtmlSymbol = "<p>Loren Ipsum is a popular paragraph.</p>"; |
| 29 | + String expectedStringsWithHtmlSymbol = "<p>Loren Ipsum is a popular paragraph.</p>"; |
| 30 | + Assert.assertEquals(expectedStringsWithHtmlSymbol, HtmlUtils.htmlUnescape(escapedStringsWithHtmlSymbol)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenAStringsWithHTMLCharacters_whenConvertedWithUnbescape_thenUnescape() { |
| 35 | + String expectedQuote = "\"Carpe diem\" -Horace"; |
| 36 | + String escapedQuote = ""Carpe diem" -Horace"; |
| 37 | + Assert.assertEquals(expectedQuote, HtmlEscape.unescapeHtml(escapedQuote)); |
| 38 | + |
| 39 | + String escapedStringsWithHtmlSymbol = "<p><em>Pizza is a famous Italian food. Duh.</em></p>"; |
| 40 | + String expectedStringsWithHtmlSymbol = "<p><em>Pizza is a famous Italian food. Duh.</em></p>"; |
| 41 | + Assert.assertEquals(expectedStringsWithHtmlSymbol, HtmlEscape.unescapeHtml(escapedStringsWithHtmlSymbol)); |
| 42 | + } |
| 43 | + @Test |
| 44 | + public void givenAStringsWithHTMLCharacters_whenConvertedWithJsoup_thenUnescape() { |
| 45 | + String expectedQuote = "\"Jsoup\" is another strong library"; |
| 46 | + String escapedQuote = ""Jsoup" is another strong library"; |
| 47 | + Assert.assertEquals(expectedQuote, Entities.unescape(escapedQuote)); |
| 48 | + |
| 49 | + String escapedStringsWithHtmlSymbol = "<p>It simplifies working with real-world <strong>HTML</strong> and <strong>XML</strong></p>"; |
| 50 | + String expectedStringsWithHtmlSymbol = "<p>It simplifies working with real-world <strong>HTML</strong> and <strong>XML</strong></p>"; |
| 51 | + Assert.assertEquals(expectedStringsWithHtmlSymbol, Entities.unescape(escapedStringsWithHtmlSymbol)); |
| 52 | + } |
| 53 | +} |
0 commit comments