From 02e42c5d0b3ee689c5d0d6bb2819f7526db6a083 Mon Sep 17 00:00:00 2001 From: wuwen Date: Fri, 13 Dec 2024 16:05:50 +0800 Subject: [PATCH] [MRELEASE-1054] Add some unit test cases. --- .../release/phase/MapVersionsPhaseTest.java | 32 +++++++++++-- .../phase/RewritePomsForReleasePhaseTest.java | 16 +++++++ .../phase/ScmCommitPreparationPhaseTest.java | 46 +++++++++++++++++++ .../expected-pom.xml | 37 +++++++++++++++ .../pom.xml | 37 +++++++++++++++ .../subproject1/expected-pom.xml | 30 ++++++++++++ .../subproject1/pom.xml | 30 ++++++++++++ .../subproject2/expected-pom.xml | 36 +++++++++++++++ .../subproject2/pom.xml | 36 +++++++++++++++ 9 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/expected-pom.xml create mode 100644 maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/pom.xml diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java index 8a63cd2a3..fb9928fe3 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/MapVersionsPhaseTest.java @@ -2454,21 +2454,43 @@ public void testSimulateRelease_CheckModificationExcludes() throws Exception { MapReleaseVersionsPhase phase = new MapReleaseVersionsPhase(scmRepositoryConfigurator, mockPrompter, versionPolicies); - List reactorProjects = Collections.singletonList(createProjectWithPomFile("artifactId", "1.2")); + List reactorProjects = new ArrayList<>(); + Collections.addAll( + reactorProjects, + createProject("bar", "1.11-SNAPSHOT"), + createProjectWithPomFile("artifactId", "1.2-SNAPSHOT", "src/test/resources/projects/scm-commit/multiple-poms/pom.xml"), + createProjectWithPomFile("subproject1", "2.0", "src/test/resources/projects/scm-commit/multiple-poms/subproject1/pom.xml")); ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder(); - builder.setCheckModificationExcludes(Collections.singletonList("**/pom1.xml")); + builder.setCheckModificationExcludes(Collections.singletonList("**/subproject1/pom.xml")); + builder.setInteractive(false); // test phase.simulate(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); // verify - assertNull( + assertEquals( "Check release versions", + "1.2", ReleaseUtils.buildReleaseDescriptor(builder).getProjectReleaseVersion("groupId:artifactId")); assertNull( "Check development versions", ReleaseUtils.buildReleaseDescriptor(builder).getProjectDevelopmentVersion("groupId:artifactId")); + + assertEquals( + "Check release versions", + "1.11", + ReleaseUtils.buildReleaseDescriptor(builder).getProjectReleaseVersion("groupId:bar")); + assertNull( + "Check development versions", + ReleaseUtils.buildReleaseDescriptor(builder).getProjectDevelopmentVersion("groupId:bar")); + + assertNull( + "Check release versions", + ReleaseUtils.buildReleaseDescriptor(builder).getProjectReleaseVersion("groupId:subproject1")); + assertNull( + "Check development versions", + ReleaseUtils.buildReleaseDescriptor(builder).getProjectDevelopmentVersion("groupId:subproject1")); } private static MavenProject createProject(String artifactId, String version) { @@ -2479,13 +2501,13 @@ private static MavenProject createProject(String artifactId, String version) { return new MavenProject(model); } - private static MavenProject createProjectWithPomFile(String artifactId, String version) { + private static MavenProject createProjectWithPomFile(String artifactId, String version, String pathName) { Model model = new Model(); model.setGroupId("groupId"); model.setArtifactId(artifactId); model.setVersion(version); MavenProject mavenProject = new MavenProject(model); - mavenProject.setFile(new File("src/test/resources/pomfinder/pom1.xml")); + mavenProject.setFile(new File(pathName)); return mavenProject; } } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java index 71515aed7..ee71ff20e 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/RewritePomsForReleasePhaseTest.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -454,4 +455,19 @@ public void testRewritePomWithDifferentVersionsAcrossModules() throws Exception assertTrue(comparePomFiles(reactorProjects)); } + + @Test + public void testRewritePomWithCheckModificationExcludes() throws Exception { + List reactorProjects = createReactorProjects("multimodule-with-check-modification-excludes"); + + ReleaseDescriptorBuilder builder = + createDescriptorFromProjects(reactorProjects, "multimodule-with-check-modification-excludes"); + builder.addReleaseVersion("groupId:artifactId", NEXT_VERSION); + builder.addReleaseVersion("groupId:subproject1", ALTERNATIVE_NEXT_VERSION); + builder.setCheckModificationExcludes(Collections.singletonList("**/subproject2/*")); + + phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); + + assertTrue(comparePomFiles(reactorProjects)); + } } diff --git a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/ScmCommitPreparationPhaseTest.java b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/ScmCommitPreparationPhaseTest.java index d4e04cf84..ca11da07e 100644 --- a/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/ScmCommitPreparationPhaseTest.java +++ b/maven-release-manager/src/test/java/org/apache/maven/shared/release/phase/ScmCommitPreparationPhaseTest.java @@ -521,6 +521,52 @@ public void testSuppressCommitAfterBranch() throws Exception { verifyNoMoreInteractions(scmProviderMock); } + @Test + public void testCommitMultiModuleWithCheckModificationExcludes() throws Exception { + // prepare + ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder(); + String dir = "scm-commit/multiple-poms"; + List reactorProjects = createReactorProjects(dir, dir, null); + builder.setScmSourceUrl("scm-url"); + MavenProject rootProject = ReleaseUtil.getRootProject(reactorProjects); + builder.setWorkingDirectory(rootProject.getFile().getParentFile().getAbsolutePath()); + builder.setScmReleaseLabel("release-label"); + builder.setCheckModificationExcludes(Collections.singletonList("**/subproject2/*")); + + List poms = new ArrayList<>(); + for (Iterator i = reactorProjects.iterator(); i.hasNext(); ) { + MavenProject project = i.next(); + //This is a mock match that verifies that the project has not been submitted + if (!"subproject2".equals(project.getName())) { + poms.add(project.getFile()); + } + } + ScmFileSet fileSet = new ScmFileSet(rootProject.getFile().getParentFile(), poms); + + ScmProvider scmProviderMock = mock(ScmProvider.class); + when(scmProviderMock.checkIn( + isA(ScmRepository.class), + argThat(new IsScmFileSetEquals(fileSet)), + isNull(ScmVersion.class), + eq(PREFIX + "release-label"))) + .thenReturn(new CheckInScmResult( + "...", + Collections.singletonList( + new ScmFile(rootProject.getFile().getPath(), ScmFileStatus.CHECKED_IN)))); + ScmManagerStub stub = (ScmManagerStub) lookup(ScmManager.class); + stub.setScmProvider(scmProviderMock); + + // execute + phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), new DefaultReleaseEnvironment(), reactorProjects); + + // verify + verify(scmProviderMock) + .checkIn( + isA(ScmRepository.class), argThat(new IsScmFileSetEquals(fileSet)), + isNull(ScmVersion.class), eq(PREFIX + "release-label")); + verifyNoMoreInteractions(scmProviderMock); + } + private List createReactorProjects() throws Exception { String dir = "scm-commit/single-pom"; return createReactorProjects(dir, dir, null); diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/expected-pom.xml new file mode 100644 index 000000000..e2e54a297 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/expected-pom.xml @@ -0,0 +1,37 @@ + + + + 4.0.0 + groupId + artifactId + 1.0 + pom + + + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + scm:svn:file://localhost/tmp/scm-repo/tags/release-label + file://localhost/tmp/scm-repo/tags/release-label + + + + subproject1 + subproject2 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/pom.xml new file mode 100644 index 000000000..e51608bf3 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/pom.xml @@ -0,0 +1,37 @@ + + + + 4.0.0 + groupId + artifactId + 1.0-SNAPSHOT + pom + + + scm:svn:file://localhost/tmp/scm-repo/trunk + scm:svn:file://localhost/tmp/scm-repo/trunk + file://localhost/tmp/scm-repo/trunk + + + + subproject1 + subproject2 + + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/expected-pom.xml new file mode 100644 index 000000000..6d39eb267 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/expected-pom.xml @@ -0,0 +1,30 @@ + + + + 4.0.0 + + groupId + artifactId + 1.0 + + + subproject1 + 2.0 + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/pom.xml new file mode 100644 index 000000000..fd15e14f3 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject1/pom.xml @@ -0,0 +1,30 @@ + + + + 4.0.0 + + groupId + artifactId + 1.0-SNAPSHOT + + + subproject1 + 2.0-SNAPSHOT + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/expected-pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/expected-pom.xml new file mode 100644 index 000000000..559f728c0 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/expected-pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + + groupId + artifactId + 1.0-SNAPSHOT + + + + scm:svn:file://localhost/tmp/scm-repo/trunk/subproject2 + scm:svn:file://localhost/tmp/scm-repo/trunk/subproject2 + file://localhost/tmp/scm-repo/trunk/subproject2 + + + subproject2 + 2.0-SNAPSHOT + diff --git a/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/pom.xml b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/pom.xml new file mode 100644 index 000000000..559f728c0 --- /dev/null +++ b/maven-release-manager/src/test/resources/projects/rewrite-for-release/multimodule-with-check-modification-excludes/subproject2/pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + + groupId + artifactId + 1.0-SNAPSHOT + + + + scm:svn:file://localhost/tmp/scm-repo/trunk/subproject2 + scm:svn:file://localhost/tmp/scm-repo/trunk/subproject2 + file://localhost/tmp/scm-repo/trunk/subproject2 + + + subproject2 + 2.0-SNAPSHOT +