Skip to content

Commit 0ceeef0

Browse files
committed
Add withBootParentOf to TestProjectContext
- TestProjectContext supports creating pom.xml with spring-boot-parent of given version - removed unused variable
1 parent f38608e commit 0ceeef0

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ public static class Builder {
249249
private OpenRewriteMavenBuildFile mockedBuildFile;
250250
private DependencyHelper dependencyHelper = new DependencyHelper();
251251
private SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
252+
253+
private Optional<String> springVersion = Optional.empty();
254+
252255
private JavaParser javaParser;
253256

254257
public Builder(Path projectRoot) {
@@ -456,10 +459,22 @@ public ProjectContext serializeProjectContext(Path targetDir) {
456459
public ProjectContext build() {
457460
verifyValidBuildFileSetup();
458461

459-
if (dependencies != null && !dependencies.isEmpty()) {
460-
String generatedPomXml = renderPomXmlWithGivenDependencies();
461-
resourcesWithRelativePaths.put(Path.of("pom.xml"), generatedPomXml);
462-
}
462+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
463+
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
464+
" <modelVersion>4.0.0</modelVersion>\n" +
465+
"{{springParentPom}}" +
466+
" <groupId>com.example</groupId>\n" +
467+
" <artifactId>dummy-root</artifactId>\n" +
468+
" <version>0.1.0-SNAPSHOT</version>\n" +
469+
" <packaging>jar</packaging>\n" +
470+
"{{dependencies}}" +
471+
"</project>\n";
472+
473+
xml = xml
474+
.replace("{{dependencies}}", getDependenciesSection())
475+
.replace("{{springParentPom}}", getSpringParentPomSection());
476+
477+
resourcesWithRelativePaths.put(Path.of("pom.xml"), xml);
463478

464479
if (!containsAnyPomXml()) {
465480
withDummyRootBuildFile();
@@ -588,22 +603,9 @@ private Parser.Input createParserInput(Path path, String value) {
588603

589604

590605
@NotNull
591-
private String renderPomXmlWithGivenDependencies() {
592-
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
593-
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
594-
" <modelVersion>4.0.0</modelVersion>\n" +
595-
" <groupId>com.example</groupId>\n" +
596-
" <artifactId>dummy-root</artifactId>\n" +
597-
" <version>0.1.0-SNAPSHOT</version>\n" +
598-
" <packaging>jar</packaging>\n" +
599-
"{{dependencies}}\n" +
600-
"</project>\n";
601-
602-
String dependenciesText = null;
603-
if(dependencies.isEmpty()) {
604-
dependenciesText = "";
605-
} else {
606-
StringBuilder dependenciesSection = new StringBuilder();
606+
private String getDependenciesSection() {
607+
StringBuilder dependenciesSection = new StringBuilder();
608+
if(!dependencies.isEmpty()) {
607609
dependenciesSection.append(" ").append("<dependencies>").append("\n");
608610
dependencyHelper.mapCoordinatesToDependencies(dependencies).stream().forEach(dependency -> {
609611
dependenciesSection.append(" ").append(" ").append("<dependency>").append("\n");
@@ -613,11 +615,32 @@ private String renderPomXmlWithGivenDependencies() {
613615
dependenciesSection.append(" ").append(" ").append("</dependency>").append("\n");
614616
});
615617
dependenciesSection.append(" ").append("</dependencies>").append("\n");
616-
dependenciesText = dependenciesSection.toString();
617618
}
618619

619-
String buildFileSource = xml.replace("{{dependencies}}", dependenciesText);
620-
return buildFileSource;
620+
return dependenciesSection.toString();
621+
}
622+
623+
@NotNull
624+
private String getSpringParentPomSection() {
625+
626+
if (this.springVersion.isPresent()) {
627+
return """
628+
<parent>
629+
<groupId>org.springframework.boot</groupId>
630+
<artifactId>spring-boot-starter-parent</artifactId>
631+
<version>%s</version>
632+
<relativePath/>
633+
</parent>
634+
""".formatted(this.springVersion.get());
635+
}
636+
637+
return "";
638+
}
639+
640+
public Builder withSpringBootParentOf(String springVersion) {
641+
642+
this.springVersion = Optional.of(springVersion);
643+
return this;
621644
}
622645
}
623646

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ private void verify(Consumer<String> assertion) {
195195
* code of buttons to apply a recipe.
196196
*/
197197
private String replaceRecipeButtonCodeFromExpectedOutput(SpringBootUpgradeReportSection sectionUnderTest, String renderedSection) {
198-
StringBuilder sb = new StringBuilder();
199198
List<String> buttonCodes = new ArrayList<>();
200199
if(sectionUnderTest.getRemediation().getPossibilities().isEmpty()) {
201200
String recipe = sectionUnderTest.getRemediation().getRecipe();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public class A {}
165165
""";
166166

167167
ProjectContext context = TestProjectContext.buildProjectContext()
168+
.withSpringBootParentOf("2.7,1")
168169
.addJavaSource("src/main/java", javaClassWithReactiveSortingRepo)
169170
.addJavaSource("src/main/java",javaClassWithoutReactiveSortingRepo)
170171
.withBuildFileHavingDependencies("org.springframework.data:spring-data-commons:2.7.1")

0 commit comments

Comments
 (0)