Skip to content

Commit

Permalink
Resolves mojohaus#899: Corrected the invocation of Resolver to retrie…
Browse files Browse the repository at this point in the history
…ve the timestamped snapshot version; added unit tests for LockSnapshotsMojo for the case when no timestamped versions are found
  • Loading branch information
jarmoniuk committed Jan 8, 2023
1 parent da7fe18 commit 8fc252b
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.codehaus.mojo.versions.api.change;

/*
* 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
* 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
* 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.
* 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.
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
*
* @author Slawomir Jaranowski
* @since 2.14.0
* @param <T> concrete {@link VersionChange} sub-interface
*/
public interface ChangeRecord {
public interface ChangeRecord<T extends VersionChange> {
/**
* @return a details about changed item
* @since 2.14.0
*/
VersionChange getVersionChange();
T getVersionChange();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.IOException;
import java.nio.file.Path;

import org.codehaus.mojo.versions.api.change.VersionChange;

/**
* Interface for implement a recorder of version changes.
*
Expand All @@ -32,8 +34,9 @@ public interface ChangeRecorder {
*
* @param changeRecord a record described change
* @since 2.14.0
* @param <T> concrete {@link VersionChange} sub-interface
*/
void recordChange(ChangeRecord changeRecord);
<T extends VersionChange> void recordChange(ChangeRecord<T> changeRecord);

/**
* Write the current set of changes to the given output path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
*
*/

import org.codehaus.mojo.versions.api.change.DependencyVersionChange;

/**
* Represents a change record of an item's version.
*
* @author Slawomir Jaranowski
* @since 2.14.0
*/
public interface DependencyChangeRecord extends ChangeRecord {
public interface DependencyChangeRecord extends ChangeRecord<DependencyVersionChange> {
/**
* Describe where version item is updated.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.codehaus.mojo.versions.api.recording;

/*
* 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 org.codehaus.mojo.versions.api.change.PropertyVersionChange;

/**
* Represents a change record of an item's version.
*
* @author Slawomir Jaranowski
* @since 2.14.0
*/
public interface PropertyChangeRecord extends ChangeRecord<PropertyVersionChange> {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.versions.api.change.DependencyVersionChange;
import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord;
import org.codehaus.mojo.versions.change.DefaultDependencyVersionChange;

Expand Down Expand Up @@ -79,7 +78,7 @@ public Builder withArtifact(Artifact artifact) {
return this;
}

public ChangeRecord build() {
public DependencyChangeRecord build() {
return new DefaultDependencyChangeRecord(
kind, new DefaultDependencyVersionChange(groupId, artifactId, oldVersion, newVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Objects;

import org.codehaus.mojo.versions.api.change.PropertyVersionChange;
import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.PropertyChangeRecord;
import org.codehaus.mojo.versions.change.DefaultPropertyVersionChange;

/*
Expand All @@ -25,7 +25,7 @@
* under the License.
*/

public class DefaultPropertyChangeRecord implements ChangeRecord {
public class DefaultPropertyChangeRecord implements PropertyChangeRecord {
private final PropertyVersionChange versionChange;

private DefaultPropertyChangeRecord(PropertyVersionChange versionChange) {
Expand Down Expand Up @@ -61,7 +61,7 @@ public Builder withNewValue(String newValue) {
return this;
}

public ChangeRecord build() {
public PropertyChangeRecord build() {
return new DefaultPropertyChangeRecord(new DefaultPropertyVersionChange(property, oldValue, newValue));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
import static org.codehaus.mojo.versions.api.recording.DependencyChangeRecord.ChangeKind.DEPENDENCY;
import static org.codehaus.mojo.versions.api.recording.DependencyChangeRecord.ChangeKind.DEPENDENCY_MANAGEMENT;
import static org.codehaus.mojo.versions.api.recording.DependencyChangeRecord.ChangeKind.PARENT;

/**
* Replaces any release versions with the next release version (if it has been released).
Expand Down Expand Up @@ -87,25 +90,26 @@ protected void update(ModifiedPomXMLEventReader pom)
DependencyManagement dependencyManagement =
PomHelper.getRawModel(getProject()).getDependencyManagement();
if (dependencyManagement != null) {
useNextReleases(
pom, dependencyManagement.getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY_MANAGEMENT);
useNextReleases(pom, dependencyManagement.getDependencies(), DEPENDENCY_MANAGEMENT);
}
}

if (getProject().getDependencies() != null && isProcessingDependencies()) {
useNextReleases(pom, getProject().getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY);
useNextReleases(pom, getProject().getDependencies(), DEPENDENCY);
}

if (getProject().getParent() != null && isProcessingParent()) {
useNextReleases(pom, singletonList(getParentDependency()), ChangeRecord.ChangeKind.PARENT);
useNextReleases(pom, singletonList(getParentDependency()), PARENT);
}
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

private void useNextReleases(
ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies, ChangeRecord.ChangeKind changeKind)
ModifiedPomXMLEventReader pom,
Collection<Dependency> dependencies,
DependencyChangeRecord.ChangeKind changeKind)
throws XMLStreamException, MojoExecutionException, VersionRetrievalException {
useLatestVersions(
pom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
import static org.codehaus.mojo.versions.api.recording.DependencyChangeRecord.ChangeKind.DEPENDENCY;
import static org.codehaus.mojo.versions.api.recording.DependencyChangeRecord.ChangeKind.DEPENDENCY_MANAGEMENT;
import static org.codehaus.mojo.versions.api.recording.DependencyChangeRecord.ChangeKind.PARENT;

/**
* Replaces any version with the latest version.
Expand Down Expand Up @@ -89,23 +92,24 @@ protected void update(ModifiedPomXMLEventReader pom)
DependencyManagement dependencyManagement =
PomHelper.getRawModel(getProject()).getDependencyManagement();
if (dependencyManagement != null) {
useNextVersions(
pom, dependencyManagement.getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY_MANAGEMENT);
useNextVersions(pom, dependencyManagement.getDependencies(), DEPENDENCY_MANAGEMENT);
}
}
if (getProject().getDependencies() != null && isProcessingDependencies()) {
useNextVersions(pom, getProject().getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY);
useNextVersions(pom, getProject().getDependencies(), DEPENDENCY);
}
if (getProject().getParent() != null && isProcessingParent()) {
useNextVersions(pom, singletonList(getParentDependency()), ChangeRecord.ChangeKind.PARENT);
useNextVersions(pom, singletonList(getParentDependency()), PARENT);
}
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

private void useNextVersions(
ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies, ChangeRecord.ChangeKind changeKind)
ModifiedPomXMLEventReader pom,
Collection<Dependency> dependencies,
DependencyChangeRecord.ChangeKind changeKind)
throws XMLStreamException, MojoExecutionException, VersionRetrievalException {
useLatestVersions(
pom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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.change.DefaultDependencyVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -131,7 +131,7 @@ public void testAllowDowngrade()
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
hasItem(new DefaultDependencyVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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.change.DefaultDependencyVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void testFindANewerRelease() throws IllegalAccessException {
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0", "1.1.1")));
hasItem(new DefaultDependencyVersionChange("default-group", "dependency-artifact", "1.1.0", "1.1.1")));
}

@Test
Expand Down Expand Up @@ -160,7 +160,7 @@ public void testAllowDowngrade()
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
hasItem(new DefaultDependencyVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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.change.DefaultDependencyVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -135,7 +135,8 @@ public void testFindANewerVersion() throws IllegalAccessException {
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0-SNAPSHOT", "1.1.1")));
hasItem(new DefaultDependencyVersionChange(
"default-group", "dependency-artifact", "1.1.0-SNAPSHOT", "1.1.1")));
}

@Test
Expand Down Expand Up @@ -166,7 +167,7 @@ public void testAllowDowngrade()
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
hasItem(new DefaultDependencyVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}
Expand Down

0 comments on commit 8fc252b

Please sign in to comment.