Extract test helper module

* Extract test and util module
* Enhance TestProjectHelper (#76)
* Disable test failing under Windows
This commit is contained in:
Fabian Krüger
2024-04-22 19:24:17 +02:00
committed by GitHub
parent f096b030eb
commit ba8bc91f52
9 changed files with 266 additions and 72 deletions

View File

@@ -28,19 +28,6 @@
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-launcher</artifactId>
<classifier>test</classifier>
<type>test-jar</type>
<version>0.1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -53,6 +53,21 @@
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-launcher</artifactId>
<classifier>test</classifier>
<type>test-jar</type>
<version>0.1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -21,14 +21,6 @@
<artifactId>spring-rewrite-commons-launcher</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-launcher</artifactId>
<classifier>test</classifier>
<type>test-jar</type>
<version>0.1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- prod dependencies -->

View File

@@ -26,6 +26,11 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-utils</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
@@ -162,14 +167,8 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>${junit-pioneer.version}</version>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-test</artifactId>
<scope>test</scope>
</dependency>

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -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<Resource> 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/<givenProjectDir>}
* @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/<givenProjectDir>}
* @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);

View File

@@ -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");
}
}
}