BAEL-5644-escape-html-java (#14492)

This commit is contained in:
vunamtien
2023-07-28 21:44:51 +07:00
committed by GitHub
parent a8001c3a8e
commit 53aea2f855
3 changed files with 70 additions and 0 deletions
@@ -0,0 +1,43 @@
package com.baeldung.escapehtml;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HtmlEscapeUnitTest {
@Test
public void whenInputContainAmpersand_thenEscape() {
String input = "AT&T";
String expected = "AT&T";
assertEquals(expected, HtmlEscapeUtils.escapeWithApacheCommons(input));
assertEquals(expected, HtmlEscapeUtils.escapeWithGuava(input));
assertEquals(expected, HtmlEscapeUtils.escapeWithSpring(input));
}
@Test
public void whenInputContainDoubleQuotes_thenEscape() {
String input = "She said, \"Hello!\"";
String expected = "She said, "Hello!"";
assertEquals(expected, HtmlEscapeUtils.escapeWithApacheCommons(input));
assertEquals(expected, HtmlEscapeUtils.escapeWithGuava(input));
assertEquals(expected, HtmlEscapeUtils.escapeWithSpring(input));
}
@Test
public void whenInputContainManyHtmlSymbols_thenEscape() {
String input = "<p>This is a <strong>test</strong> string.</p>";
String expected = "&lt;p&gt;This is a &lt;strong&gt;test&lt;/strong&gt; string.&lt;/p&gt;";
assertEquals(expected, HtmlEscapeUtils.escapeWithApacheCommons(input));
assertEquals(expected, HtmlEscapeUtils.escapeWithGuava(input));
assertEquals(expected, HtmlEscapeUtils.escapeWithSpring(input));
}
@Test
public void whenInputContainNoHtmlSymbols_thenEscape() {
String input = "This is a plain text.";
assertEquals(input, HtmlEscapeUtils.escapeWithApacheCommons(input));
assertEquals(input, HtmlEscapeUtils.escapeWithGuava(input));
assertEquals(input, HtmlEscapeUtils.escapeWithSpring(input));
}
}