• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.jsoup.nodes;
2 
3 import org.jsoup.Jsoup;
4 import org.junit.jupiter.api.Test;
5 
6 import static org.junit.jupiter.api.Assertions.*;
7 
8 public class CommentTest {
9     private Comment comment = new Comment(" This is one heck of a comment! ");
10     private Comment decl = new Comment("?xml encoding='ISO-8859-1'?");
11 
12     @Test
nodeName()13     public void nodeName() {
14         assertEquals("#comment", comment.nodeName());
15     }
16 
17     @Test
getData()18     public void getData() {
19         assertEquals(" This is one heck of a comment! ", comment.getData());
20     }
21 
22     @Test
testToString()23     public void testToString() {
24         assertEquals("<!-- This is one heck of a comment! -->", comment.toString());
25 
26         Document doc = Jsoup.parse("<div><!-- comment--></div>");
27         assertEquals("<div>\n <!-- comment-->\n</div>", doc.body().html());
28 
29         doc = Jsoup.parse("<p>One<!-- comment -->Two</p>");
30         assertEquals("<p>One<!-- comment -->Two</p>", doc.body().html());
31         assertEquals("OneTwo", doc.text());
32     }
33 
34     @Test
testHtmlNoPretty()35     public void testHtmlNoPretty() {
36         Document doc = Jsoup.parse("<!-- a simple comment -->");
37         doc.outputSettings().prettyPrint(false);
38         assertEquals("<!-- a simple comment --><html><head></head><body></body></html>", doc.html());
39         Node node = doc.childNode(0);
40         Comment c1 = (Comment) node;
41         assertEquals("<!-- a simple comment -->", c1.outerHtml());
42     }
43 
stableIndentInBlock()44     @Test void stableIndentInBlock() {
45         String html = "<div><!-- comment --> Text</div><p><!-- comment --> Text</p>";
46         Document doc = Jsoup.parse(html);
47         String out = doc.body().html();
48         assertEquals("<div>\n" +
49             " <!-- comment --> Text\n" +
50             "</div>\n" +
51             "<p><!-- comment --> Text</p>", out);
52 
53         Document doc2 = Jsoup.parse(out);
54         String out2 = doc2.body().html();
55         assertEquals(out, out2);
56     }
57 
58     @Test
testClone()59     public void testClone() {
60         Comment c1 = comment.clone();
61         assertNotSame(comment, c1);
62         assertEquals(comment.getData(), comment.getData());
63         c1.setData("New");
64         assertEquals("New", c1.getData());
65         assertNotEquals(c1.getData(), comment.getData());
66     }
67 
68     @Test
isXmlDeclaration()69     public void isXmlDeclaration() {
70         assertFalse(comment.isXmlDeclaration());
71         assertTrue(decl.isXmlDeclaration());
72     }
73 
74     @Test
asXmlDeclaration()75     public void asXmlDeclaration() {
76         XmlDeclaration xmlDeclaration = decl.asXmlDeclaration();
77         assertNotNull(xmlDeclaration);
78     }
79 }
80