|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.sbm.parsers; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junitpioneer.jupiter.ExpectedToFail; |
| 21 | +import org.junitpioneer.jupiter.Issue; |
| 22 | +import org.openrewrite.ExecutionContext; |
| 23 | +import org.openrewrite.Recipe; |
| 24 | +import org.openrewrite.RecipeRun; |
| 25 | +import org.openrewrite.TreeVisitor; |
| 26 | +import org.openrewrite.internal.InMemoryLargeSourceSet; |
| 27 | +import org.openrewrite.java.JavaIsoVisitor; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | +import org.openrewrite.marker.Markers; |
| 30 | +import org.openrewrite.marker.SearchResult; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.sbm.boot.autoconfigure.SbmSupportRewriteConfiguration; |
| 34 | +import org.springframework.sbm.parsers.maven.RewriteMavenProjectParser; |
| 35 | +import org.springframework.sbm.parsers.maven.SbmTestConfiguration; |
| 36 | +import org.springframework.sbm.test.util.TestProjectHelper; |
| 37 | + |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.util.UUID; |
| 40 | +import java.util.concurrent.atomic.AtomicInteger; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Fabian Krüger |
| 46 | + */ |
| 47 | +@SpringBootTest(classes = {SbmSupportRewriteConfiguration.class, SbmTestConfiguration.class}) |
| 48 | +@Issue("https://github.com/spring-projects-experimental/spring-boot-migrator/issues/975") |
| 49 | +public class CompareParserRecipeRunTest { |
| 50 | + |
| 51 | + @Autowired |
| 52 | + RewriteProjectParser sut; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + RewriteMavenProjectParser comparingParser; |
| 56 | + |
| 57 | + @Autowired |
| 58 | + private ExecutionContext executionContext; |
| 59 | + |
| 60 | + @Test |
| 61 | + @DisplayName("runningRecipe") |
| 62 | + @ExpectedToFail("FIXME: #975") |
| 63 | + void runningRecipe() { |
| 64 | + Path baseDir = TestProjectHelper.getMavenProject("parser-recipe-run"); |
| 65 | + RewriteProjectParsingResult sutParsingResult = sut.parse(baseDir); |
| 66 | + RewriteProjectParsingResult compParsingResult = comparingParser.parse(baseDir); |
| 67 | + |
| 68 | + AtomicInteger counter = new AtomicInteger(0); |
| 69 | + |
| 70 | + Recipe recipe = new Recipe() { |
| 71 | + @Override |
| 72 | + public String getDisplayName() { |
| 73 | + return "Dummy recipe for test"; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String getDescription() { |
| 78 | + return getDisplayName(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 83 | + return new JavaIsoVisitor<>() { |
| 84 | + @Override |
| 85 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) { |
| 86 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext); |
| 87 | + if(cd.getType().getFullyQualifiedName().equals("com.example.app.My")) { |
| 88 | + Markers markers = cd.getMarkers(); |
| 89 | + markers = markers.addIfAbsent(new SearchResult(UUID.randomUUID(), "Another visit")); |
| 90 | + // This triggers the result |
| 91 | + cd = cd.withMarkers(markers); |
| 92 | + counter.incrementAndGet(); |
| 93 | + } |
| 94 | + |
| 95 | + return cd; |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + }; |
| 100 | + // Run the Comparing Parser reusing OpenRewrite code |
| 101 | + RecipeRun compRecipeRun = recipe.run(new InMemoryLargeSourceSet(compParsingResult.sourceFiles()), executionContext); |
| 102 | + assertThat(counter.get()).isEqualTo(2); |
| 103 | + assertThat(compRecipeRun.getChangeset().getAllResults()).hasSize(1); |
| 104 | + |
| 105 | + // Run Parser independent from Maven |
| 106 | + counter.setRelease(0); |
| 107 | + RecipeRun sutRecipeRun = recipe.run(new InMemoryLargeSourceSet(sutParsingResult.sourceFiles()), executionContext); |
| 108 | + assertThat(counter.get()).isEqualTo(3); // differs, should be 2 |
| 109 | + assertThat(sutRecipeRun.getChangeset().getAllResults()).hasSize(1); // is 0 |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments