You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2018/01/28 12:03:39 UTC

[maven-artifact-transfer] branch MSHARED-656 updated: Reformatted code Added null checks for parameters in ProjectDeployer and ProjectInstaller including tests to check this.

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

khmarbaise pushed a commit to branch MSHARED-656
in repository https://gitbox.apache.org/repos/asf/maven-artifact-transfer.git


The following commit(s) were added to refs/heads/MSHARED-656 by this push:
     new 019a3c8  Reformatted code Added null checks for parameters in ProjectDeployer and ProjectInstaller including tests to check this.
019a3c8 is described below

commit 019a3c8a8d632ed2ec4c00489005e85ea3d8a446
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Sun Jan 28 13:02:07 2018 +0100

    Reformatted code
    Added null checks for parameters in ProjectDeployer and ProjectInstaller
    including tests to check this.
---
 .../artifact/deploy/ArtifactDeployerException.java |  1 +
 .../shared/project/deploy/ProjectDeployer.java     | 13 ++-
 .../deploy/internal/DefaultProjectDeployer.java    | 94 ++++++++++++----------
 .../shared/project/install/ProjectInstaller.java   | 16 ++--
 .../install/internal/DefaultProjectInstaller.java  | 15 +++-
 .../DefaultDependencyCoordinateTest.java           |  1 +
 .../internal/DefaultProjectDeployerTest.java       | 82 +++++++++++++++++++
 .../internal/DefaultProjectInstallerTest.java      | 69 ++++++++++++++++
 8 files changed, 231 insertions(+), 60 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/artifact/deploy/ArtifactDeployerException.java b/src/main/java/org/apache/maven/shared/artifact/deploy/ArtifactDeployerException.java
index b0dac8d..a66f6f0 100644
--- a/src/main/java/org/apache/maven/shared/artifact/deploy/ArtifactDeployerException.java
+++ b/src/main/java/org/apache/maven/shared/artifact/deploy/ArtifactDeployerException.java
@@ -20,6 +20,7 @@ package org.apache.maven.shared.artifact.deploy;
  */
 
 /**
+ * An artifact could not correctly being deployed.
  * 
  */
 public class ArtifactDeployerException
diff --git a/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployer.java b/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployer.java
index ce99e78..98e131d 100644
--- a/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployer.java
+++ b/src/main/java/org/apache/maven/shared/project/deploy/ProjectDeployer.java
@@ -21,6 +21,7 @@ package org.apache.maven.shared.project.deploy;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.deploy.ArtifactDeployerException;
 import org.apache.maven.shared.project.NoFileAssignedException;
 
 /**
@@ -55,14 +56,12 @@ public interface ProjectDeployer
      * @param request {@link ProjectDeployerRequest}
      * @param artifactRepository {@link ArtifactRepository}
      * @throws NoFileAssignedException In case of missing file which has not been assigned to project.
-     * @throws IllegalArgumentException in case of artifact is not correctly assigned.
+     * @throws ArtifactDeployerException in case of artifact could not correctly deployed.
+     * @throws IllegalArgumentException in case <code>buildingRequest</code> is <code>null</code>, <code>request</code>
+     *             is <code>null</code> or <code>artifactRepository</code> is <code>null</code>.
      */
-    // TODO: Think about null check for projectBuildingRequest?
-    // TODO: Think about null check for projectDeployerRequest?
-    // TODO: Think about null check for artifactRepository?
     void deploy( ProjectBuildingRequest buildingRequest, ProjectDeployerRequest request,
-                        ArtifactRepository artifactRepository )
-        throws NoFileAssignedException, IllegalArgumentException;
-
+                 ArtifactRepository artifactRepository )
+        throws NoFileAssignedException, IllegalArgumentException, ArtifactDeployerException;
 
 }
diff --git a/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java b/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java
index c15ef4f..b4d88ff 100644
--- a/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java
+++ b/src/main/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployer.java
@@ -54,24 +54,19 @@ public class DefaultProjectDeployer
     private ArtifactDeployer deployer;
 
     /**
-     * This will deploy a single project which may contain several artifacts into the appropriate remote repository.
-     * 
-     * @param buildingRequest {@link ProjectBuildingRequest}
-     * @param request {@link ProjectDeployerRequest}
-     * @param artifactRepository {@link ArtifactRepository}
-     * @throws IllegalArgumentException in case of artifact is not correctly assigned.
-     * @throws NoFileAssignedException In case no file has been assigned to main file.
+     * {@inheritDoc}
      */
