elharo commented on code in PR #11185:
URL: https://github.com/apache/maven/pull/11185#discussion_r2386262201


##########
impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderNamespaceTest.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.model.v4;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.StringReader;
+
+import org.apache.maven.api.model.InputSource;
+import org.apache.maven.api.model.Model;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MavenStaxReaderNamespaceTest {
+
+    private static final InputSource NO_INPUT_SOURCE = null;
+
+    private static final String POM_41 = ""
+            + "<project xmlns=\"http://maven.apache.org/POM/4.1.0\"; "
+            + "         
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>"
+            + "  <modelVersion>4.1.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_40 = ""
+            + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"; "
+            + "         
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>"
+            + "  <modelVersion>4.0.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_NO_NS = ""
+            + "<project>"
+            + "  <modelVersion>4.0.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_BAD_NS = ""
+            + "<project xmlns=\"http://example.com/not/pom\";>"
+            + "  <modelVersion>4.1.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    @Test
+    void acceptsPom41() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_41), /*strict*/ true, 
NO_INPUT_SOURCE);

Review Comment:
   m --> model



##########
impl/maven-support/src/test/java/org/apache/maven/model/v4/MetadataStaxReaderNamespaceTest.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.model.v4;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.StringReader;
+
+import org.apache.maven.api.metadata.Metadata;
+import org.apache.maven.metadata.v4.MetadataStaxReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MetadataStaxReaderNamespaceTest {
+
+    private static final String META_110 = ""
+            + "<metadata xmlns=\"http://maven.apache.org/METADATA/1.1.0\";>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <versioning>"
+            + "    <latest>1.0</latest>"
+            + "    <release>1.0</release>"
+            + "  </versioning>"
+            + "</metadata>";
+
+    private static final String META_NO_NS = ""
+            + "<metadata>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <versioning>"
+            + "    <latest>1.0</latest>"
+            + "  </versioning>"
+            + "</metadata>";
+
+    private static final String META_BAD_NS = ""
+            + "<metadata xmlns=\"http://example.com/not/metadata\";>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "</metadata>";
+
+    @Test
+    void acceptsMetadata110() throws Exception {
+        MetadataStaxReader reader = new MetadataStaxReader();
+        Metadata md = reader.read(new StringReader(META_110), /*strict*/ true);

Review Comment:
   md --> metadata



##########
impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderNamespaceTest.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.model.v4;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.StringReader;
+
+import org.apache.maven.api.model.InputSource;
+import org.apache.maven.api.model.Model;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MavenStaxReaderNamespaceTest {
+
+    private static final InputSource NO_INPUT_SOURCE = null;
+
+    private static final String POM_41 = ""
+            + "<project xmlns=\"http://maven.apache.org/POM/4.1.0\"; "
+            + "         
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>"
+            + "  <modelVersion>4.1.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_40 = ""
+            + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"; "
+            + "         
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>"
+            + "  <modelVersion>4.0.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_NO_NS = ""
+            + "<project>"
+            + "  <modelVersion>4.0.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_BAD_NS = ""
+            + "<project xmlns=\"http://example.com/not/pom\";>"
+            + "  <modelVersion>4.1.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    @Test
+    void acceptsPom41() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_41), /*strict*/ true, 
NO_INPUT_SOURCE);
+        assertNotNull(m);
+        assertEquals("com.acme", m.getGroupId());
+        assertEquals("demo", m.getArtifactId());
+        assertEquals("1.0", m.getVersion());
+    }
+
+    @Test
+    void acceptsPom40() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_40), true, NO_INPUT_SOURCE);
+        assertNotNull(m);
+        assertEquals("com.acme", m.getGroupId());
+        assertEquals("demo", m.getArtifactId());
+        assertEquals("1.0", m.getVersion());
+    }
+
+    @Test
+    void acceptsPomWithoutNamespace() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_NO_NS), true, 
NO_INPUT_SOURCE);
+        assertNotNull(m);
+        assertEquals("com.acme", m.getGroupId());
+        assertEquals("demo", m.getArtifactId());
+        assertEquals("1.0", m.getVersion());
+    }
+
+    @Test
+    void rejectsUnexpectedPomNamespace() {
+        MavenStaxReader reader = new MavenStaxReader();
+        XMLStreamException ex = assertThrows(
+                XMLStreamException.class, () -> reader.read(new 
StringReader(POM_BAD_NS), true, NO_INPUT_SOURCE));
+        // sanity check: message mentions unrecognized namespace
+        assertTrue(ex.getMessage().toLowerCase().contains("unrecognized pom 
namespace"));

Review Comment:
   Locale.ROOT



##########
impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderNamespaceTest.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.model.v4;
+
+import javax.xml.stream.XMLStreamException;
+
+import java.io.StringReader;
+
+import org.apache.maven.api.model.InputSource;
+import org.apache.maven.api.model.Model;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MavenStaxReaderNamespaceTest {
+
+    private static final InputSource NO_INPUT_SOURCE = null;
+
+    private static final String POM_41 = ""
+            + "<project xmlns=\"http://maven.apache.org/POM/4.1.0\"; "
+            + "         
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>"
+            + "  <modelVersion>4.1.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_40 = ""
+            + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"; "
+            + "         
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>"
+            + "  <modelVersion>4.0.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_NO_NS = ""
+            + "<project>"
+            + "  <modelVersion>4.0.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    private static final String POM_BAD_NS = ""
+            + "<project xmlns=\"http://example.com/not/pom\";>"
+            + "  <modelVersion>4.1.0</modelVersion>"
+            + "  <groupId>com.acme</groupId>"
+            + "  <artifactId>demo</artifactId>"
+            + "  <version>1.0</version>"
+            + "</project>";
+
+    @Test
+    void acceptsPom41() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_41), /*strict*/ true, 
NO_INPUT_SOURCE);
+        assertNotNull(m);
+        assertEquals("com.acme", m.getGroupId());
+        assertEquals("demo", m.getArtifactId());
+        assertEquals("1.0", m.getVersion());
+    }
+
+    @Test
+    void acceptsPom40() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_40), true, NO_INPUT_SOURCE);
+        assertNotNull(m);
+        assertEquals("com.acme", m.getGroupId());
+        assertEquals("demo", m.getArtifactId());
+        assertEquals("1.0", m.getVersion());
+    }
+
+    @Test
+    void acceptsPomWithoutNamespace() throws Exception {
+        MavenStaxReader reader = new MavenStaxReader();
+        Model m = reader.read(new StringReader(POM_NO_NS), true, 
NO_INPUT_SOURCE);

Review Comment:
   m --> model



##########
src/mdo/reader-stax.vm:
##########
@@ -246,51 +246,76 @@ public class ${className} {
 #end
     } //-- ${root.name} read(InputStream, boolean)
 
-    /**
-     * Method read.
-     *
-     * @param parser a parser object.
-     * @param strict a strict object.
-     * @throws XMLStreamException XMLStreamException if
-     * any.
-     * @return ${root.name}
-     */
+/**
+* Method read.
+*
+* @param parser a parser object.
+* @param strict a strict object.
+* @throws XMLStreamException XMLStreamException if any.
+* @return ${root.name}
+*/
 #if ( $locationTracking )
-    public ${root.name} read(XMLStreamReader parser, boolean strict, 
InputSource inputSrc) throws XMLStreamException {
+public ${root.name} read(XMLStreamReader parser, boolean strict, InputSource 
inputSrc) throws XMLStreamException {
 #else
-    public ${root.name} read(XMLStreamReader parser, boolean strict) throws 
XMLStreamException {
+public ${root.name} read(XMLStreamReader parser, boolean strict) throws 
XMLStreamException {
 #end
 #if ( $needXmlContext )
-        Deque<Object> context = new ArrayDeque<>();
+Deque<Object> context = new ArrayDeque<>();
 #end
-        $rootUcapName $rootLcapName = null;
-        int eventType = parser.getEventType();
-        boolean parsed = false;
-        while (eventType != XMLStreamReader.END_DOCUMENT) {
-            if (eventType == XMLStreamReader.START_ELEMENT) {
-                if (strict && ! "${rootTag}".equals(parser.getLocalName())) {
-                    throw new XMLStreamException("Expected root element 
'${rootTag}' but found '" + parser.getName() + "'", parser.getLocation(), null);
-                } else if (parsed) {
-                    // fallback, already expected a XMLStreamException due to 
invalid XML
-                    throw new XMLStreamException("Duplicated tag: 
'${rootTag}'", parser.getLocation(), null);
-                }
+$rootUcapName $rootLcapName = null;
+    int eventType = parser.getEventType();
+    boolean parsed = false;
+    while (eventType != XMLStreamReader.END_DOCUMENT) {
+    if (eventType == XMLStreamReader.START_ELEMENT) {
+    if (strict && ! "${rootTag}".equals(parser.getLocalName())) {
+    throw new XMLStreamException("Expected root element '${rootTag}' but found 
'" + parser.getName() + "'", parser.getLocation(), null);
+    } else if (parsed) {
+    throw new XMLStreamException("Duplicated tag: '${rootTag}'", 
parser.getLocation(), null);
+    }
+
+    // Enforce root namespace per model (strict mode).
+    String rootNs = parser.getNamespaceURI();
+    boolean hasNs = rootNs != null && !rootNs.isEmpty();

Review Comment:
   hasNamespace



##########
src/mdo/reader-stax.vm:
##########
@@ -246,51 +246,76 @@ public class ${className} {
 #end
     } //-- ${root.name} read(InputStream, boolean)
 
-    /**
-     * Method read.
-     *
-     * @param parser a parser object.
-     * @param strict a strict object.
-     * @throws XMLStreamException XMLStreamException if
-     * any.
-     * @return ${root.name}
-     */
+/**
+* Method read.
+*
+* @param parser a parser object.
+* @param strict a strict object.
+* @throws XMLStreamException XMLStreamException if any.
+* @return ${root.name}
+*/
 #if ( $locationTracking )
-    public ${root.name} read(XMLStreamReader parser, boolean strict, 
InputSource inputSrc) throws XMLStreamException {
+public ${root.name} read(XMLStreamReader parser, boolean strict, InputSource 
inputSrc) throws XMLStreamException {
 #else
-    public ${root.name} read(XMLStreamReader parser, boolean strict) throws 
XMLStreamException {
+public ${root.name} read(XMLStreamReader parser, boolean strict) throws 
XMLStreamException {
 #end
 #if ( $needXmlContext )
-        Deque<Object> context = new ArrayDeque<>();
+Deque<Object> context = new ArrayDeque<>();
 #end
-        $rootUcapName $rootLcapName = null;
-        int eventType = parser.getEventType();
-        boolean parsed = false;
-        while (eventType != XMLStreamReader.END_DOCUMENT) {
-            if (eventType == XMLStreamReader.START_ELEMENT) {
-                if (strict && ! "${rootTag}".equals(parser.getLocalName())) {
-                    throw new XMLStreamException("Expected root element 
'${rootTag}' but found '" + parser.getName() + "'", parser.getLocation(), null);
-                } else if (parsed) {
-                    // fallback, already expected a XMLStreamException due to 
invalid XML
-                    throw new XMLStreamException("Duplicated tag: 
'${rootTag}'", parser.getLocation(), null);
-                }
+$rootUcapName $rootLcapName = null;
+    int eventType = parser.getEventType();
+    boolean parsed = false;
+    while (eventType != XMLStreamReader.END_DOCUMENT) {
+    if (eventType == XMLStreamReader.START_ELEMENT) {
+    if (strict && ! "${rootTag}".equals(parser.getLocalName())) {
+    throw new XMLStreamException("Expected root element '${rootTag}' but found 
'" + parser.getName() + "'", parser.getLocation(), null);
+    } else if (parsed) {
+    throw new XMLStreamException("Duplicated tag: '${rootTag}'", 
parser.getLocation(), null);
+    }
+
+    // Enforce root namespace per model (strict mode).
+    String rootNs = parser.getNamespaceURI();

Review Comment:
   rootNamespace



-- 
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]

Reply via email to