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/24 13:16:58 UTC

[maven-resolver] branch master updated: [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 master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 01d1666  [MRESOLVER-151] Enforce a checksum policy to be provided explicitly
01d1666 is described below

commit 01d16666164113f08eba9a8b4cbb5499f01b05b1
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
    
    This closes #83
---
 .../impl/DefaultChecksumPolicyProvider.java        | 63 +++++++++++++++-------
 .../impl/DefaultChecksumPolicyProviderTest.java    | 25 +++++----
 2 files changed, 61 insertions(+), 27 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..7025771 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
@@ -19,7 +19,10 @@ package org.eclipse.aether.internal.impl;
  * under the License.
  */
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 
 import org.eclipse.aether.DefaultRepositorySystemSession;
 import org.eclipse.aether.internal.test.util.TestUtils;
@@ -27,6 +30,8 @@ import org.eclipse.aether.repository.RemoteRepository;
 import org.eclipse.aether.repository.RepositoryPolicy;
 import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
 import org.eclipse.aether.transfer.TransferResource;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -88,7 +93,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 +106,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 ) );
@@ -133,10 +138,12 @@ public class DefaultChecksumPolicyProviderTest
                 { RepositoryPolicy.CHECKSUM_POLICY_IGNORE, RepositoryPolicy.CHECKSUM_POLICY_IGNORE } };
         for ( String[] testCase : testCases )
         {
-            assertEquals( "unknown vs " + testCase[1], testCase[0],
-                          provider.getEffectiveChecksumPolicy( session, CHECKSUM_POLICY_UNKNOWN, testCase[1] ) );
-            assertEquals( "unknown vs " + testCase[1], testCase[0],
-                          provider.getEffectiveChecksumPolicy( session, testCase[1], CHECKSUM_POLICY_UNKNOWN ) );
+            IllegalArgumentException e = assertThrows( IllegalArgumentException.class,
+                    () -> provider.getEffectiveChecksumPolicy( session, CHECKSUM_POLICY_UNKNOWN, testCase[1] ) );
+            assertThat( e.getMessage(), is("Unsupported policy: unknown") );
+            e = assertThrows( IllegalArgumentException.class,
+                    () -> provider.getEffectiveChecksumPolicy( session, testCase[1], CHECKSUM_POLICY_UNKNOWN ) );
+            assertThat( e.getMessage(), is("Unsupported policy: unknown") );
         }
     }