This is an automated email from the ASF dual-hosted git repository.

bmarwell pushed a commit to branch MWRAPPER-86_trim_repourl_slash
in repository https://gitbox.apache.org/repos/asf/maven-wrapper.git

commit 8175cca96776efd5eab4e7b82f407152a4166a75
Author: Benjamin Marwell <bmarw...@apache.org>
AuthorDate: Fri Feb 9 16:37:16 2024 +0100

    [MWRAPPER-86] open methods for tests; refactor; add tests
---
 maven-wrapper-plugin/pom.xml                       |  7 ++
 .../apache/maven/plugins/wrapper/WrapperMojo.java  | 38 ++++++----
 .../maven/plugins/wrapper/WrapperMojoTest.java     | 83 ++++++++++++++++++++++
 3 files changed, 113 insertions(+), 15 deletions(-)

diff --git a/maven-wrapper-plugin/pom.xml b/maven-wrapper-plugin/pom.xml
index 199b245..7e0f7b7 100644
--- a/maven-wrapper-plugin/pom.xml
+++ b/maven-wrapper-plugin/pom.xml
@@ -100,6 +100,13 @@ under the License.
       <type>pom</type>
       <scope>test</scope>
     </dependency>
+
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+      <version>5.10.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git 
a/maven-wrapper-plugin/src/main/java/org/apache/maven/plugins/wrapper/WrapperMojo.java
 
b/maven-wrapper-plugin/src/main/java/org/apache/maven/plugins/wrapper/WrapperMojo.java
index 9674b58..8d91d98 100644
--- 
a/maven-wrapper-plugin/src/main/java/org/apache/maven/plugins/wrapper/WrapperMojo.java
+++ 
b/maven-wrapper-plugin/src/main/java/org/apache/maven/plugins/wrapper/WrapperMojo.java
@@ -62,7 +62,7 @@ import static 
org.apache.maven.shared.utils.logging.MessageUtils.buffer;
 public class WrapperMojo extends AbstractMojo {
     private static final String MVNW_REPOURL = "MVNW_REPOURL";
 
-    private static final String DEFAULT_REPOURL = 
"https://repo.maven.apache.org/maven2";;
+    protected static final String DEFAULT_REPOURL = 
"https://repo.maven.apache.org/maven2";;
 
     // CONFIGURATION PARAMETERS
 
@@ -334,31 +334,39 @@ public class WrapperMojo extends AbstractMojo {
      * Determine the repository URL to download Wrapper and Maven from.
      */
     private String getRepoUrl() {
-        // default
-        String repoUrl = DEFAULT_REPOURL;
-
         // adapt to also support MVNW_REPOURL as supported by mvnw scripts 
from maven-wrapper
-        String mvnwRepoUrl = System.getenv(MVNW_REPOURL);
-        if (mvnwRepoUrl != null && !mvnwRepoUrl.trim().isEmpty()) {
-            repoUrl = mvnwRepoUrl.trim();
+        String envRepoUrl = System.getenv(MVNW_REPOURL);
+        final String repoUrl = determineRepoUrl(envRepoUrl, this.settings);
+
+        getLog().debug("Determined repo URL to use as " + repoUrl);
+
+        return repoUrl;
+    }
+
+    protected String determineRepoUrl(String envRepoUrl, Settings settings) {
+
+        if (envRepoUrl != null && !envRepoUrl.trim().isEmpty() && 
envRepoUrl.length() > 4) {
+            String repoUrl = envRepoUrl.trim();
+
             if (repoUrl.endsWith("/")) {
-                repoUrl = repoUrl.substring(0, repoUrl.length() - 2);
+                repoUrl = repoUrl.substring(0, repoUrl.length() - 1);
             }
+
             getLog().debug("Using repo URL from " + MVNW_REPOURL + " 
environment variable.");
+
+            return repoUrl;
         }
+
         // otherwise mirror from settings
-        else if (settings.getMirrors() != null && 
!settings.getMirrors().isEmpty()) {
+        if (settings.getMirrors() != null && !settings.getMirrors().isEmpty()) 
{
             for (Mirror current : settings.getMirrors()) {
                 if ("*".equals(current.getMirrorOf())) {
-                    repoUrl = current.getUrl();
-                    break;
+                    getLog().debug("Using repo URL from * mirror in settings 
file.");
+                    return current.getUrl();
                 }
             }
-            getLog().debug("Using repo URL from * mirror in settings file.");
         }
 
-        getLog().debug("Determined repo URL to use as " + repoUrl);
-
-        return repoUrl;
+        return DEFAULT_REPOURL;
     }
 }
diff --git 
a/maven-wrapper-plugin/src/test/java/org/apache/maven/plugins/wrapper/WrapperMojoTest.java
 
b/maven-wrapper-plugin/src/test/java/org/apache/maven/plugins/wrapper/WrapperMojoTest.java
new file mode 100644
index 0000000..4fcaec9
--- /dev/null
+++ 
b/maven-wrapper-plugin/src/test/java/org/apache/maven/plugins/wrapper/WrapperMojoTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.plugins.wrapper;
+
+import org.apache.maven.settings.Settings;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+class WrapperMojoTest {
+
+    @Test
+    void user_supplied_repo_url_gets_trailing_slash_trimmed() {
+        // given
+        Settings settings = new Settings();
+        WrapperMojo wrapperMojo = new WrapperMojo();
+
+        // when
+        String determinedRepoUrl = 
wrapperMojo.determineRepoUrl(WrapperMojo.DEFAULT_REPOURL + "/", settings);
+
+        // then
+        Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, 
determinedRepoUrl);
+    }
+
+    @Test
+    void null_repo_url_not_used() {
+        // given
+        Settings settings = new Settings();
+        WrapperMojo wrapperMojo = new WrapperMojo();
+
+        // when
+        String determinedRepoUrl = wrapperMojo.determineRepoUrl(null, 
settings);
+
+        // then
+        Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, 
determinedRepoUrl);
+    }
+
+    @Test
+    void empty_repo_url_not_used() {
+        // given
+        Settings settings = new Settings();
+        WrapperMojo wrapperMojo = new WrapperMojo();
+
+        // when
+        String determinedRepoUrl = wrapperMojo.determineRepoUrl("", settings);
+
+        // then
+        Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, 
determinedRepoUrl);
+    }
+
+    @Test
+    void slash_repo_url_not_used() {
+        // given
+        Settings settings = new Settings();
+        WrapperMojo wrapperMojo = new WrapperMojo();
+
+        // when
+        String determinedRepoUrl = wrapperMojo.determineRepoUrl("/", settings);
+
+        // then
+        Assertions.assertEquals(WrapperMojo.DEFAULT_REPOURL, 
determinedRepoUrl);
+    }
+}

Reply via email to