gnodet-bot commented on code in PR #13882:
URL: https://github.com/apache/maven/pull/13882#discussion_r4547601293
##########
impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderTest.java:
##########
@@ -281,6 +283,69 @@ void testLocationReportingForListElements() throws
Exception {
assertEquals(5, module3Location.getColumnNumber(), "Module 3 should
start at column 5");
}
+ @Test
+ void testRandomRootNamespaceIsRejectedInStrictMode() {
+ // https://github.com/apache/maven/issues/10985
+ String xml = pomWithNamespace("https://ff.com");
+
+ XMLStreamException ex = assertThrows(XMLStreamException.class, () ->
fromXml(xml));
+ assertTrue(
+ ex.getMessage().contains("Unrecognized namespace
'https://ff.com' for root element 'project'"),
+ ex.getMessage());
+
assertTrue(ex.getMessage().contains("http://maven.apache.org/POM/4.0.0"),
ex.getMessage());
+ assertNotNull(ex.getLocation());
+ assertEquals(1, ex.getLocation().getLineNumber());
+ }
+
+ @Test
+ void testUnknownRootNamespacesAreRejectedInStrictMode() {
+ assertAll(Stream.of(
+ // only exact matches are accepted, no prefix matching
+ "http://maven.apache.org/POM/",
+ "http://maven.apache.org/POM/4.0",
+ "http://maven.apache.org/POM/4.0.0/",
+ "http://maven.apache.org/POM/9.9.9",
+ // namespaces are case-sensitive and the scheme is
part of the namespace name
+ "https://maven.apache.org/POM/4.0.0",
+ "http://maven.apache.org/pom/4.0.0",
+ // namespaces of other Maven models are not valid for
a POM
+ "http://maven.apache.org/METADATA/1.1.0",
+ "http://maven.apache.org/SETTINGS/1.0.0")
+ .map(ns -> () -> {
+ XMLStreamException ex =
+ assertThrows(XMLStreamException.class, () ->
fromXml(pomWithNamespace(ns)), ns);
+ assertTrue(ex.getMessage().contains("Unrecognized
namespace '" + ns + "'"), ex.getMessage());
+ }));
+ }
+
+ @Test
+ void testKnownRootNamespacesAreAcceptedInStrictMode() {
+ assertAll(Stream.of(
+ "http://maven.apache.org/POM/4.0.0",
+ "http://maven.apache.org/POM/4.1.0",
+ "http://maven.apache.org/POM/4.2.0")
+ .map(ns -> () -> assertEquals(ns,
fromXml(pomWithNamespace(ns)).getNamespaceUri(), ns)));
+ }
+
+ @Test
+ void testRandomRootNamespaceIsAcceptedInLenientMode() throws
XMLStreamException {
+ // POMs consumed from repositories are read leniently and must keep
working
+ Model model = new MavenStaxReader().read(new
StringReader(pomWithNamespace("https://ff.com")), false, null);
+
+ assertEquals("https://ff.com", model.getNamespaceUri());
+ assertEquals("missing-namespace", model.getArtifactId());
+ }
+
+ private static String pomWithNamespace(String namespace) {
+ return "<project xmlns=\"" + namespace + "\">\n"
+ + " <modelVersion>4.0.0</modelVersion>\n"
+ + " <groupId>org.apache.maven.its</groupId>\n"
+ + " <artifactId>missing-namespace</artifactId>\n"
+ + " <version>1.0-SNAPSHOT</version>\n"
Review Comment:
💡 Minor naming nit: `missing-namespace` implies the namespace is absent, but
the helper is used to build POMs with an *unrecognized* namespace (one is
always provided). Consider renaming to `unrecognized-namespace` for clarity —
`testRandomRootNamespaceIsAcceptedInLenientMode` would then read
`assertEquals("unrecognized-namespace", model.getArtifactId())`, which is
immediately clear.
```suggestion
+ " <artifactId>unrecognized-namespace</artifactId>\n"
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]