|
| 1 | +/* |
| 2 | + * Copyright 2012-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.assertj.tests.core.util.xml; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.catchException; |
| 19 | +import static org.assertj.core.api.BDDAssertions.then; |
| 20 | +import static org.assertj.core.util.xml.XmlStringPrettyFormatter.xmlPrettyFormat; |
| 21 | +import static org.junit.jupiter.params.provider.Arguments.argumentSet; |
| 22 | + |
| 23 | +import java.math.BigDecimal; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.FieldSource; |
| 29 | +import org.junit.jupiter.params.provider.ValueSource; |
| 30 | +import org.junitpioneer.jupiter.DefaultLocale; |
| 31 | +import org.xml.sax.SAXParseException; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Joel Costigliola |
| 35 | + */ |
| 36 | +@DefaultLocale("en") |
| 37 | +class XmlStringPrettyFormatter_prettyFormat_Test { |
| 38 | + |
| 39 | + private final BigDecimal javaVersion = new BigDecimal(System.getProperty("java.specification.version")); |
| 40 | + |
| 41 | + private final String expected_formatted_xml = javaVersion.compareTo(new BigDecimal("9")) >= 0 |
| 42 | + ? """ |
| 43 | + <?xml version="1.0" encoding="UTF-8"?><rss version="2.0"> |
| 44 | + <channel> |
| 45 | + <title>Java Tutorials and Examples 1</title> |
| 46 | + <language>en-us</language> |
| 47 | + </channel> |
| 48 | + </rss> |
| 49 | + """ |
| 50 | + : """ |
| 51 | + <?xml version="1.0" encoding="UTF-8"?> |
| 52 | + <rss version="2.0"> |
| 53 | + <channel> |
| 54 | + <title>Java Tutorials and Examples 1</title> |
| 55 | + <language>en-us</language> |
| 56 | + </channel> |
| 57 | + </rss> |
| 58 | + """; |
| 59 | + |
| 60 | + @Test |
| 61 | + void should_format_input_prettily() { |
| 62 | + // GIVEN |
| 63 | + String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel><title>Java Tutorials and Examples 1</title><language>en-us</language></channel></rss>"; |
| 64 | + // WHEN |
| 65 | + String result = xmlPrettyFormat(xmlString); |
| 66 | + // THEN |
| 67 | + then(result).isEqualTo(expected_formatted_xml); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void should_format_input_without_xml_declaration_prettily() { |
| 72 | + // GIVEN |
| 73 | + String xmlString = "<rss version=\"2.0\"><channel><title>Java Tutorials and Examples 1</title><language>en-us</language></channel></rss>"; |
| 74 | + // WHEN |
| 75 | + String result = xmlPrettyFormat(xmlString); |
| 76 | + // THEN |
| 77 | + if (javaVersion.compareTo(new BigDecimal("9")) >= 0) { |
| 78 | + then(result).isEqualTo(expected_formatted_xml.substring("<?xml version='1.0' encoding='UTF-8'?>".length())); |
| 79 | + } else { |
| 80 | + then(result).isEqualTo(expected_formatted_xml.substring("<?xml version='1.0' encoding='UTF-8'?>\n".length())); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void should_format_input_with_space_and_newline_prettily() { |
| 86 | + // GIVEN |
| 87 | + String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel> <title>Java Tutorials and Examples 1</title> \n\n<language>en-us</language> </channel></rss>"; |
| 88 | + // WHEN |
| 89 | + String result = xmlPrettyFormat(xmlString); |
| 90 | + // THEN |
| 91 | + then(result).isEqualTo(expected_formatted_xml); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void should_fail_if_input_is_null() { |
| 96 | + // WHEN |
| 97 | + Exception exception = catchException(() -> xmlPrettyFormat(null)); |
| 98 | + // THEN |
| 99 | + then(exception).isInstanceOf(IllegalArgumentException.class) |
| 100 | + .hasMessageStartingWith("Expecting XML String not to be null"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void should_fail_if_input_is_not_valid() { |
| 105 | + // GIVEN |
| 106 | + String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel><title>Java Tutorials and Examples 1</title><language>en-us</language></chnel></rss>"; |
| 107 | + // WHEN |
| 108 | + Exception exception = catchException(() -> xmlPrettyFormat(xmlString)); |
| 109 | + // THEN |
| 110 | + then(exception).isInstanceOf(RuntimeException.class) |
| 111 | + .hasMessage("Unable to format XML string") |
| 112 | + .hasRootCauseInstanceOf(SAXParseException.class) |
| 113 | + .cause() |
| 114 | + .hasMessageContaining("The element type \"channel\" must be terminated by the matching end-tag \"</channel>\""); |
| 115 | + } |
| 116 | + |
| 117 | + @ParameterizedTest |
| 118 | + @ValueSource(strings = { |
| 119 | + // Injection into document content |
| 120 | + """ |
| 121 | + <?xml version="1.0"?> |
| 122 | + <!DOCTYPE root [ |
| 123 | + <!ENTITY xxe SYSTEM "file:///etc/hosts"> |
| 124 | + ]> |
| 125 | + <root>&xxe;</root> |
| 126 | + """, |
| 127 | + // Injection during document parsing |
| 128 | + """ |
| 129 | + <?xml version="1.0"?> |
| 130 | + <!DOCTYPE root [ |
| 131 | + <!ENTITY % xxe SYSTEM "file:///etc/hosts"> |
| 132 | + %xxe; |
| 133 | + ]> |
| 134 | + <root>foo</root> |
| 135 | + """ |
| 136 | + }) |
| 137 | + void should_fail_if_input_contains_doctype_declaration(String input) { |
| 138 | + // WHEN |
| 139 | + Exception exception = catchException(() -> xmlPrettyFormat(input)); |
| 140 | + // THEN |
| 141 | + then(exception).isInstanceOf(RuntimeException.class) |
| 142 | + .hasMessage("Unable to format XML string") |
| 143 | + .cause() |
| 144 | + .isInstanceOf(SAXParseException.class) |
| 145 | + .hasMessageContaining("DOCTYPE is disallowed"); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments