|
19 | 19 | package org.apache.maven.impl; |
20 | 20 |
|
21 | 21 | import java.io.StringReader; |
| 22 | +import java.io.StringWriter; |
| 23 | +import java.util.function.Function; |
22 | 24 |
|
23 | 25 | import org.apache.maven.api.model.Model; |
24 | 26 | import org.apache.maven.api.services.xml.XmlReaderException; |
25 | 27 | import org.apache.maven.api.services.xml.XmlReaderRequest; |
| 28 | +import org.apache.maven.api.services.xml.XmlWriterRequest; |
26 | 29 | import org.junit.jupiter.api.BeforeEach; |
27 | 30 | import org.junit.jupiter.api.Test; |
28 | 31 |
|
29 | 32 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
30 | 34 | import static org.junit.jupiter.api.Assertions.assertThrows; |
31 | 35 | import static org.junit.jupiter.api.Assertions.assertTrue; |
32 | 36 |
|
@@ -122,4 +126,62 @@ void testMalformedModelVersion() throws Exception { |
122 | 126 | Model model = factory.read(request); |
123 | 127 | assertEquals("invalid.version", model.getModelVersion()); |
124 | 128 | } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testWriteWithoutFormatterDisablesLocationTracking() throws Exception { |
| 132 | + // minimal valid model we can round-trip |
| 133 | + String xml = |
| 134 | + """ |
| 135 | + <project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 136 | + <modelVersion>4.0.0</modelVersion> |
| 137 | + <groupId>g</groupId> |
| 138 | + <artifactId>a</artifactId> |
| 139 | + <version>1</version> |
| 140 | + </project>"""; |
| 141 | + |
| 142 | + Model model = factory.read(XmlReaderRequest.builder() |
| 143 | + .reader(new StringReader(xml)) |
| 144 | + .strict(true) |
| 145 | + .build()); |
| 146 | + |
| 147 | + StringWriter out = new StringWriter(); |
| 148 | + factory.write(XmlWriterRequest.<Model>builder() |
| 149 | + .writer(out) |
| 150 | + .content(model) |
| 151 | + // no formatter -> tracking should be OFF |
| 152 | + .build()); |
| 153 | + |
| 154 | + String result = out.toString(); |
| 155 | + assertFalse(result.contains("LOC_MARK"), "Unexpected marker found in output"); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testWriteWithFormatterEnablesLocationTracking() throws Exception { |
| 160 | + String xml = |
| 161 | + """ |
| 162 | + <project xmlns="http://maven.apache.org/POM/4.0.0"> |
| 163 | + <modelVersion>4.0.0</modelVersion> |
| 164 | + <groupId>g</groupId> |
| 165 | + <artifactId>a</artifactId> |
| 166 | + <version>1</version> |
| 167 | + </project>"""; |
| 168 | + |
| 169 | + Model model = factory.read(XmlReaderRequest.builder() |
| 170 | + .reader(new StringReader(xml)) |
| 171 | + .strict(true) |
| 172 | + .build()); |
| 173 | + |
| 174 | + StringWriter out = new StringWriter(); |
| 175 | + Function<Object, String> formatter = o -> "LOC_MARK"; |
| 176 | + |
| 177 | + factory.write(XmlWriterRequest.<Model>builder() |
| 178 | + .writer(out) |
| 179 | + .content(model) |
| 180 | + .inputLocationFormatter(formatter) |
| 181 | + .build()); |
| 182 | + |
| 183 | + String result = out.toString(); |
| 184 | + // Presence of our formatter's output proves tracking was enabled and formatter applied |
| 185 | + assertTrue(result.contains("LOC_MARK"), "Expected formatter marker in output when tracking is enabled"); |
| 186 | + } |
125 | 187 | } |
0 commit comments