Skip to content

Commit bb4f70d

Browse files
committed
Fixing unit tests
1 parent e6a361d commit bb4f70d

File tree

11 files changed

+91
-64
lines changed

11 files changed

+91
-64
lines changed

components/sbm-core/src/test/java/org/springframework/sbm/build/api/Module_searchTestJava_Test.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ void withNoClassesInSrcTestJava_providesEmptyProjectResources() {
6666

6767
ProjectContext context = builder.build();
6868

69-
verifySearchTest(context, projectResourceSet -> {
70-
assertThat(projectResourceSet.list()).isEmpty();
71-
}, "");
69+
verifySearchTest(context, projectResourceSet -> assertThat(projectResourceSet.list()).isEmpty(), "");
7270
}
7371

7472
@Test

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/ChangesToDataPropertiesHelper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public boolean evaluate(ProjectContext context) {
6868
.collect(Collectors.toList());
6969
if (!propertiesFound.isEmpty()) {
7070
if (data.containsKey("matches")) {
71-
data.get("matches").add(new Match(absolutePath.toString(), p.getSourcePath().toString(), propertiesFound));
71+
data.get("matches").add(new Match(absolutePath.toUri().toString(), p.getSourcePath().toString(), propertiesFound));
7272
} else {
7373
List<Match> matches = new ArrayList<>();
74-
matches.add(new Match(absolutePath.toString(), p.getSourcePath().toString(), propertiesFound));
74+
matches.add(new Match(absolutePath.toUri().toString(), p.getSourcePath().toString(), propertiesFound));
7575
data.put("matches", matches);
7676
}
7777
}
@@ -87,7 +87,7 @@ public Map<String, List<Match>> getData() {
8787

8888
@RequiredArgsConstructor
8989
@Getter
90-
public class Match {
90+
public static class Match {
9191
private final String absolutePath;
9292
private final String relativePath;
9393
private final List<Object> propertiesFound;

components/sbm-recipes-boot-upgrade/src/main/resources/recipes/27_30/report/sbu30-report.yaml

+6-7
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
The scan found properties with `spring.data` prefix but no dependency matching `org.springframework.data:.*`.
234234
235235
<#list matches as match>
236-
* file://${match.absolutePath}[`${match.relativePath}`]<#lt>
236+
* ${match.absolutePath}[`${match.relativePath}`]<#lt>
237237
<#list match.propertiesFound as property>
238238
** `${property}`<#lt>
239239
</#list>
@@ -392,7 +392,7 @@
392392
393393
@Override
394394
public void configurePathMatch(PathMatchConfigurer configurer) {
395-
configurer.setUseTrailingSlashMatch(true);
395+
configurer.setUseTrailingSlashMatch(true);
396396
}
397397
}
398398
----
@@ -443,11 +443,10 @@
443443
@Configuration
444444
public class WebConfiguration implements WebMvcConfigurer {
445445
446-
@Override
447-
public void configurePathMatch(PathMatchConfigurer configurer) {
448-
configurer.setUseTrailingSlashMatch(true);
449-
}
450-
446+
@Override
447+
public void configurePathMatch(PathMatchConfigurer configurer) {
448+
configurer.setUseTrailingSlashMatch(true);
449+
}
451450
}
452451
----
453452

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade/common/actions/CreateAutoconfigurationActionTest.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.sbm.project.resource.ProjectResource;
2424
import org.springframework.sbm.project.resource.TestProjectContext;
2525

26+
import java.nio.file.Path;
2627
import java.util.List;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
@@ -71,7 +72,7 @@ public void itDeletesSourceWhenMovedToNewFile() {
7172

7273
String content = getSpringFactoryFile();
7374

74-
assertThat(content).isEqualTo("""
75+
assertThat(content).isEqualToNormalizingNewlines("""
7576
hello.world=something
7677
""");
7778
}
@@ -95,7 +96,7 @@ public void shouldMoveMultipleProperties() {
9596

9697
String content = getNewAutoConfigFileContents();
9798

98-
assertThat(content).isEqualTo("""
99+
assertThat(content).isEqualToNormalizingNewlines("""
99100
XYZ
100101
ABC
101102
DEF"""
@@ -128,15 +129,15 @@ public void moduleInsideModuleMavenSetup() {
128129
private void assertSpringConfigFileContentsInProject(String project) {
129130
List<ProjectResource> content = getAutoConfigFileAsProjectResource();
130131
assertThat(content).hasSize(1);
131-
assertThat(content.get(0).getAbsolutePath().toString())
132-
.isEqualTo(TestProjectContext.getDefaultProjectRoot()
133-
+ "/" + project + "/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports");
132+
assertThat(content.get(0).getAbsolutePath())
133+
.isEqualTo(TestProjectContext.getDefaultProjectRoot().resolve(Path.of(project,
134+
"src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports").normalize()));
134135

135136
List<ProjectResource> oldFile = getFileAsProjectResource(EXISTING_SPRING_FACTORIES_FILE);
136137
assertThat(oldFile).hasSize(1);
137-
assertThat(oldFile.get(0).getAbsolutePath().toString())
138-
.isEqualTo(TestProjectContext.getDefaultProjectRoot() +
139-
"/" + project + "/src/main/resources/META-INF/spring.factories");
138+
assertThat(oldFile.get(0).getAbsolutePath())
139+
.isEqualTo(TestProjectContext.getDefaultProjectRoot().resolve(Path.of(project,
140+
"/src/main/resources/META-INF/spring.factories")));
140141

141142
assertThat(oldFile.get(0).print()).isEqualTo("");
142143
}

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_24_25/recipes/Boot_24_25_UpdateDependenciesRecipeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void updateWithParentPom() {
4444
<version>2.5.6</version>
4545
<relativePath/> <!-- lookup parent from repository -->
4646
</parent>
47-
"""
47+
""".replace("\n", System.lineSeparator())
4848
);
4949
}
5050
}

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/ChangeJavaxPackagesToJakartaTest.java

+42-32
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package org.springframework.sbm.boot.upgrade_27_30;
1717

18+
import org.intellij.lang.annotations.Language;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.java.ChangePackage;
2021
import org.springframework.sbm.engine.context.ProjectContext;
2122
import org.springframework.sbm.java.api.JavaSource;
2223
import org.springframework.sbm.project.resource.TestProjectContext;
24+
import org.springframework.sbm.utils.LinuxWindowsPathUnifier;
2325

2426
import java.util.List;
25-
import java.util.stream.Collectors;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
2829

@@ -31,34 +32,43 @@ public class ChangeJavaxPackagesToJakartaTest {
3132
@Test
3233
void collectingJavaxPackages() {
3334

34-
String javaClass1 =
35-
"package com.example;\n" +
36-
"import javax.money.MonetaryAmount;\n" +
37-
"public class SomeClass {\n" +
38-
" public MonetaryAmount convertToEntityAttribute() {\n" +
39-
" return null;\n" +
40-
" }\n" +
41-
"}";
42-
43-
String javaClass2 =
44-
"package com.example;\n" +
45-
"import javax.persistence.Converter;\n" +
46-
"public class SomeClass2 {\n" +
47-
" public Converter getConverter() {\n" +
48-
" return null;\n" +
49-
" }\n" +
50-
"}";
35+
@Language("java")
36+
String javaClass1 = """
37+
package com.example;
38+
import javax.money.MonetaryAmount;
39+
public class SomeClass {
40+
public MonetaryAmount convertToEntityAttribute() {
41+
return null;
42+
}
43+
}
44+
""";
45+
46+
@Language("java")
47+
String javaClass2 = """
48+
package com.example;
49+
import javax.persistence.Converter;
50+
public class SomeClass2 {
51+
public Converter getConverter() {
52+
return null;
53+
}
54+
}
55+
""";
56+
57+
@Language("java")
58+
String javaClass3 = """
59+
package com.example;
60+
public class NoImports {}
61+
""";
62+
63+
@Language("java")
64+
String javaClass4 = """
65+
package com.example;
66+
import java.math.BigDecimal;
67+
public class OtherImports {
68+
private BigDecimal number;
69+
}
70+
""";
5171

52-
String javaClass3 =
53-
"package com.example;\n" +
54-
"public class NoImports {}";
55-
56-
String javaClass4 =
57-
"package com.example;\n" +
58-
"import java.math.BigDecimal;\n" +
59-
"public class OtherImports {\n" +
60-
" private BigDecimal number;\n" +
61-
"}";
6272
ProjectContext context = TestProjectContext.buildProjectContext()
6373
.withBuildFileHavingDependencies("javax.money:money-api:1.1")
6474
.withJavaSource("src/main/java", javaClass1)
@@ -67,13 +77,13 @@ void collectingJavaxPackages() {
6777
.withJavaSource("src/main/java", javaClass4)
6878
.build();
6979

70-
List<JavaSource> matches = context.getProjectJavaSources().asStream()
80+
List<JavaSource> matches = context.getProjectJavaSources().stream()
7181
.filter(js -> js.hasImportStartingWith("javax."))
72-
.collect(Collectors.toList());
82+
.toList();
7383

7484
assertThat(matches).hasSize(2);
75-
assertThat(matches.get(0).getSourcePath().toString()).isEqualTo("src/main/java/com/example/SomeClass.java");
76-
assertThat(matches.get(1).getSourcePath().toString()).isEqualTo("src/main/java/com/example/SomeClass2.java");
85+
assertThat(LinuxWindowsPathUnifier.unifyPath(matches.get(0).getSourcePath())).isEqualTo("src/main/java/com/example/SomeClass.java");
86+
assertThat(LinuxWindowsPathUnifier.unifyPath(matches.get(1).getSourcePath())).isEqualTo("src/main/java/com/example/SomeClass2.java");
7787
matches.forEach(m -> System.out.println(m.getSourcePath()));
7888

7989
}

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/BannerSupportReportSectionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void rendersBannerSupportInformation() {
4949
The scan found banner image files here:
5050
5151
* `%1$s`
52-
* `%2%s`
52+
* `%2$s`
5353
5454
==== Remediation
5555

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/ChangesToDataPropertiesReportSectionTest.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
2525
import org.springframework.sbm.project.resource.TestProjectContext;
2626

27+
import java.nio.file.Path;
28+
2729

2830
/**
2931
* @author Fabian Krüger
@@ -40,6 +42,10 @@ void changesToDataPropertiesSection_renders() {
4042
.withProjectResource("src/main/resources/application-another.properties", "spring.data.here=there")
4143
.build();
4244

45+
Path projectRoot = context.getProjectRootDirectory();
46+
Path props1 = Path.of("src", "main", "resources", "application.properties");
47+
Path props2 = Path.of("src", "main", "resources", "application-another.properties");
48+
4349
SpringBootUpgradeReportTestSupport.generatedSection("Changes to Data Properties")
4450
.fromProjectContext(context)
4551
.shouldRenderAs(
@@ -53,16 +59,23 @@ void changesToDataPropertiesSection_renders() {
5359
==== Why is the application affected
5460
The scan found properties with `spring.data` prefix but no dependency matching `org.springframework.data:.*`.
5561
56-
* file://<PATH>/src/main/resources/application.properties[`src/main/resources/application.properties`]
62+
* %s[`%s`]
5763
** `spring.data.foo`
58-
* file://<PATH>/src/main/resources/application-another.properties[`src/main/resources/application-another.properties`]
64+
* %s[`%s`]
5965
** `spring.data.here`
6066
6167
==== Remediation
6268
Either add `spring-data` dependency, rename the property or remove it in case it's not required anymore.
6369
6470
65-
""");
71+
""".formatted(
72+
projectRoot.resolve(props1).toUri(),
73+
props1,
74+
projectRoot.resolve(props2).toUri(),
75+
props2
76+
77+
)
78+
);
6679
}
6780

6881
@Test

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/SpringFactoriesHelperTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.springframework.sbm.engine.context.ProjectContext;
2020
import org.springframework.sbm.project.resource.TestProjectContext;
2121

22+
import java.nio.file.Path;
23+
2224
import static org.assertj.core.api.Assertions.assertThat;
2325

2426
class SpringFactoriesHelperTest {
@@ -43,6 +45,9 @@ public void detectsFileWithSpringFactories() {
4345

4446
assertThat(sut.getData()).isNotNull();
4547
assertThat(sut.getData().get("files")).hasSize(1);
46-
assertThat(sut.getData().get("files").get(0)).contains("src/main/resources/META-INF/spring.factories");
48+
49+
Path actual = context.getProjectRootDirectory().resolve(Path.of("src/main/resources/META-INF/spring.factories"));
50+
Path expected = Path.of(sut.getData().get("files").get(0));
51+
assertThat(expected).isEqualTo(actual);
4752
}
4853
}

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/SpringMVCAndWebFluxUrlMatchingChangesReportSectionTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
165165
166166
167167
""".formatted(
168-
projectRoot.resolve(restController1Path).toUri(),
169-
restController1Path,
168+
// Note the controller order here, sorted by package / absolute path
170169
projectRoot.resolve(restController2Path).toUri(),
171-
restController2Path
170+
restController2Path,
171+
projectRoot.resolve(restController1Path).toUri(),
172+
restController1Path
172173
);
173174

174175
SpringBootUpgradeReportTestSupport.generatedSection("Spring MVC and WebFlux URL matching changes")

docs/boot-3.0.0-M3-upgrade.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ All variables used in the template must be provided by `getData`.
6161
The scan found properties with `spring.data` prefix but no dependency matching `org.springframework.data:.*`.
6262
6363
<#list matches as match> <2>
64-
* file://${match.absolutePath}[`${match.relativePath}`]
64+
* ${match.absolutePath.toUri()}[`${match.relativePath}`]
6565
<#list match.propertiesFound as property>
6666
** `${property}`
6767
</#list>
@@ -232,7 +232,7 @@ https://github.com/Buzzardo/spring-style-guide/blob/master/spring-style-guide.ad
232232
The scan found properties with `spring.data` prefix but no dependency matching `org.springframework.data:.*`.
233233
234234
<#list matches as match>
235-
* file://${match.absolutePath}[`${match.relativePath}`]
235+
* ${match.absolutePath.toUri()}[`${match.relativePath}`]
236236
<#list match.propertiesFound as property>
237237
** `${property}`
238238
</#list>

0 commit comments

Comments
 (0)