Skip to content

Commit b119229

Browse files
authored
Provide test to prove bug in #975 (#976)
1 parent e5ffdd4 commit b119229

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2021 - 2023 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-parent</artifactId>
24+
<version>2.7.17</version>
25+
<relativePath/> <!-- lookup parent from repository -->
26+
</parent>
27+
<groupId>com.example</groupId>
28+
<artifactId>app</artifactId>
29+
<version>0.0.1-SNAPSHOT</version>
30+
<name>app</name>
31+
<description>Demo project for Spring Boot</description>
32+
<properties>
33+
<java.version>17</java.version>
34+
</properties>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-jpa</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
17+
package com.example.app;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class AppApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(AppApplication.class, args);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 com.example.app;
17+
18+
import javax.persistence.Entity;
19+
import javax.persistence.Id;
20+
21+
/**
22+
* @author Fabian Krüger
23+
*/
24+
@Entity
25+
public class My {
26+
@Id
27+
private Long id;
28+
private String name;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.app;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface MyRepository extends JpaRepository<My, Long> {
6+
7+
}

0 commit comments

Comments
 (0)