diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java index 01c83b5a0..86490d2e0 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java @@ -10,7 +10,9 @@ *******************************************************************************/ package org.springframework.ide.vscode.boot.java.rewrite; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -75,19 +77,30 @@ public class SpringBootUpgrade { }); } - private Recipe createUpgradeRecipe(int major, int minor, int targetMajor, int targetMinor) { - Recipe recipe = new DeclarativeRecipe("upgrade-spring-boot", "Upgrade Spring Boot from " + createVersionString(major, minor) + " to " + createVersionString(targetMajor, targetMinor), - "", Collections.emptySet(), null, null, false); - for (int currentMajor = major, currentMinor = minor + 1; targetMajor != currentMajor && currentMinor != targetMinor;) { + static List createRecipeIdsChain(int major, int minor, int targetMajor, int targetMinor) { + List ids = new ArrayList<>(); + for (int currentMajor = major, currentMinor = minor + 1; targetMajor > currentMajor || (targetMajor == currentMajor && currentMinor <= targetMinor);) { String recipeId = VERSION_TO_RECIPE_ID.get(createVersionString(currentMajor, currentMinor)); if (recipeId == null) { currentMajor++; currentMinor = 0; } else { - recipeRepo.getRecipe(recipeId).ifPresent(recipe::doNext); + ids.add(recipeId); currentMinor++; } } + return ids; + } + + private Recipe createUpgradeRecipe(int major, int minor, int targetMajor, int targetMinor) { + Recipe recipe = new DeclarativeRecipe("upgrade-spring-boot", "Upgrade Spring Boot from " + createVersionString(major, minor) + " to " + createVersionString(targetMajor, targetMinor), + "", Collections.emptySet(), null, null, false); + + createRecipeIdsChain(major, minor, targetMajor, targetMinor).stream() + .map(recipeRepo::getRecipe) + .filter(o -> o.isPresent()) + .forEach(o -> recipe.doNext(o.get())); + if (recipe.getRecipeList().isEmpty()) { throw new IllegalStateException("No upgrade recipes found!"); } else if (recipe.getRecipeList().size() == 1) { diff --git a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgradeTest.java b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgradeTest.java new file mode 100644 index 000000000..07d949f7f --- /dev/null +++ b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgradeTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2022 VMware, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VMware, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.vscode.boot.java.rewrite; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import org.junit.Test; + +public class SpringBootUpgradeTest { + + @Test + public void recipeIdChain1() throws Exception { + assertEquals(List.of( + "org.openrewrite.java.spring.boot2.SpringBoot1To2Migration", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_1", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_2", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_3", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_4", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_5" + ), SpringBootUpgrade.createRecipeIdsChain(1, 3, 2, 5)); + } + + @Test + public void recipeIdChain2() throws Exception { + assertEquals(List.of( + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_3", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_4", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_5", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_6", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7" + ), SpringBootUpgrade.createRecipeIdsChain(2, 2, 2, 7)); + } + + @Test + public void recipeIdChain3() throws Exception { + assertEquals(List.of( + "org.openrewrite.java.spring.boot2.SpringBoot1To2Migration", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_1", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_2", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_3", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_4", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_5", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_6", + "org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7", + "org.springframework.sts.java.spring.boot3.UpgradeSpringBoot_3_0" + ), SpringBootUpgrade.createRecipeIdsChain(1, 3, 3, 0)); + } + + @Test + public void recipeIdChain4() throws Exception { + assertEquals(List.of( + ), SpringBootUpgrade.createRecipeIdsChain(2, 2, 2, 2)); + } + + @Test + public void recipeIdChain5() throws Exception { + assertEquals(List.of( + ), SpringBootUpgrade.createRecipeIdsChain(2, 7, 2, 2)); + } +} +