gnodet commented on code in PR #11568:
URL: https://github.com/apache/maven/pull/11568#discussion_r3369563723
##########
impl/maven-impl/src/main/java/org/apache/maven/api/services/model/UrlNormalizer.java:
##########
@@ -18,6 +18,17 @@
*/
package org.apache.maven.api.services.model;
+/**
+ * Simplifies URLs by removing parent directory references ("/../") and
collapsing path segments.
+ * This performs purely string-based normalization without full URL parsing or
validation.
+ *
+ * <p>The normalization process iteratively removes "/../" segments by
eliminating the preceding path segment,
+ * effectively resolving relative path traversals.
+ *
+ * <p>This does not guarantee that the resulting URL is valid or reachable; it
simply
Review Comment:
Nit: there is a trailing blank line before the closing `*/`. The existing
method javadoc in this same file does not have one. Consider removing it for
consistency:
```suggestion
* produces a more canonical representation of the input string.
*/
```
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultUrlNormalizerTest.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.impl;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class DefaultUrlNormalizerTest {
+
+ private final DefaultUrlNormalizer sut = new DefaultUrlNormalizer();
+
+ @Test
+ void normalizeShouldHandleNullAndEdgeCases() {
+ assertNull(sut.normalize(null));
+ assertEquals("", sut.normalize(""));
+ assertEquals("/", sut.normalize("/../"));
+ assertEquals("", sut.normalize("a/../"));
+ assertEquals("b", sut.normalize("a/../b"));
+ assertEquals("b/d", sut.normalize("a/../b/c/../d"));
+ assertEquals("b/c/d", sut.normalize("a/../b/c/d"));
+ assertEquals("b/c", sut.normalize("a/../b/c"));
+ assertEquals("b/", sut.normalize("a/../b/c/../"));
+ assertEquals("../", sut.normalize("../"));
+ }
+
+ @Test
+ void normalizeShouldPreserveHttpUrlTrailingSlash() {
+ assertEquals("https://example.com/path",
sut.normalize("https://example.com/path"));
+ assertEquals("https://example.com/path/",
sut.normalize("https://example.com/path/"));
+ }
+
+ @Test
+ void normalizeShouldCollapseParentReferencesInUrl() {
+ assertEquals("https://example.com/child",
sut.normalize("https://example.com/parent/../child"));
+ assertEquals("https://example.com/child",
sut.normalize("https://example.com/grand/parent/../../child"));
+ }
+
+ @Test
+ void normalizeHandlesDoubleSlashesAfterParent() {
+ assertEquals("https://example.com//child",
sut.normalize("https://example.com/parent/..//child"));
+ assertEquals("https://example.com/child",
sut.normalize("https://example.com/parent//../child"));
+ }
Review Comment:
Good idea testing with `file:////` and percent-encoded paths. You might also
consider adding the `scm:hg:ssh://localhost//home/user` case from the compat
tests, since SCM URLs are a common real-world input for this normalizer. Not a
blocker.
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultUrlNormalizerTest.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.impl;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class DefaultUrlNormalizerTest {
+
+ private final DefaultUrlNormalizer sut = new DefaultUrlNormalizer();
+
+ @Test
+ void normalizeShouldHandleNullAndEdgeCases() {
+ assertNull(sut.normalize(null));
+ assertEquals("", sut.normalize(""));
+ assertEquals("/", sut.normalize("/../"));
+ assertEquals("", sut.normalize("a/../"));
+ assertEquals("b", sut.normalize("a/../b"));
+ assertEquals("b/d", sut.normalize("a/../b/c/../d"));
+ assertEquals("b/c/d", sut.normalize("a/../b/c/d"));
+ assertEquals("b/c", sut.normalize("a/../b/c"));
+ assertEquals("b/", sut.normalize("a/../b/c/../"));
+ assertEquals("../", sut.normalize("../"));
+ }
Review Comment:
Minor: the `../` case at line 42 is testing a different behavioral category
(leading parent references that cannot be resolved) compared to the
null/empty/normal-traversal cases above it. Consider splitting this into its
own test method for clarity, similar to how the compat test
(`DefaultUrlNormalizerTest` under `compat/maven-model-builder`) has a dedicated
`leadingParentDirectoryNotRemovedFromRelativeUriReference` test. Not a blocker,
just a readability suggestion.
--
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]