You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2020/12/23 10:42:40 UTC

[maven-resolver] 01/01: [MRESOLVER-151] Enforce a checksum policy to be provided explicitly

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

michaelo pushed a commit to branch MRSOLVER-151
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git

commit 74acea3b7d33d4db9bcd5371dd06ed256f27536a
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Wed Dec 23 11:40:55 2020 +0100

    [MRESOLVER-151] Enforce a checksum policy to be provided explicitly
---
 .../impl/DefaultChecksumPolicyProvider.java        | 63 +++++++++++++++-------
 .../impl/DefaultChecksumPolicyProviderTest.java    | 10 ++--
 2 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProvider.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProvider.java
index 9385e9a..9059802 100644
--- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProvider.java
+++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProvider.java
@@ -8,9 +8,9 @@ package org.eclipse.aether.internal.impl;
  * 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
@@ -21,6 +21,8 @@ package org.eclipse.aether.internal.impl;
 
 import javax.inject.Named;
 
+import java.util.Objects;
+
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.repository.RemoteRepository;
 import org.eclipse.aether.repository.RepositoryPolicy;
@@ -49,20 +51,31 @@ public final class DefaultChecksumPolicyProvider
     public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
                                              TransferResource resource, String policy )
     {
-        if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
-        {
-            return null;
-        }
-        if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
+        Objects.requireNonNull( session, "session cannot be null" );
+        Objects.requireNonNull( repository, "repository cannot be null" );
+        Objects.requireNonNull( resource, "resource cannot be null" );
+        validatePolicy( "policy", policy );
+
+        switch ( policy )
         {
-            return new FailChecksumPolicy( resource );
+            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
+                return null;
+            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
+                return new FailChecksumPolicy( resource );
+            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
+                return new WarnChecksumPolicy( resource );
+            default:
+                throw new IllegalArgumentException( "Unsupported policy: " + policy );
         }
-        return new WarnChecksumPolicy( resource );
     }
 
     public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
     {
-        if ( policy1 != null && policy1.equals( policy2 ) )
+        Objects.requireNonNull( session, "session cannot be null" );
+        validatePolicy( "policy1", policy1 );
+        validatePolicy( "policy2", policy2 );
+
+        if ( policy1.equals( policy2 ) )
         {
             return policy1;
         }
@@ -80,17 +93,31 @@ public final class DefaultChecksumPolicyProvider
 
     private static int ordinalOfPolicy( String policy )
     {
-        if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
-        {
-            return ORDINAL_FAIL;
-        }
-        else if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
+        switch ( policy )
         {
-            return ORDINAL_IGNORE;
+            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
+                return ORDINAL_IGNORE;
+            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
+                return ORDINAL_FAIL;
+            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
+                return ORDINAL_WARN;
+            default:
+                throw new IllegalArgumentException( "Unsupported policy: " + policy );
         }
-        else
+    }
+
+    private static void validatePolicy( String paramName, String policy )
+    {
+        Objects.requireNonNull( policy, paramName + "cannot be null" );
+
+        switch ( policy )
         {
-            return ORDINAL_WARN;
+            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
+            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
+            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
+                break;
+            default:
+                throw new IllegalArgumentException( "Unsupported policy: " + policy );
         }
     }
 
diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java
index 4f508ec..9a483fd 100644
--- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java
+++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultChecksumPolicyProviderTest.java
@@ -8,9 +8,9 @@ package org.eclipse.aether.internal.impl;
  * 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
@@ -88,7 +88,7 @@ public class DefaultChecksumPolicyProviderTest
         assertNull( policy );
     }
 
-    @Test
+    @Test( expected = IllegalArgumentException.class )
     public void testNewChecksumPolicy_Unknown()
     {
         ChecksumPolicy policy = provider.newChecksumPolicy( session, repository, resource, CHECKSUM_POLICY_UNKNOWN );
@@ -101,7 +101,7 @@ public class DefaultChecksumPolicyProviderTest
     {
         String[] policies =
             { RepositoryPolicy.CHECKSUM_POLICY_FAIL, RepositoryPolicy.CHECKSUM_POLICY_WARN,
-                RepositoryPolicy.CHECKSUM_POLICY_IGNORE, CHECKSUM_POLICY_UNKNOWN };
+                RepositoryPolicy.CHECKSUM_POLICY_IGNORE };
         for ( String policy : policies )
         {
             assertEquals( policy, policy, provider.getEffectiveChecksumPolicy( session, policy, policy ) );
@@ -124,7 +124,7 @@ public class DefaultChecksumPolicyProviderTest
         }
     }
 
-    @Test
+    @Test( expected = IllegalArgumentException.class )
     public void testGetEffectiveChecksumPolicy_UnknownPolicies()
     {
         String[][] testCases =