-    public void deploy( ProjectBuildingRequest buildingRequest, ProjectDeployerRequest request,
+    public void deploy( ProjectBuildingRequest buildingRequest, ProjectDeployerRequest projectDeployerRequest,
                         ArtifactRepository artifactRepository )
-        throws NoFileAssignedException, IllegalArgumentException
+        throws NoFileAssignedException, IllegalArgumentException, ArtifactDeployerException
     {
+        validateParameters( buildingRequest, projectDeployerRequest, artifactRepository );
 
-        Artifact artifact = request.getProject().getArtifact();
-        String packaging = request.getProject().getPackaging();
-        File pomFile = request.getProject().getFile();
+        Artifact artifact = projectDeployerRequest.getProject().getArtifact();
+        String packaging = projectDeployerRequest.getProject().getPackaging();
+        File pomFile = projectDeployerRequest.getProject().getFile();
 
-        List<Artifact> attachedArtifacts = request.getProject().getAttachedArtifacts();
+        List<Artifact> attachedArtifacts = projectDeployerRequest.getProject().getAttachedArtifacts();
 
         // Deploy the POM
         boolean isPomArtifact = "pom".equals( packaging );
@@ -85,57 +80,68 @@ public class DefaultProjectDeployer
             artifact.addMetadata( metadata );
         }
 
-        //FIXME: It does not make sense to set an artifact explicitly to a "Release"
+        // FIXME: It does not make sense to set an artifact explicitly to a "Release"
         // cause this should be choosen only by the not existing of "-SNAPSHOT" in the
         // version.
-        if ( request.isUpdateReleaseInfo() )
+        if ( projectDeployerRequest.isUpdateReleaseInfo() )
         {
             artifact.setRelease( true );
         }
 
         artifact.setRepository( artifactRepository );
 
-        int retryFailedDeploymentCount = request.getRetryFailedDeploymentCount();
+        int retryFailedDeploymentCount = projectDeployerRequest.getRetryFailedDeploymentCount();
 
-        try
+        List<Artifact> deployableArtifacts = new ArrayList<Artifact>();
+        if ( isPomArtifact )
+        {
+            deployableArtifacts.add( artifact );
+        }
+        else
         {
-            List<Artifact> deployableArtifacts = new ArrayList<Artifact>();
-            if ( isPomArtifact )
+            File file = artifact.getFile();
+
+            if ( file != null && file.isFile() )
             {
                 deployableArtifacts.add( artifact );
             }
-            else
+            else if ( !attachedArtifacts.isEmpty() )
             {
-                File file = artifact.getFile();
-
-                if ( file != null && file.isFile() )
-                {
-                    deployableArtifacts.add( artifact );
-                }
-                else if ( !attachedArtifacts.isEmpty() )
-                {
-                    // TODO: Reconsider this exception? Better Exception type?
-                    throw new NoFileAssignedException( "The packaging plugin for this project did not assign "
-                        + "a main file to the project but it has attachments. Change packaging to 'pom'." );
-                }
-                else
-                {
-                    // TODO: Reconsider this exception? Better Exception type?
-                    throw new NoFileAssignedException( "The packaging for this project did not assign "
-                        + "a file to the build artifact" );
-                }
+                // TODO: Reconsider this exception? Better Exception type?
+                throw new NoFileAssignedException( "The packaging plugin for this project did not assign "
+                    + "a main file to the project but it has attachments. Change packaging to 'pom'." );
             }
-
-            for ( Artifact attached : attachedArtifacts )
+            else
             {
-                deployableArtifacts.add( attached );
+                // TODO: Reconsider this exception? Better Exception type?
+                throw new NoFileAssignedException( "The packaging for this project did not assign "
+                    + "a file to the build artifact" );
             }
+        }
+
+        for ( Artifact attached : attachedArtifacts )
+        {
+            deployableArtifacts.add( attached );
+        }
 
-            deploy( buildingRequest, deployableArtifacts, artifactRepository, retryFailedDeploymentCount );
+        deploy( buildingRequest, deployableArtifacts, artifactRepository, retryFailedDeploymentCount );
+    }
+
+    private void validateParameters( ProjectBuildingRequest buildingRequest,
+                                     ProjectDeployerRequest projectDeployerRequest,
+                                     ArtifactRepository artifactRepository )
+    {
+        if ( buildingRequest == null )
+        {
+            throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
+        }
+        if ( projectDeployerRequest == null )
+        {
+            throw new IllegalArgumentException( "The parameter projectDeployerRequest is not allowed to be null." );
         }
-        catch ( ArtifactDeployerException e )
+        if ( artifactRepository == null )
         {
-            throw new IllegalArgumentException( e.getMessage(), e );
+            throw new IllegalArgumentException( "The parameter artifactRepository is not allowed to be null." );
         }
     }
 
