forked from mojohaus/versions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves mojohaus#600: Add allowDowngrade capability to UseLatestRele…
…asesMojo, UseNextReleasesMojo, UseNextVersionsMojo
- Loading branch information
Showing
6 changed files
with
435 additions
and
7 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
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
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
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
155 changes: 155 additions & 0 deletions
155
versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextReleasesMojoTest.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,155 @@ | ||
package org.codehaus.mojo.versions; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
|
||
import java.util.HashMap; | ||
|
||
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.MojoFailureException; | ||
import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; | ||
import org.apache.maven.project.MavenProject; | ||
import org.apache.maven.repository.RepositorySystem; | ||
import org.codehaus.mojo.versions.api.PomHelper; | ||
import org.codehaus.mojo.versions.api.VersionRetrievalException; | ||
import org.codehaus.mojo.versions.change.DefaultVersionChange; | ||
import org.codehaus.mojo.versions.utils.DependencyBuilder; | ||
import org.codehaus.mojo.versions.utils.TestChangeRecorder; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; | ||
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; | ||
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Unit test suite for {@link UseNextReleasesMojo} | ||
*/ | ||
public class UseNextReleasesMojoTest { | ||
private UseNextReleasesMojo mojo; | ||
private TestChangeRecorder changeRecorder; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
RepositorySystem repositorySystemMock = mock(RepositorySystem.class); | ||
when(repositorySystemMock.createDependencyArtifact(any(Dependency.class))) | ||
.thenAnswer(invocation -> { | ||
Dependency dependency = invocation.getArgument(0); | ||
return new DefaultArtifact( | ||
dependency.getGroupId(), | ||
dependency.getArtifactId(), | ||
dependency.getVersion(), | ||
dependency.getScope(), | ||
dependency.getType(), | ||
dependency.getClassifier() != null ? dependency.getClassifier() : "default", | ||
new DefaultArtifactHandlerStub("default")); | ||
}); | ||
|
||
org.eclipse.aether.RepositorySystem aetherRepositorySystem = mockAetherRepositorySystem(); | ||
|
||
changeRecorder = new TestChangeRecorder(); | ||
|
||
mojo = new UseNextReleasesMojo(repositorySystemMock, aetherRepositorySystem, null, changeRecorder.asTestMap()) { | ||
{ | ||
reactorProjects = emptyList(); | ||
MavenProject project = new MavenProject() { | ||
{ | ||
setModel(new Model() { | ||
{ | ||
setGroupId("default-group"); | ||
setArtifactId("project-artifact"); | ||
setVersion("1.0.0-SNAPSHOT"); | ||
|
||
setDependencies(singletonList(DependencyBuilder.newBuilder() | ||
.withGroupId("default-group") | ||
.withArtifactId("artifactA") | ||
.withVersion("1.0.0") | ||
.withScope(SCOPE_COMPILE) | ||
.withType("jar") | ||
.withClassifier("default") | ||
.build())); | ||
} | ||
}); | ||
} | ||
}; | ||
setProject(project); | ||
|
||
session = mockMavenSession(); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void testAllowDowngrade() | ||
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { | ||
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() { | ||
{ | ||
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"}); | ||
} | ||
}); | ||
mojo.getProject() | ||
.setDependencies(singletonList(DependencyBuilder.newBuilder() | ||
.withGroupId("default-group") | ||
.withArtifactId("artifactA") | ||
.withVersion("1.0.1-SNAPSHOT") | ||
.build())); | ||
mojo.allowDowngrade = true; | ||
|
||
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) { | ||
pomHelper | ||
.when(() -> PomHelper.setDependencyVersion( | ||
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class))) | ||
.thenReturn(true); | ||
pomHelper | ||
.when(() -> PomHelper.getRawModel(any(MavenProject.class))) | ||
.thenReturn(mojo.getProject().getModel()); | ||
mojo.update(null); | ||
} | ||
assertThat( | ||
changeRecorder.getChanges(), | ||
hasItem(new DefaultVersionChange( | ||
"default-group", "artifactA", | ||
"1.0.1-SNAPSHOT", "1.0.0"))); | ||
} | ||
|
||
@Test | ||
public void testDisallowDowngrade() | ||
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { | ||
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() { | ||
{ | ||
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"}); | ||
} | ||
}); | ||
mojo.getProject() | ||
.setDependencies(singletonList(DependencyBuilder.newBuilder() | ||
.withGroupId("default-group") | ||
.withArtifactId("artifactA") | ||
.withVersion("1.0.1-SNAPSHOT") | ||
.build())); | ||
|
||
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) { | ||
pomHelper | ||
.when(() -> PomHelper.setDependencyVersion( | ||
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class))) | ||
.thenReturn(true); | ||
pomHelper | ||
.when(() -> PomHelper.getRawModel(any(MavenProject.class))) | ||
.thenReturn(mojo.getProject().getModel()); | ||
mojo.update(null); | ||
} | ||
assertThat(changeRecorder.getChanges(), empty()); | ||
} | ||
} |
Oops, something went wrong.