1 package org.jsoup.nodes; 2 3 import org.jsoup.Jsoup; 4 import org.jsoup.parser.Parser; 5 import org.junit.jupiter.api.Test; 6 7 import static org.junit.jupiter.api.Assertions.assertEquals; 8 import static org.junit.jupiter.api.Assertions.assertThrows; 9 10 /** 11 * Tests for the DocumentType node 12 * 13 * @author Jonathan Hedley, http://jonathanhedley.com/ 14 */ 15 public class DocumentTypeTest { 16 @Test constructorValidationOkWithBlankName()17 public void constructorValidationOkWithBlankName() { 18 new DocumentType("","", ""); 19 } 20 21 @Test constructorValidationThrowsExceptionOnNulls()22 public void constructorValidationThrowsExceptionOnNulls() { 23 assertThrows(IllegalArgumentException.class, () -> new DocumentType("html", null, null)); 24 } 25 26 @Test constructorValidationOkWithBlankPublicAndSystemIds()27 public void constructorValidationOkWithBlankPublicAndSystemIds() { 28 new DocumentType("html","", ""); 29 } 30 outerHtmlGeneration()31 @Test public void outerHtmlGeneration() { 32 DocumentType html5 = new DocumentType("html", "", ""); 33 assertEquals("<!doctype html>", html5.outerHtml()); 34 35 DocumentType publicDocType = new DocumentType("html", "-//IETF//DTD HTML//", ""); 36 assertEquals("<!DOCTYPE html PUBLIC \"-//IETF//DTD HTML//\">", publicDocType.outerHtml()); 37 38 DocumentType systemDocType = new DocumentType("html", "", "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"); 39 assertEquals("<!DOCTYPE html SYSTEM \"http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd\">", systemDocType.outerHtml()); 40 41 DocumentType combo = new DocumentType("notHtml", "--public", "--system"); 42 assertEquals("<!DOCTYPE notHtml PUBLIC \"--public\" \"--system\">", combo.outerHtml()); 43 assertEquals("notHtml", combo.name()); 44 assertEquals("--public", combo.publicId()); 45 assertEquals("--system", combo.systemId()); 46 } 47 testRoundTrip()48 @Test public void testRoundTrip() { 49 String base = "<!DOCTYPE html>"; 50 assertEquals("<!doctype html>", htmlOutput(base)); 51 assertEquals(base, xmlOutput(base)); 52 53 String publicDoc = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"; 54 assertEquals(publicDoc, htmlOutput(publicDoc)); 55 assertEquals(publicDoc, xmlOutput(publicDoc)); 56 57 String systemDoc = "<!DOCTYPE html SYSTEM \"exampledtdfile.dtd\">"; 58 assertEquals(systemDoc, htmlOutput(systemDoc)); 59 assertEquals(systemDoc, xmlOutput(systemDoc)); 60 61 String legacyDoc = "<!DOCTYPE html SYSTEM \"about:legacy-compat\">"; 62 assertEquals(legacyDoc, htmlOutput(legacyDoc)); 63 assertEquals(legacyDoc, xmlOutput(legacyDoc)); 64 } 65 htmlOutput(String in)66 private String htmlOutput(String in) { 67 DocumentType type = (DocumentType) Jsoup.parse(in).childNode(0); 68 return type.outerHtml(); 69 } 70 xmlOutput(String in)71 private String xmlOutput(String in) { 72 return Jsoup.parse(in, "", Parser.xmlParser()).childNode(0).outerHtml(); 73 } 74 } 75