-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #899: Corrected the invocation of Resolver to retrieve the t…
…imestamped snapshot version; added unit tests for LockSnapshotsMojo for the case when no timestamped versions are found
- Loading branch information
Showing
6 changed files
with
250 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
versions-maven-plugin/src/it/it-lock-snapshots-junit5/invoker.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:lock-snapshots |
20 changes: 20 additions & 0 deletions
20
versions-maven-plugin/src/it/it-lock-snapshots-junit5/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>localhost</groupId> | ||
<artifactId>it-lock-snapshots-001</artifactId> | ||
<version>1.0</version> | ||
<packaging>pom</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<!-- Can't get mrm-maven-plugin to provide maven-metadata --> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>5.0-SNAPSHOT</version> | ||
<type>pom</type> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
3 changes: 3 additions & 0 deletions
3
versions-maven-plugin/src/it/it-lock-snapshots-junit5/verify.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) ) | ||
|
||
assert !( project.dependencies.dependency.version =~ /-SNAPSHOT/ ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/LockSnapshotsMojoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package org.codehaus.mojo.versions; | ||
/* | ||
* Copyright MojoHaus and Contributors | ||
* | ||
* Licensed 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 javax.xml.stream.XMLStreamException; | ||
|
||
import java.util.List; | ||
import java.util.function.UnaryOperator; | ||
|
||
import org.apache.maven.artifact.DefaultArtifact; | ||
import org.apache.maven.model.Dependency; | ||
import org.apache.maven.model.Model; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; | ||
import org.apache.maven.project.MavenProject; | ||
import org.codehaus.mojo.versions.api.PomHelper; | ||
import org.codehaus.mojo.versions.utils.DependencyBuilder; | ||
import org.codehaus.mojo.versions.utils.MockUtils; | ||
import org.eclipse.aether.RepositorySystem; | ||
import org.eclipse.aether.resolution.VersionRequest; | ||
import org.eclipse.aether.resolution.VersionResolutionException; | ||
import org.eclipse.aether.resolution.VersionResult; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Unit tests for {@link LockSnapshotsMojo} | ||
*/ | ||
public class LockSnapshotsMojoTest { | ||
|
||
private LockSnapshotsMojo createMojo(RepositorySystem repositorySystem) { | ||
return new LockSnapshotsMojo(null, repositorySystem, null, null) { | ||
{ | ||
reactorProjects = emptyList(); | ||
project = new MavenProject(new Model() { | ||
{ | ||
setGroupId("default-group"); | ||
setArtifactId("default-project"); | ||
setVersion("1.0-SNAPSHOT"); | ||
} | ||
|
||
@Override | ||
public void setDependencies(List<Dependency> dependencies) { | ||
super.setDependencies(singletonList( | ||
DependencyBuilder.dependencyWith("default-group", "default-artifact", "1.0-SNAPSHOT"))); | ||
} | ||
}); | ||
session = MockUtils.mockMavenSession(); | ||
} | ||
}; | ||
} | ||
|
||
private RepositorySystem mockRepositorySystem(UnaryOperator<String> versionProducer) | ||
throws VersionResolutionException { | ||
RepositorySystem repositorySystem = mock(RepositorySystem.class); | ||
when(repositorySystem.resolveVersion(any(), any())).then(i -> { | ||
VersionRequest request = i.getArgument(1); | ||
return new VersionResult(request) | ||
.setVersion(versionProducer.apply(request.getArtifact().getVersion())); | ||
}); | ||
return repositorySystem; | ||
} | ||
|
||
@Test | ||
public void testNoTimestampedDependencyFoundNull() | ||
throws XMLStreamException, MojoExecutionException, VersionResolutionException { | ||
RepositorySystem repositorySystem = mockRepositorySystem(v -> null); | ||
|
||
LockSnapshotsMojo mojo = createMojo(repositorySystem); | ||
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) { | ||
pomHelper | ||
.when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any())) | ||
.thenThrow(new RuntimeException("Not supposed to modify the dependency")); | ||
mojo.lockSnapshots(null, mojo.project.getDependencies()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNoTimestampedDependencyFoundSameVersion() | ||
throws XMLStreamException, MojoExecutionException, VersionResolutionException { | ||
RepositorySystem repositorySystem = mockRepositorySystem(UnaryOperator.identity()); | ||
|
||
LockSnapshotsMojo mojo = createMojo(repositorySystem); | ||
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) { | ||
pomHelper | ||
.when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any())) | ||
.thenThrow(new RuntimeException("Not supposed to modify the dependency")); | ||
mojo.lockSnapshots(null, mojo.project.getDependencies()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNoTimestampedParentFoundNull() | ||
throws XMLStreamException, MojoExecutionException, VersionResolutionException { | ||
RepositorySystem repositorySystem = mockRepositorySystem(v -> null); | ||
|
||
LockSnapshotsMojo mojo = createMojo(repositorySystem); | ||
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) { | ||
pomHelper | ||
.when(() -> PomHelper.setProjectParentVersion(any(), any())) | ||
.thenThrow(new RuntimeException("Not supposed to modify the parent")); | ||
mojo.lockParentSnapshot( | ||
null, | ||
new MavenProject(new Model() { | ||
{ | ||
setGroupId("default-group"); | ||
setArtifactId("default-parent"); | ||
setVersion("1.0-SNAPSHOT"); | ||
} | ||
}) { | ||
{ | ||
setArtifact(new DefaultArtifact( | ||
"default-group", | ||
"default-parent", | ||
"1.0-SNAPSHOT", | ||
"compile", | ||
"pom", | ||
null, | ||
new DefaultArtifactHandlerStub("jar"))); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNoTimestampedParentFoundSameVersion() | ||
throws XMLStreamException, MojoExecutionException, VersionResolutionException { | ||
RepositorySystem repositorySystem = mockRepositorySystem(UnaryOperator.identity()); | ||
|
||
LockSnapshotsMojo mojo = createMojo(repositorySystem); | ||
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) { | ||
pomHelper | ||
.when(() -> PomHelper.setProjectParentVersion(any(), any())) | ||
.thenThrow(new RuntimeException("Not supposed to modify the parent")); | ||
mojo.lockParentSnapshot( | ||
null, | ||
new MavenProject(new Model() { | ||
{ | ||
setGroupId("default-group"); | ||
setArtifactId("default-parent"); | ||
setVersion("1.0-SNAPSHOT"); | ||
} | ||
}) { | ||
{ | ||
setArtifact(new DefaultArtifact( | ||
"default-group", | ||
"default-parent", | ||
"1.0-SNAPSHOT", | ||
"compile", | ||
"pom", | ||
null, | ||
new DefaultArtifactHandlerStub("jar"))); | ||
} | ||
}); | ||
} | ||
} | ||
} |