From ba8bc91f52864b69bc6699eee6a38941cbc12756 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fabian=20Kr=C3=BCger?=
<56278322+fabapp2@users.noreply.github.com>
Date: Mon, 22 Apr 2024 19:24:17 +0200
Subject: [PATCH] Extract test helper module
* Extract test and util module
* Enhance TestProjectHelper (#76)
* Disable test failing under Windows
---
.../dependency-resolution-tests/pom.xml | 13 --
.../pom.xml | 17 +-
.../private-artifact-repository-tests/pom.xml | 8 -
spring-rewrite-commons-launcher/pom.xml | 15 +-
...=> RewriteProjectParserLstParityTest.java} | 64 ++++----
...er.java => ParserLstParityTestHelper.java} | 14 +-
.../rewrite/test/util/DummyResource.java | 0
.../rewrite/test/util/TestProjectHelper.java | 59 ++++++-
.../test/util/TestProjectHelperTest.java | 148 ++++++++++++++++++
9 files changed, 266 insertions(+), 72 deletions(-)
rename spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/{RewriteProjectParserParityTest.java => RewriteProjectParserLstParityTest.java} (83%)
rename spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/{ParserParityTestHelper.java => ParserLstParityTestHelper.java} (97%)
rename {spring-rewrite-commons-launcher/src/test => spring-rewrite-commons-test/src/main}/java/org/springframework/rewrite/test/util/DummyResource.java (100%)
rename {spring-rewrite-commons-launcher/src/test => spring-rewrite-commons-test/src/main}/java/org/springframework/rewrite/test/util/TestProjectHelper.java (63%)
create mode 100644 spring-rewrite-commons-test/src/test/java/org/springframework/rewrite/test/util/TestProjectHelperTest.java
diff --git a/spring-rewrite-commons-functional-tests/dependency-resolution-tests/pom.xml b/spring-rewrite-commons-functional-tests/dependency-resolution-tests/pom.xml
index a0b0bb9..3cfa1c8 100644
--- a/spring-rewrite-commons-functional-tests/dependency-resolution-tests/pom.xml
+++ b/spring-rewrite-commons-functional-tests/dependency-resolution-tests/pom.xml
@@ -28,19 +28,6 @@
org.openrewrite
rewrite-java
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
- org.springframework.rewrite
- spring-rewrite-commons-launcher
- test
- test-jar
- 0.1.0-SNAPSHOT
- test
-
\ No newline at end of file
diff --git a/spring-rewrite-commons-functional-tests/pom.xml b/spring-rewrite-commons-functional-tests/pom.xml
index 9df7174..6602565 100644
--- a/spring-rewrite-commons-functional-tests/pom.xml
+++ b/spring-rewrite-commons-functional-tests/pom.xml
@@ -53,6 +53,21 @@
import
-
+
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons-test
+ test
+
+
+ org.springframework.rewrite
+ spring-rewrite-commons-launcher
+ test
+ test-jar
+ 0.1.0-SNAPSHOT
+ test
+
+
\ No newline at end of file
diff --git a/spring-rewrite-commons-functional-tests/private-artifact-repository-tests/pom.xml b/spring-rewrite-commons-functional-tests/private-artifact-repository-tests/pom.xml
index 58172e7..7e20378 100644
--- a/spring-rewrite-commons-functional-tests/private-artifact-repository-tests/pom.xml
+++ b/spring-rewrite-commons-functional-tests/private-artifact-repository-tests/pom.xml
@@ -21,14 +21,6 @@
spring-rewrite-commons-launcher
0.1.0-SNAPSHOT
-
- org.springframework.rewrite
- spring-rewrite-commons-launcher
- test
- test-jar
- 0.1.0-SNAPSHOT
- test
-
diff --git a/spring-rewrite-commons-launcher/pom.xml b/spring-rewrite-commons-launcher/pom.xml
index b1ceb08..16757cb 100644
--- a/spring-rewrite-commons-launcher/pom.xml
+++ b/spring-rewrite-commons-launcher/pom.xml
@@ -26,6 +26,11 @@
spring-boot-configuration-processor
true
+
+ org.springframework.rewrite
+ spring-rewrite-commons-utils
+ 0.1.0-SNAPSHOT
+
org.openrewrite
rewrite-maven
@@ -162,14 +167,8 @@
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
- org.junit-pioneer
- junit-pioneer
- ${junit-pioneer.version}
+ org.springframework.rewrite
+ spring-rewrite-commons-test
test
diff --git a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/RewriteProjectParserParityTest.java b/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/RewriteProjectParserLstParityTest.java
similarity index 83%
rename from spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/RewriteProjectParserParityTest.java
rename to spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/RewriteProjectParserLstParityTest.java
index 5215248..69d937c 100644
--- a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/RewriteProjectParserParityTest.java
+++ b/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/parser/RewriteProjectParserLstParityTest.java
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
+import org.junitpioneer.jupiter.ExpectedToFail;
import org.junitpioneer.jupiter.Issue;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
@@ -35,7 +36,7 @@ import org.openrewrite.tree.ParsingExecutionContextView;
import org.springframework.rewrite.parser.maven.ComparingParserFactory;
import org.springframework.rewrite.parser.maven.RewriteMavenProjectParser;
import org.springframework.rewrite.test.util.DummyResource;
-import org.springframework.rewrite.test.util.ParserParityTestHelper;
+import org.springframework.rewrite.test.util.ParserLstParityTestHelper;
import org.springframework.rewrite.test.util.TestProjectHelper;
import java.nio.file.Path;
@@ -52,7 +53,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.fail;
/**
- * Test parity between OpenRewrite parser logic and RewriteProjectParser.
+ * Test parity of generated LST between OpenRewrite parser logic and RewriteProjectParser.
*
* RewriteMavenProjectParser resembles the parser logic from OpenRewrite's Maven plugin
*
@@ -67,42 +68,45 @@ class RewriteProjectParserParityTest {
@DisplayName("parseResources")
void parseResources() {
Path baseDir = TestProjectHelper.getMavenProject("resources");
- ParserParityTestHelper.scanProjectDir(baseDir).verifyParity((comparingParsingResult, testedParsingResult) -> {
- assertThat(comparingParsingResult.sourceFiles()).hasSize(5);
- });
+ ParserLstParityTestHelper.scanProjectDir(baseDir)
+ .verifyParity((comparingParsingResult, testedParsingResult) -> {
+ assertThat(comparingParsingResult.sourceFiles()).hasSize(5);
+ });
}
@Test
@DisplayName("testFailingProject")
void testFailingProject() {
Path baseDir = Path.of("./testcode/maven-projects/failing");
- ParserParityTestHelper.scanProjectDir(baseDir).verifyParity((comparingParsingResult, testedParsingResult) -> {
- assertThat(comparingParsingResult.sourceFiles().get(1)).isInstanceOf(J.CompilationUnit.class);
- J.CompilationUnit cu = (J.CompilationUnit) comparingParsingResult.sourceFiles().get(1);
- assertThat(cu.getTypesInUse()
- .getTypesInUse()
- .stream()
- .map(t -> t.toString())
- .anyMatch(t -> t.equals("javax.validation.constraints.Min"))).isTrue();
+ ParserLstParityTestHelper.scanProjectDir(baseDir)
+ .verifyParity((comparingParsingResult, testedParsingResult) -> {
+ assertThat(comparingParsingResult.sourceFiles().get(1)).isInstanceOf(J.CompilationUnit.class);
+ J.CompilationUnit cu = (J.CompilationUnit) comparingParsingResult.sourceFiles().get(1);
+ assertThat(cu.getTypesInUse()
+ .getTypesInUse()
+ .stream()
+ .map(t -> t.toString())
+ .anyMatch(t -> t.equals("javax.validation.constraints.Min"))).isTrue();
- assertThat(testedParsingResult.sourceFiles().get(1)).isInstanceOf(J.CompilationUnit.class);
- J.CompilationUnit cu2 = (J.CompilationUnit) testedParsingResult.sourceFiles().get(1);
- assertThat(cu2.getTypesInUse()
- .getTypesInUse()
- .stream()
- .map(t -> t.toString())
- .anyMatch(t -> t.equals("javax.validation.constraints.Min"))).isTrue();
- });
+ assertThat(testedParsingResult.sourceFiles().get(1)).isInstanceOf(J.CompilationUnit.class);
+ J.CompilationUnit cu2 = (J.CompilationUnit) testedParsingResult.sourceFiles().get(1);
+ assertThat(cu2.getTypesInUse()
+ .getTypesInUse()
+ .stream()
+ .map(t -> t.toString())
+ .anyMatch(t -> t.equals("javax.validation.constraints.Min"))).isTrue();
+ });
}
@Test
@DisplayName("parse4Modules")
void parse4Modules() {
Path baseDir = TestProjectHelper.getMavenProject("4-modules");
- ParserParityTestHelper.scanProjectDir(baseDir).verifyParity((comparingParsingResult, testedParsingResult) -> {
- assertThat(comparingParsingResult.sourceFiles()).hasSize(4);
- assertThat(testedParsingResult.sourceFiles()).hasSize(4);
- });
+ ParserLstParityTestHelper.scanProjectDir(baseDir)
+ .verifyParity((comparingParsingResult, testedParsingResult) -> {
+ assertThat(comparingParsingResult.sourceFiles()).hasSize(4);
+ assertThat(testedParsingResult.sourceFiles()).hasSize(4);
+ });
}
@Test
@@ -170,7 +174,7 @@ class RewriteProjectParserParityTest {
comparingSpringRewriteProperties.setPomCacheEnabled(true);
comparingSpringRewriteProperties.setPomCacheEnabled(true);
- ParserParityTestHelper.scanProjectDir(tempDir)
+ ParserLstParityTestHelper.scanProjectDir(tempDir)
.withParserProperties(comparingSpringRewriteProperties)
.verifyParity();
}
@@ -185,7 +189,7 @@ class RewriteProjectParserParityTest {
void parseMultiModule1() {
Path baseDir = getMavenProject("multi-module-1");
- ParserParityTestHelper.scanProjectDir(baseDir).verifyParity();
+ ParserLstParityTestHelper.scanProjectDir(baseDir).verifyParity();
}
@Test
@@ -206,7 +210,7 @@ class RewriteProjectParserParityTest {
@Issue("https://github.com/spring-projects-experimental/spring-boot-migrator/issues/875")
void parseCheckstyle() {
Path baseDir = getMavenProject("checkstyle");
- ParserParityTestHelper.scanProjectDir(baseDir)
+ ParserLstParityTestHelper.scanProjectDir(baseDir)
.parseSequentially()
.verifyParity((comparingParsingResult, testedParsingResult) -> {
assertThat(
@@ -224,7 +228,7 @@ class RewriteProjectParserParityTest {
@Test
@DisplayName("Parse complex Maven reactor project")
- @Disabled("https://github.com/openrewrite/rewrite/issues/3409")
+ @ExpectedToFail("https://github.com/openrewrite/rewrite/issues/3409")
void parseComplexMavenReactorProject() {
Path projectRoot = Path.of("./testcode/maven-projects/cwa-server").toAbsolutePath().normalize();
TestProjectHelper.createTestProject(projectRoot)
@@ -250,7 +254,7 @@ class RewriteProjectParserParityTest {
}
});
- ParserParityTestHelper.scanProjectDir(projectRoot)
+ ParserLstParityTestHelper.scanProjectDir(projectRoot)
.parseSequentially()
.withExecutionContextForComparingParser(executionContext)
.withParserProperties(springRewriteProperties)
diff --git a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/ParserParityTestHelper.java b/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/ParserLstParityTestHelper.java
similarity index 97%
rename from spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/ParserParityTestHelper.java
rename to spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/ParserLstParityTestHelper.java
index 3f4bfe9..58b5095 100644
--- a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/ParserParityTestHelper.java
+++ b/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/ParserLstParityTestHelper.java
@@ -48,7 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Fabian Krüger
*/
-public class ParserParityTestHelper {
+public class ParserLstParityTestHelper {
private final Path baseDir;
@@ -58,12 +58,12 @@ public class ParserParityTestHelper {
private ExecutionContext executionContext;
- private ParserParityTestHelper(Path baseDir) {
+ private ParserLstParityTestHelper(Path baseDir) {
this.baseDir = baseDir;
}
- public static ParserParityTestHelper scanProjectDir(Path baseDir) {
- ParserParityTestHelper helper = new ParserParityTestHelper(baseDir);
+ public static ParserLstParityTestHelper scanProjectDir(Path baseDir) {
+ ParserLstParityTestHelper helper = new ParserLstParityTestHelper(baseDir);
return helper;
}
@@ -71,17 +71,17 @@ public class ParserParityTestHelper {
* Sequentially parse given project using tested parser and then comparing parser. The
* parser are executed in parallel by default.
*/
- public ParserParityTestHelper parseSequentially() {
+ public ParserLstParityTestHelper parseSequentially() {
this.isParallelParse = false;
return this;
}
- public ParserParityTestHelper withParserProperties(SpringRewriteProperties springRewriteProperties) {
+ public ParserLstParityTestHelper withParserProperties(SpringRewriteProperties springRewriteProperties) {
this.springRewriteProperties = springRewriteProperties;
return this;
}
- public ParserParityTestHelper withExecutionContextForComparingParser(ExecutionContext executionContext) {
+ public ParserLstParityTestHelper withExecutionContextForComparingParser(ExecutionContext executionContext) {
this.executionContext = executionContext;
return this;
}
diff --git a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/DummyResource.java b/spring-rewrite-commons-test/src/main/java/org/springframework/rewrite/test/util/DummyResource.java
similarity index 100%
rename from spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/DummyResource.java
rename to spring-rewrite-commons-test/src/main/java/org/springframework/rewrite/test/util/DummyResource.java
diff --git a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/TestProjectHelper.java b/spring-rewrite-commons-test/src/main/java/org/springframework/rewrite/test/util/TestProjectHelper.java
similarity index 63%
rename from spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/TestProjectHelper.java
rename to spring-rewrite-commons-test/src/main/java/org/springframework/rewrite/test/util/TestProjectHelper.java
index befece9..507b23e 100644
--- a/spring-rewrite-commons-launcher/src/test/java/org/springframework/rewrite/test/util/TestProjectHelper.java
+++ b/spring-rewrite-commons-test/src/main/java/org/springframework/rewrite/test/util/TestProjectHelper.java
@@ -16,8 +16,10 @@
package org.springframework.rewrite.test.util;
import org.apache.commons.io.FileUtils;
-import org.openrewrite.shaded.jgit.api.Git;
-import org.openrewrite.shaded.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.ResetCommand;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Ref;
import org.springframework.core.io.Resource;
import org.springframework.rewrite.utils.ResourceUtil;
@@ -27,12 +29,19 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.concurrent.ExecutionException;
/**
+ * Helper to set up test projects for OpenRewrite migrations.
+ *
* @author Fabian Krüger
*/
public class TestProjectHelper {
+ public static final String TESTCODE_MAVEN_PROJECTS = "./testcode/maven-projects/";
+
+ private static final String TESTCODE_GRADLE_PROJECTS = "./testcode/gradle-projects/";
+
private final Path targetDir;
private List resources = new ArrayList<>();
@@ -45,12 +54,26 @@ public class TestProjectHelper {
private boolean deleteDirIfExists = false;
+ private String gitHash = null;
+
public TestProjectHelper(Path targetDir) {
this.targetDir = targetDir;
}
- public static Path getMavenProject(String s) {
- return Path.of("./testcode/maven-projects/").resolve(s).toAbsolutePath().normalize();
+ /**
+ * Returns the absolute path to {@code ./testcode/maven-projects/}
+ * @param givenProjectDir
+ */
+ public static Path getMavenProject(String givenProjectDir) {
+ return Path.of(TESTCODE_MAVEN_PROJECTS).resolve(givenProjectDir).toAbsolutePath().normalize();
+ }
+
+ /**
+ * Returns the absolute path to {@code ./testcode/gradle-projects/}
+ * @param givenProjectDir
+ */
+ public static Path getGradleProject(String givenProjectDir) {
+ return Path.of(TESTCODE_GRADLE_PROJECTS).resolve(givenProjectDir).toAbsolutePath().normalize();
}
public static TestProjectHelper createTestProject(Path targetDir) {
@@ -76,6 +99,11 @@ public class TestProjectHelper {
return this;
}
+ public TestProjectHelper checkoutCommit(String gitHash) {
+ this.gitHash = gitHash;
+ return this;
+ }
+
public TestProjectHelper checkoutTag(String tag) {
this.gitTag = tag;
return this;
@@ -110,7 +138,10 @@ public class TestProjectHelper {
Git git = Git.cloneRepository().setDirectory(directory).setURI(this.gitUrl).call();
if (gitTag != null) {
- git.checkout().setName("refs/tags/" + gitTag).call();
+ git.checkout().setName("refs/tags/" + gitTag).setCreateBranch(false).call();
+ }
+ else if (gitHash != null) {
+ checkoutCommit(git, gitHash);
}
}
catch (GitAPIException e) {
@@ -120,6 +151,24 @@ public class TestProjectHelper {
ResourceUtil.write(targetDir, resources);
}
+ private void checkoutCommit(Git git, String startingGitHash) {
+ try {
+ Ref ref = git.checkout().setName(startingGitHash).call();
+ }
+ catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private void resetRepo(Git git, String startingGitHash) {
+ try {
+ git.reset().setRef(startingGitHash).setMode(ResetCommand.ResetType.HARD).call();
+ }
+ catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
public TestProjectHelper addResource(String relativePath, String content) {
DummyResource dummyResource = new DummyResource(targetDir.resolve(relativePath), content);
this.resources.add(dummyResource);
diff --git a/spring-rewrite-commons-test/src/test/java/org/springframework/rewrite/test/util/TestProjectHelperTest.java b/spring-rewrite-commons-test/src/test/java/org/springframework/rewrite/test/util/TestProjectHelperTest.java
new file mode 100644
index 0000000..a1f2b03
--- /dev/null
+++ b/spring-rewrite-commons-test/src/test/java/org/springframework/rewrite/test/util/TestProjectHelperTest.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2021 - 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://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.
+ */
+package org.springframework.rewrite.test.util;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Repository;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Fabian Krüger
+ */
+class TestProjectHelperTest {
+
+ @TempDir
+ Path baseDir;
+
+ @Test
+ @DisplayName("getMavenProject")
+ void getMavenProject() {
+ Path project = TestProjectHelper.getMavenProject("maven-project");
+ Path expected = Path.of("./testcode/maven-projects/maven-project").toAbsolutePath().normalize();
+ assertThat(project).isEqualTo(expected);
+ }
+
+ @Test
+ @DisplayName("getGradleProject")
+ void getGradleProject() {
+ Path project = TestProjectHelper.getGradleProject("maven-project");
+ Path expected = Path.of("./testcode/gradle-projects/maven-project").toAbsolutePath().normalize();
+ assertThat(project).isEqualTo(expected);
+ }
+
+ @Test
+ @DisplayName("writeToFileSystem should create dirs")
+ void writeToFileSystem() {
+ Path baseDir = this.baseDir.resolve("sub-dir");
+ TestProjectHelper.createTestProject(baseDir).writeToFilesystem();
+ assertThat(baseDir).exists();
+ }
+
+ @Test
+ @DisplayName("addResource should write file with content")
+ void addResource(@TempDir Path tempDir) {
+ Path baseDir = tempDir.resolve("sub-dir");
+ TestProjectHelper.createTestProject(baseDir)
+ .addResource("src/main/resources/some.txt", "content...")
+ .writeToFilesystem();
+ assertThat(baseDir.resolve("src/main/resources/some.txt")).exists().hasContent("content...");
+ }
+
+ @Test
+ @DisplayName("initializeGitRepo should init a git repo")
+ void initializeGitRepo() throws IOException, GitAPIException {
+ Path baseDir = this.baseDir.resolve("sub-dir");
+ TestProjectHelper.createTestProject(baseDir).initializeGitRepo().writeToFilesystem();
+ assertThat(baseDir.resolve(".git")).exists();
+ assertThat(Git.open(baseDir.toFile()).status().call()).isNotNull();
+ }
+
+ @Test
+ @DisplayName("deleteDirIfExists")
+ void deleteDirIfExists() throws IOException {
+ Files.writeString(baseDir.resolve("some.txt"), "...");
+ assertThat(baseDir.resolve("some.txt")).exists();
+
+ TestProjectHelper.createTestProject(baseDir)
+ .deleteDirIfExists()
+ .addResource("other.txt", "...")
+ .writeToFilesystem();
+ assertThat(baseDir.resolve("some.txt")).doesNotExist();
+ assertThat(baseDir.resolve("other.txt")).exists();
+ }
+
+ @Nested
+ @DisabledOnOs(value = OS.WINDOWS, disabledReason = "https://github.com/junit-team/junit5/issues/2811")
+ class TextProjectHelper_ExistingGitRepoSupport {
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ @DisplayName("cloneGitProject should clone a given GitHub project")
+ void cloneGitProject() throws IOException, GitAPIException {
+ Path baseDir = tempDir.resolve("sub-dir");
+ String url = "https://github.com/spring-guides/gs-rest-service-cors";
+ TestProjectHelper.createTestProject(baseDir).cloneGitProject(url).writeToFilesystem();
+ assertThat(baseDir.resolve(".git")).exists();
+ Repository repository = Git.open(baseDir.toFile()).getRepository();
+ assertThat(repository.getBranch().toString()).isEqualTo("main");
+ assertThat(repository.getRemoteNames()).containsExactly("origin");
+ }
+
+ @Test
+ @DisplayName("checkoutTag should checkout given tag name")
+ void checkoutTag() throws IOException, GitAPIException {
+ Path baseDir = tempDir.resolve("sub-dir");
+ String url = "https://github.com/spring-guides/gs-rest-service-cors";
+ TestProjectHelper.createTestProject(baseDir)
+ .cloneGitProject(url)
+ .checkoutTag("2.1.3.RELEASE")
+ .writeToFilesystem();
+ assertThat(baseDir.resolve(".git")).exists();
+ Repository repository = Git.open(baseDir.toFile()).getRepository();
+ assertThat(repository.getBranch().toString()).isEqualTo("f8681fa5e4eb35665107d0adac95f79b1b92df47");
+ }
+
+ @Test
+ @DisplayName("checkoutCommit should checkout given commit hash")
+ void checkoutCommit() throws IOException, GitAPIException {
+ Path baseDir = tempDir.resolve("sub-dir");
+ String url = "https://github.com/spring-guides/gs-rest-service-cors";
+ TestProjectHelper.createTestProject(baseDir)
+ .cloneGitProject(url)
+ .checkoutCommit("f8681fa")
+ .writeToFilesystem();
+ assertThat(baseDir.resolve(".git")).exists();
+ Repository repository = Git.open(baseDir.toFile()).getRepository();
+ assertThat(repository.getBranch().toString()).isEqualTo("f8681fa5e4eb35665107d0adac95f79b1b92df47");
+ }
+
+ }
+
+}
\ No newline at end of file