[MSHARED-677] - Add null checks for ArtifactResolver interface.
diff --git a/.gitignore b/.gitignore
index f79c928..d0d7222 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,3 +13,4 @@
/bootstrap
/dependencies.xml
.java-version
+dependency-reduced-pom.xml
diff --git a/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java b/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java
index 1e6999f..65fb591 100644
--- a/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java
+++ b/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java
@@ -34,46 +34,51 @@
* @param mavenArtifact {@link Artifact}
* @return {@link ArtifactResult}
* @throws ArtifactResolverException in case of an error.
+ * @throws IllegalArgumentException in case of parameter <code>buildingRequest</code> is <code>null</code> or
+ * parameter <code>mavenArtifact</code> is <code>null</code>.
*/
ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
- throws ArtifactResolverException;
+ throws ArtifactResolverException, IllegalArgumentException;
/**
* @param buildingRequest {@link ProjectBuildingRequest}
* @param coordinate {@link ArtifactCoordinate}
* @return {@link ArtifactResult}
* @throws ArtifactResolverException in case of an error.
+ * @throws IllegalArgumentException in case of parameter <code>buildingRequest</code> is <code>null</code> or
+ * parameter <code>coordinate</code> is <code>null</code>.
*/
ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
- throws ArtifactResolverException;
+ throws ArtifactResolverException, IllegalArgumentException;
-// /**
-// * This will resolve the dependencies of the coordinate, not resolving the the artifact of the coordinate itself.
-// * If the coordinate needs to be resolved too, use
-// * {@link #resolveDependencies(ProjectBuildingRequest, Collection, Collection, TransformableFilter)} passing
-// * {@code Collections.singletonList(coordinate)}
-// *
-// * @param buildingRequest {@link ProjectBuildingRequest}
-// * @param coordinate {@link ArtifactCoordinate}
-// * @param filter {@link TransformableFilter}
-// * @return the resolved dependencies.
-// * @throws ArtifactResolverException in case of an error.
-// */
-// Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate,
-// TransformableFilter filter )
-// throws ArtifactResolverException;
-//
-// /**
-// * @param buildingRequest the project building request, never {@code null}
-// * @param dependencies the dependencies to resolve, never {@code null}
-// * @param managedDependencies managed dependencies, can be {@code null}
-// * @param filter a filter, can be {@code null}
-// * @return the resolved dependencies.
-// * @throws ArtifactResolverException in case of an error.
-// */
-// Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
-// Collection<Dependency> dependencies,
-// Collection<Dependency> managedDependencies,
-// TransformableFilter filter )
-// throws ArtifactResolverException;
+ // /**
+ // * This will resolve the dependencies of the coordinate, not resolving the the artifact of the coordinate itself.
+ // * If the coordinate needs to be resolved too, use
+ // * {@link #resolveDependencies(ProjectBuildingRequest, Collection, Collection, TransformableFilter)} passing
+ // * {@code Collections.singletonList(coordinate)}
+ // *
+ // * @param buildingRequest {@link ProjectBuildingRequest}
+ // * @param coordinate {@link ArtifactCoordinate}
+ // * @param filter {@link TransformableFilter}
+ // * @return the resolved dependencies.
+ // * @throws ArtifactResolverException in case of an error.
+ // */
+ // Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, ArtifactCoordinate
+ // coordinate,
+ // TransformableFilter filter )
+ // throws ArtifactResolverException;
+ //
+ // /**
+ // * @param buildingRequest the project building request, never {@code null}
+ // * @param dependencies the dependencies to resolve, never {@code null}
+ // * @param managedDependencies managed dependencies, can be {@code null}
+ // * @param filter a filter, can be {@code null}
+ // * @return the resolved dependencies.
+ // * @throws ArtifactResolverException in case of an error.
+ // */
+ // Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
+ // Collection<Dependency> dependencies,
+ // Collection<Dependency> managedDependencies,
+ // TransformableFilter filter )
+ // throws ArtifactResolverException;
}
diff --git a/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java b/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java
index be10050..50a84f5 100644
--- a/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java
+++ b/src/main/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolver.java
@@ -44,8 +44,9 @@
@Override
public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
- throws ArtifactResolverException
+ throws ArtifactResolverException, IllegalArgumentException
{
+ validateParameters( buildingRequest, mavenArtifact );
try
{
String hint = isMaven31() ? "maven31" : "maven3";
@@ -62,8 +63,9 @@
@Override
public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
- throws ArtifactResolverException
+ throws ArtifactResolverException, IllegalArgumentException
{
+ validateParameters( buildingRequest, coordinate );
try
{
String hint = isMaven31() ? "maven31" : "maven3";
@@ -78,45 +80,69 @@
}
}
-// @Override
-// public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
-// Collection<Dependency> coordinates,
-// Collection<Dependency> managedDependencies,
-// TransformableFilter filter )
-// throws ArtifactResolverException
-// {
-// try
-// {
-// String hint = isMaven31() ? "maven31" : "maven3";
-//
-// ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
-//
-// return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinates, null, filter );
-// }
-// catch ( ComponentLookupException e )
-// {
-// throw new ArtifactResolverException( e.getMessage(), e );
-// }
-// }
-//
-// @Override
-// public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
-// ArtifactCoordinate coordinate, TransformableFilter filter )
-// throws ArtifactResolverException
-// {
-// try
-// {
-// String hint = isMaven31() ? "maven31" : "maven3";
-//
-// ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
-//
-// return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter );
-// }
-// catch ( ComponentLookupException e )
-// {
-// throw new ArtifactResolverException( e.getMessage(), e );
-// }
-// }
+ private void validateParameters( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
+ {
+ if ( buildingRequest == null )
+ {
+ throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
+ }
+ if ( mavenArtifact == null )
+ {
+ throw new IllegalArgumentException( "The parameter mavenArtifact is not allowed to be null." );
+ }
+ }
+
+ private void validateParameters( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
+ {
+ if ( buildingRequest == null )
+ {
+ throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
+ }
+ if ( coordinate == null )
+ {
+ throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
+ }
+ }
+
+ // @Override
+ // public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
+ // Collection<Dependency> coordinates,
+ // Collection<Dependency> managedDependencies,
+ // TransformableFilter filter )
+ // throws ArtifactResolverException
+ // {
+ // try
+ // {
+ // String hint = isMaven31() ? "maven31" : "maven3";
+ //
+ // ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
+ //
+ // return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinates, null, filter );
+ // }
+ // catch ( ComponentLookupException e )
+ // {
+ // throw new ArtifactResolverException( e.getMessage(), e );
+ // }
+ // }
+ //
+ // @Override
+ // public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
+ // ArtifactCoordinate coordinate, TransformableFilter filter )
+ // throws ArtifactResolverException
+ // {
+ // try
+ // {
+ // String hint = isMaven31() ? "maven31" : "maven3";
+ //
+ // ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
+ //
+ // return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter );
+ // }
+ // catch ( ComponentLookupException e )
+ // {
+ // throw new ArtifactResolverException( e.getMessage(), e );
+ // }
+ // }
/**
* @return true if the current Maven version is Maven 3.1.
diff --git a/src/test/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolverTest.java b/src/test/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolverTest.java
new file mode 100644
index 0000000..094ee71
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/artifact/resolve/internal/DefaultArtifactResolverTest.java
@@ -0,0 +1,98 @@
+package org.apache.maven.shared.artifact.resolve.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.artifact.Artifact;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.ArtifactCoordinate;
+import org.apache.maven.shared.artifact.deploy.ArtifactDeployerException;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
+import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
+import org.junit.Before;
+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 ArtifactResolver}.
+ *
+ * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmabaise@apache.org</a>
+ */
+public class DefaultArtifactResolverTest
+{
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private ArtifactResolver dap;
+
+ @Before
+ public void setUp()
+ {
+ dap = new DefaultArtifactResolver();
+ }
+
+ @Test
+ public void resolveArtifactWithArtifactShouldFaileWithIAEWhenParameterBuildingRequestIsNull()
+ throws ArtifactDeployerException, ArtifactResolverException
+ {
+ thrown.expect( IllegalArgumentException.class );
+ thrown.expectMessage( "The parameter buildingRequest is not allowed to be null." );
+
+ dap.resolveArtifact( null, (Artifact) null );
+ }
+
+ @Test
+ public void resolveArtifactWithArtifactShouldFaileWithIAEWhenArtifactIsNull()
+ throws ArtifactDeployerException, ArtifactResolverException
+ {
+ thrown.expect( IllegalArgumentException.class );
+ thrown.expectMessage( "The parameter mavenArtifact is not allowed to be null." );
+
+ ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
+
+ dap.resolveArtifact( pbr, (Artifact) null );
+ }
+
+ @Test
+ public void resolveArtifactWithCoordinateShouldFaileWithIAEWhenParameterBuildingRequestIsNull()
+ throws ArtifactDeployerException, ArtifactResolverException
+ {
+ thrown.expect( IllegalArgumentException.class );
+ thrown.expectMessage( "The parameter buildingRequest is not allowed to be null." );
+
+ dap.resolveArtifact( null, (ArtifactCoordinate) null );
+ }
+
+ @Test
+ public void resolveArtifactWithCoordinateShouldFaileWithIAEWhenArtifactIsNull()
+ throws ArtifactDeployerException, ArtifactResolverException
+ {
+ thrown.expect( IllegalArgumentException.class );
+ thrown.expectMessage( "The parameter coordinate is not allowed to be null." );
+
+ ProjectBuildingRequest pbr = mock( ProjectBuildingRequest.class );
+
+ dap.resolveArtifact( pbr, (ArtifactCoordinate) null );
+ }
+
+}