diff --git a/src/main/java/org/apache/maven/shared/project/install/ProjectInstaller.java b/src/main/java/org/apache/maven/shared/project/install/ProjectInstaller.java
index 770aee1..d7ddd40 100644
--- a/src/main/java/org/apache/maven/shared/project/install/ProjectInstaller.java
+++ b/src/main/java/org/apache/maven/shared/project/install/ProjectInstaller.java
@@ -54,15 +54,15 @@ public interface ProjectInstaller
      *  installer.install( session.getProjectBuildingRequest(), pir );
      * </pre>
      * 
-     * To set a different local repository than the current one in the Maven session, you can inject an instance of
-     * the <code>RepositoryManager</code> and set the path to the local repository, called 
-     * <code>localRepositoryPath</code>, as such:
+     * To set a different local repository than the current one in the Maven session, you can inject an instance of the
+     * <code>RepositoryManager</code> and set the path to the local repository, called <code>localRepositoryPath</code>,
+     * as such:
      * 
      * <pre class="java">
-     *  &#64;Component
-     *  private RepositoryManager repositoryManager;
+     * &#64;Component
+     * private RepositoryManager repositoryManager;
      * 
-     *  buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryPath );
+     * buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryPath );
      * </pre>
      * 
      * @param projectBuildingRequest {@link ProjectBuildingRequest}
@@ -70,9 +70,9 @@ public interface ProjectInstaller
      * @throws IOException In case of problems related to checksums.
      * @throws ArtifactInstallerException In case of problems to install artifacts.
      * @throws NoFileAssignedException If no file has been assigned to the project.
+     * @throws IllegalArgumentException in case <code>projectBuildingRequest</code> is <code>null</code>,
+     *             <code>projectInstallerRequest</code> is <code>null</code>.
      */
-    // TODO: Think about null check for projectBuildingRequest?
-    // TODO: Think about null check for projectInstallerRequest?
     void install( ProjectBuildingRequest projectBuildingRequest, ProjectInstallerRequest projectInstallerRequest )
         throws IOException, ArtifactInstallerException, NoFileAssignedException;
 
diff --git a/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java b/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java
index 227e537..c1b23f1 100644
--- a/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java
+++ b/src/main/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstaller.java
@@ -71,7 +71,8 @@ public class DefaultProjectInstaller
     public void install( ProjectBuildingRequest buildingRequest, ProjectInstallerRequest request )
         throws IOException, ArtifactInstallerException, NoFileAssignedException
     {
-
+        validateParameters( buildingRequest, request );
+        
         MavenProject project = request.getProject();
         boolean createChecksum = request.isCreateChecksum();
         boolean updateReleaseInfo = request.isUpdateReleaseInfo();
@@ -145,6 +146,18 @@ public class DefaultProjectInstaller
         installChecksums( metadataFiles );
     }
 
+    private void validateParameters( ProjectBuildingRequest buildingRequest, ProjectInstallerRequest request )
+    {
+        if ( buildingRequest == null )
+        {
+            throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
+        }
+        if ( request == null )
+        {
+            throw new IllegalArgumentException( "The parameter request is not allowed to be null." );
+        }
+    }
+
     /**
      * Installs the checksums for the specified artifact if this has been enabled in the plugin configuration. This
      * method creates checksums for files that have already been installed to the local repo to account for on-the-fly
diff --git a/src/test/java/org/apache/maven/shared/dependency/DefaultDependencyCoordinateTest.java b/src/test/java/org/apache/maven/shared/dependency/DefaultDependencyCoordinateTest.java
index a52d055..f3d00ea 100644
--- a/src/test/java/org/apache/maven/shared/dependency/DefaultDependencyCoordinateTest.java
+++ b/src/test/java/org/apache/maven/shared/dependency/DefaultDependencyCoordinateTest.java
@@ -18,6 +18,7 @@ package org.apache.maven.shared.dependency;
  * specific language governing permissions and limitations
  * under the License.
  */
