Skip to content

Commit

Permalink
Support more types in recipe descriptor loading from json
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Jan 24, 2023
1 parent c8b12a3 commit ff5adf8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* Copyright (c) 2022, 2023 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
Expand Down Expand Up @@ -114,29 +114,33 @@ private static Object getOptionValueForType(Class<?> type, OptionDescriptor opti
Object[] valueToSet = (Object[]) Array.newInstance(type.getComponentType(), arrayValue.size());
int k = 0;
for (Object v : arrayValue) {
valueToSet[k] = getOptionValue(type.getComponentType().getSimpleName(), v);
valueToSet[k] = getOptionValue(type.getComponentType(), v);
}
return valueToSet;
} else {
return getOptionValue(option.getType(), option.getValue());
return getOptionValue(type, option.getValue());
}
}

private static Object getOptionValue(String type, Object v) {
switch (type) {
case "int":
private static Object getOptionValue(Class<?> type, Object v) {
if (type.equals(int.class) || type.equals(Integer.class)) {
return ((Number) v).intValue();
case "long":
} else if (type.equals(long.class) || type.equals(Long.class)) {
return ((Number) v).longValue();
case "short":
} else if (type.equals(short.class) || type.equals(Short.class)) {
return ((Number) v).shortValue();
case "float":
} else if (type.equals(float.class) || type.equals(Float.class)) {
return ((Number) v).floatValue();
case "double":
return ((Number) v).doubleValue();
default:
return v;
} else if (type.equals(double.class) || type.equals(Double.class)) {
return ((Number) v).shortValue();
} else if (Enum.class.isAssignableFrom(type) && v instanceof String) {
try {
return type.getMethod("valueOf").invoke(v);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return v;
}

private static Object getPrimitiveDefault(Class<?> t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Duration;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openrewrite.Recipe;
import org.openrewrite.config.DeclarativeRecipe;
import org.openrewrite.config.Environment;
import org.openrewrite.config.RecipeDescriptor;
import org.openrewrite.maven.UpgradeDependencyVersion;
import org.springframework.ide.vscode.commons.rewrite.LoadUtils.DurationTypeConverter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class LoadUtilsTest {

private static Environment env;

private static Gson serializationGson = new GsonBuilder()
.registerTypeAdapter(Duration.class, new DurationTypeConverter())
.create();

@BeforeAll
public static void setupAll() {
env = Environment.builder().scanRuntimeClasspath().build();
Expand All @@ -35,8 +45,9 @@ public static void setupAll() {
@Test
public void createRecipeTest() throws Exception {
Recipe r = env.listRecipes().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
RecipeDescriptor recipeDescriptor = r.getDescriptor();
RecipeDescriptor recipeDescriptor = r.getDescriptor();
assertNotNull(recipeDescriptor);

r = LoadUtils.createRecipe(recipeDescriptor, id -> {
try {
return (Class<Recipe>) Class.forName(id);
Expand Down Expand Up @@ -70,4 +81,48 @@ public void createRecipeTest() throws Exception {
assertEquals("org.springframework.boot", upgradeDependencyRecipe.getGroupId());
assertEquals("*", upgradeDependencyRecipe.getArtifactId());
}

@SuppressWarnings("unchecked")
public void deserializeFromJson() throws Exception {
Recipe r = env.listRecipes().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
RecipeDescriptor recipeDescriptor = r.getDescriptor();
assertNotNull(recipeDescriptor);

String json = serializationGson.toJson(recipeDescriptor);
RecipeDescriptor deserialized = serializationGson.fromJson(json, RecipeDescriptor.class);
assertNotNull(deserialized);

r = LoadUtils.createRecipe(deserialized, id -> {
try {
return (Class<Recipe>) Class.forName(id);
} catch (ClassNotFoundException e) {
return null;
}
});

assertTrue(r instanceof DeclarativeRecipe);
assertEquals("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0", r.getName());
assertEquals(
"Migrate applications to the latest Spring Boot 3.0 release. This recipe will modify an application's build files, make changes to deprecated/preferred APIs, and migrate configuration settings that have changes between versions. This recipe will also chain additional framework migrations (Spring Framework, Spring Data, etc) that are required as part of the migration to Spring Boot 2.7.\n"
+ ""
+ "",
r.getDescription());
assertEquals("Migrate to Spring Boot 3.0", r.getDisplayName());
assertEquals(9, r.getRecipeList().size());

Recipe pomRecipe = r.getRecipeList().get(0);
assertTrue(pomRecipe instanceof DeclarativeRecipe);
assertEquals("org.openrewrite.java.spring.boot3.MavenPomUpgrade", pomRecipe.getName());
assertEquals("Upgrade Maven Pom to Spring Boot 3.0 from prior 2.x version.", pomRecipe.getDescription());
assertEquals("Upgrade Maven Pom to Spring Boot 3.0 from 2.x", pomRecipe.getDisplayName());
assertTrue(pomRecipe.getRecipeList().size() >= 3);

UpgradeDependencyVersion upgradeDependencyRecipe = pomRecipe.getRecipeList().stream().filter(UpgradeDependencyVersion.class::isInstance).map(UpgradeDependencyVersion.class::cast).findFirst().get();
assertEquals("org.openrewrite.maven.UpgradeDependencyVersion", upgradeDependencyRecipe.getName());
assertEquals("Upgrade Maven dependency version", upgradeDependencyRecipe.getDisplayName());
assertEquals(0, upgradeDependencyRecipe.getRecipeList().size());
assertTrue(upgradeDependencyRecipe.getNewVersion().startsWith("3.0."));
assertEquals("org.springframework.boot", upgradeDependencyRecipe.getGroupId());
assertEquals("*", upgradeDependencyRecipe.getArtifactId());
}
}

0 comments on commit ff5adf8

Please sign in to comment.