Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #890: Fixing ArrayIndexOutOfBound from UseNextVersionsMojo and UseNextReleasesMojo in case of no newer versions/releases #891

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
Expand All @@ -41,7 +41,6 @@
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
import static java.util.Optional.of;

/**
* Replaces any release versions with the next release version (if it has been released).
Expand All @@ -52,13 +51,6 @@
@Mojo(name = "use-next-releases", threadSafe = true)
public class UseNextReleasesMojo extends UseLatestVersionsMojoBase {

// ------------------------------ FIELDS ------------------------------

/**
* Pattern to match a snapshot version.
*/
private static final Pattern MATCH_SNAPSHOT_REGEX = Pattern.compile("^(.+)-((SNAPSHOT)|(\\d{8}\\.\\d{6}-\\d+))$");

// ------------------------------ METHODS --------------------------

@Inject
Expand Down Expand Up @@ -107,7 +99,8 @@ private void useNextReleases(
useLatestVersions(
pom,
dependencies,
(dep, versions) -> of(versions.getNewerVersions(dep.getVersion(), false)[0]),
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), false))
.findFirst(),
changeKind,
dep -> !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

Expand All @@ -40,7 +41,6 @@
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
import static java.util.Optional.of;

/**
* Replaces any version with the latest version.
Expand Down Expand Up @@ -97,7 +97,8 @@ private void useNextVersions(
useLatestVersions(
pom,
dependencies,
(dep, versions) -> of(versions.getNewerVersions(dep.getVersion(), allowSnapshots)[0]),
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), allowSnapshots))
.findFirst(),
changeKind);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

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;
Expand All @@ -27,32 +24,18 @@
import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject;
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

public class UseLatestReleasesMojoTest {
private UseLatestReleasesMojo 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"));
});

RepositorySystem repositorySystemMock = mockRepositorySystem();
org.eclipse.aether.RepositorySystem aetherRepositorySystem =
mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import java.util.HashMap;

import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
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;
Expand All @@ -31,37 +29,22 @@
import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject;
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@SuppressWarnings("deprecation")
public class UseLatestVersionsMojoTest {
private UseLatestVersionsMojo 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"));
});

RepositorySystem repositorySystemMock = mockRepositorySystem();
org.eclipse.aether.RepositorySystem aetherRepositorySystem =
mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.codehaus.mojo.versions;
/*
* 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 java.util.Collections;
import java.util.HashMap;

import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;

import static java.util.Collections.emptyList;
import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject;
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;

/**
* Unit tests for {@link UseNextReleasesMojo}
*/
public class UseNextReleasesMojoTest {

private UseNextReleasesMojo mojo;
private TestChangeRecorder changeRecorder;

@Before
public void setUp() throws Exception {
RepositorySystem repositorySystemMock = mockRepositorySystem();
org.eclipse.aether.RepositorySystem aetherRepositorySystem =
mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("dependency-artifact", new String[] {"1.0.0", "1.1.0", "1.1.1-SNAPSHOT"});
}
});
changeRecorder = new TestChangeRecorder();
mojo = new UseNextReleasesMojo(repositorySystemMock, aetherRepositorySystem, null, changeRecorder.asTestMap()) {
{
reactorProjects = emptyList();
session = mockMavenSession();
project = new MavenProject() {
{
setModel(new Model() {
{
setGroupId("default-group");
setArtifactId("project-artifact");
setVersion("1.0.0-SNAPSHOT");
}
});
setDependencies(Collections.singletonList(DependencyBuilder.dependencyWith(
"default-group", "dependency-artifact", "1.1.0", "default", "pom", SCOPE_COMPILE)));
}
};
}
};
setVariableValueToObject(mojo, "processDependencyManagement", false);
}

@Test
public void testNoNewerReleases() {
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any()))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
assertThat(changeRecorder.getChanges(), Matchers.empty());
}

@Test
public void testFindANewerRelease() throws IllegalAccessException {
setVariableValueToObject(
mojo, "aetherRepositorySystem", mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("dependency-artifact", new String[] {"1.0.0", "1.1.0", "1.1.1", "2.0.0"});
}
}));
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any()))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0", "1.1.1")));
}
}
Loading