From 0c892936ed98a6b4f24d56f7bd332b217729276b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Kr=C3=BCger?= Date: Thu, 19 Oct 2023 15:19:18 +0200 Subject: [PATCH] Test shows error --- .../parsers/CompareParserRecipeRunTest.java | 112 ++++++++++++++++++ .../maven-projects/parser-recipe-run/pom.xml | 57 +++++++++ .../java/com/example/app/AppApplication.java | 29 +++++ .../src/main/java/com/example/app/My.java | 29 +++++ .../java/com/example/app/MyRepository.java | 7 ++ 5 files changed, 234 insertions(+) create mode 100644 sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/CompareParserRecipeRunTest.java create mode 100644 sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/pom.xml create mode 100644 sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/AppApplication.java create mode 100644 sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/My.java create mode 100644 sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/MyRepository.java diff --git a/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/CompareParserRecipeRunTest.java b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/CompareParserRecipeRunTest.java new file mode 100644 index 000000000..71ca8e05f --- /dev/null +++ b/sbm-support-rewrite/src/test/java/org/springframework/sbm/parsers/CompareParserRecipeRunTest.java @@ -0,0 +1,112 @@ +/* + * 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.sbm.parsers; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.ExpectedToFail; +import org.junitpioneer.jupiter.Issue; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.RecipeRun; +import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.InMemoryLargeSourceSet; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.tree.J; +import org.openrewrite.marker.Markers; +import org.openrewrite.marker.SearchResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.sbm.boot.autoconfigure.SbmSupportRewriteConfiguration; +import org.springframework.sbm.parsers.maven.RewriteMavenProjectParser; +import org.springframework.sbm.parsers.maven.SbmTestConfiguration; +import org.springframework.sbm.test.util.TestProjectHelper; + +import java.nio.file.Path; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Fabian Krüger + */ +@SpringBootTest(classes = {SbmSupportRewriteConfiguration.class, SbmTestConfiguration.class}) +@Issue("https://github.com/spring-projects-experimental/spring-boot-migrator/issues/975") +public class CompareParserRecipeRunTest { + + @Autowired + RewriteProjectParser sut; + + @Autowired + RewriteMavenProjectParser comparingParser; + + @Autowired + private ExecutionContext executionContext; + + @Test + @DisplayName("runningRecipe") + @ExpectedToFail("FIXME: #975") + void runningRecipe() { + Path baseDir = TestProjectHelper.getMavenProject("parser-recipe-run"); + RewriteProjectParsingResult sutParsingResult = sut.parse(baseDir); + RewriteProjectParsingResult compParsingResult = comparingParser.parse(baseDir); + + AtomicInteger counter = new AtomicInteger(0); + + Recipe recipe = new Recipe() { + @Override + public String getDisplayName() { + return "Dummy recipe for test"; + } + + @Override + public String getDescription() { + return getDisplayName(); + } + + @Override + public TreeVisitor getVisitor() { + return new JavaIsoVisitor<>() { + @Override + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) { + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext); + if(cd.getType().getFullyQualifiedName().equals("com.example.app.My")) { + Markers markers = cd.getMarkers(); + markers = markers.addIfAbsent(new SearchResult(UUID.randomUUID(), "Another visit")); + // This triggers the result + cd = cd.withMarkers(markers); + counter.incrementAndGet(); + } + + return cd; + } + }; + } + }; + // Run the Comparing Parser reusing OpenRewrite code + RecipeRun compRecipeRun = recipe.run(new InMemoryLargeSourceSet(compParsingResult.sourceFiles()), executionContext); + assertThat(counter.get()).isEqualTo(2); + assertThat(compRecipeRun.getChangeset().getAllResults()).hasSize(1); + + // Run Parser independent from Maven + counter.setRelease(0); + RecipeRun sutRecipeRun = recipe.run(new InMemoryLargeSourceSet(sutParsingResult.sourceFiles()), executionContext); + assertThat(counter.get()).isEqualTo(3); // differs, should be 2 + assertThat(sutRecipeRun.getChangeset().getAllResults()).hasSize(1); // is 0 + } + +} diff --git a/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/pom.xml b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/pom.xml new file mode 100644 index 000000000..88839c124 --- /dev/null +++ b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/pom.xml @@ -0,0 +1,57 @@ + + + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.17 + + + com.example + app + 0.0.1-SNAPSHOT + app + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/AppApplication.java b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/AppApplication.java new file mode 100644 index 000000000..f32d594f2 --- /dev/null +++ b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/AppApplication.java @@ -0,0 +1,29 @@ +/* + * 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 com.example.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AppApplication { + + public static void main(String[] args) { + SpringApplication.run(AppApplication.class, args); + } + +} diff --git a/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/My.java b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/My.java new file mode 100644 index 000000000..9306ec196 --- /dev/null +++ b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/My.java @@ -0,0 +1,29 @@ +/* + * 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 com.example.app; + +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Fabian Krüger + */ +@Entity +public class My { + @Id + private Long id; + private String name; +} diff --git a/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/MyRepository.java b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/MyRepository.java new file mode 100644 index 000000000..c3e22c939 --- /dev/null +++ b/sbm-support-rewrite/testcode/maven-projects/parser-recipe-run/src/main/java/com/example/app/MyRepository.java @@ -0,0 +1,7 @@ +package com.example.app; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MyRepository extends JpaRepository { + +} \ No newline at end of file