+
 import static org.junit.Assert.assertEquals;
 
 import org.apache.maven.shared.dependencies.DefaultDependableCoordinate;
diff --git a/src/test/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployerTest.java b/src/test/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployerTest.java
new file mode 100644
index 0000000..2d54a24
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/project/deploy/internal/DefaultProjectDeployerTest.java
@@ -0,0 +1,82 @@
+package org.apache.maven.shared.project.deploy.internal;
+
+/*
+ * 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.
+ */
+
+import static org.mockito.Mockito.mock;
+
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.deploy.ArtifactDeployerException;
+import org.apache.maven.shared.project.NoFileAssignedException;
+import org.apache.maven.shared.project.deploy.ProjectDeployer;
+import org.apache.maven.shared.project.deploy.ProjectDeployerRequest;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * Check the parameter contracts which have been made based on the interface {@link ProjectDeployer}.
+ * 
+ * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmabaise@apache.org</a>
+ */
+public class DefaultProjectDeployerTest
+{
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void deployShouldFailWithIAEWhileBuildingRequestIsNull()
+        throws IllegalArgumentException, NoFileAssignedException, ArtifactDeployerException
+    {
+        ProjectDeployer dpi = new DefaultProjectDeployer();
+
+        expectedException.expect( IllegalArgumentException.class );
+        expectedException.expectMessage( "The parameter buildingRequest is not allowed to be null." );
+
+        dpi.deploy( null, null, null );
+    }
+
+    @Test
+    public void deployShouldFailWithIAEWhileProjectDeployerRequestIsNull()
+        throws IllegalArgumentException, NoFileAssignedException, ArtifactDeployerException
+    {
+        ProjectDeployer dpi = new DefaultProjectDeployer();
+
+        expectedException.expect( IllegalArgumentException.class );
+        expectedException.expectMessage( "The parameter projectDeployerRequest is not allowed to be null." );
+
+        ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
+        dpi.deploy( pbr, null, null );
+    }
+
+    @Test
+    public void deployShouldFailWithIAEWhileArtifactRepositoryIsNull()
+        throws IllegalArgumentException, NoFileAssignedException, ArtifactDeployerException
+    {
+        ProjectDeployer dpi = new DefaultProjectDeployer();
+
+        expectedException.expect( IllegalArgumentException.class );
+        expectedException.expectMessage( "The parameter artifactRepository is not allowed to be null." );
+
+        ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
+        ProjectDeployerRequest pdr = mock( ProjectDeployerRequest.class );
+        dpi.deploy( pbr, pdr, null );
+    }
+
+}
diff --git a/src/test/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstallerTest.java b/src/test/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstallerTest.java
new file mode 100644
index 0000000..a1ce123
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/project/install/internal/DefaultProjectInstallerTest.java
@@ -0,0 +1,69 @@
+package org.apache.maven.shared.project.install.internal;
+/*
+ * 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.
+ */
+
+import static org.mockito.Mockito.mock;
+
+import java.io.IOException;
+
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.install.ArtifactInstallerException;
+import org.apache.maven.shared.project.NoFileAssignedException;
+import org.apache.maven.shared.project.install.ProjectInstaller;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * Check the parameter contracts which have been made based on the interface {@link ProjectInstaller}.
+ * 
+ * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmabaise@apache.org</a>
+ */
+public class DefaultProjectInstallerTest
+{
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void installShouldFailWithIAEWhileBuildingRequestIsNull()
+        throws IOException, ArtifactInstallerException, NoFileAssignedException
+    {
+        ProjectInstaller dpi = new DefaultProjectInstaller();
+
+        expectedException.expect( IllegalArgumentException.class );
+        expectedException.expectMessage( "The parameter buildingRequest is not allowed to be null." );
+
+        dpi.install( null, null );
+    }
+
+    @Test
+    public void installShouldFailWithIAEWhileProjectInstallerRequestIsNull()
+        throws IOException, ArtifactInstallerException, NoFileAssignedException
+    {
+        ProjectInstaller dpi = new DefaultProjectInstaller();
+
+        expectedException.expect( IllegalArgumentException.class );
+        expectedException.expectMessage( "The parameter request is not allowed to be null." );
+        ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
+
+        dpi.install( pbr, null );
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
khmarbaise@apache.org.