Skip to content

Commit 5ce58d5

Browse files
ashakirinsanagaraj-pivotal
authored andcommitted
Improved error processing (#316)
Co-authored-by: sanagaraj-pivotal <sanagaraj@pivotal.io>
1 parent 7672d7e commit 5ce58d5

File tree

5 files changed

+83
-24
lines changed

5 files changed

+83
-24
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapter.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818

1919
import com.fasterxml.jackson.annotation.JsonIgnore;
2020
import lombok.*;
21-
import org.openrewrite.InMemoryExecutionContext;
2221
import org.openrewrite.Recipe;
23-
import org.openrewrite.Result;
24-
import org.openrewrite.SourceFile;
2522
import org.openrewrite.config.DeclarativeRecipe;
2623
import org.openrewrite.config.YamlResourceLoader;
2724
import org.springframework.beans.factory.annotation.Autowired;
2825
import org.springframework.sbm.engine.context.ProjectContext;
2926
import org.springframework.util.ReflectionUtils;
3027

3128
import java.io.ByteArrayInputStream;
32-
import java.io.IOException;
3329
import java.lang.reflect.InvocationTargetException;
3430
import java.lang.reflect.Method;
3531
import java.net.URI;
@@ -48,7 +44,7 @@ public class OpenRewriteDeclarativeRecipeAdapter extends AbstractAction {
4844

4945
@JsonIgnore
5046
@Autowired
51-
private RewriteMigrationResultMerger resultMerger;
47+
private OpenRewriteRecipeRunner openRewriteRecipeRunner;
5248

5349
@Override
5450
public boolean isApplicable(ProjectContext context) {
@@ -71,11 +67,7 @@ public void apply(ProjectContext context) {
7167
throw new RuntimeException(String.format("Ambiguous number of recipes found. Expected exactly one, found %s", rewriteYamlRecipe.size()));
7268
}
7369
Recipe recipe = rewriteYamlRecipe.iterator().next();
74-
List<? extends SourceFile> rewriteSourceFiles = context.search(new OpenRewriteSourceFilesFinder());
75-
List<Result> results = recipe.run(rewriteSourceFiles, new InMemoryExecutionContext((t) -> {
76-
throw new RuntimeException(t);
77-
}));
78-
resultMerger.mergeResults(context, results);
70+
openRewriteRecipeRunner.run(context, recipe);
7971
}
8072

8173
private void initializeRecipe(DeclarativeRecipe recipe) {

components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteNamedRecipeAdapter.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@
2222
import lombok.NoArgsConstructor;
2323
import lombok.Setter;
2424
import org.openrewrite.Recipe;
25-
import org.openrewrite.Result;
26-
import org.openrewrite.SourceFile;
2725
import org.springframework.beans.factory.annotation.Autowired;
2826
import org.springframework.sbm.engine.context.ProjectContext;
2927

30-
import java.util.List;
31-
3228
@Builder
3329
@NoArgsConstructor
3430
@AllArgsConstructor
@@ -43,13 +39,11 @@ public class OpenRewriteNamedRecipeAdapter extends AbstractAction {
4339

4440
@JsonIgnore
4541
@Autowired
46-
private RewriteMigrationResultMerger resultMerger;
42+
private OpenRewriteRecipeRunner openRewriteRecipeRunner;
4743

4844
@Override
4945
public void apply(ProjectContext context) {
5046
Recipe recipe = rewriteRecipeLoader.loadRewriteRecipe(openRewriteRecipeName);
51-
List<? extends SourceFile> rewriteSourceFiles = context.search(new OpenRewriteSourceFilesFinder());
52-
List<Result> results = recipe.run(rewriteSourceFiles);
53-
resultMerger.mergeResults(context, results);
47+
openRewriteRecipeRunner.run(context, recipe);
5448
}
5549
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.springframework.sbm.engine.recipe;
2+
3+
import org.openrewrite.InMemoryExecutionContext;
4+
import org.openrewrite.Recipe;
5+
import org.openrewrite.Result;
6+
import org.openrewrite.SourceFile;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.sbm.engine.context.ProjectContext;
9+
/*
10+
* Copyright 2021 - 2022 the original author or authors.
11+
*
12+
* Licensed under the Apache License, Version 2.0 (the "License");
13+
* you may not use this file except in compliance with the License.
14+
* You may obtain a copy of the License at
15+
*
16+
* https://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
import org.springframework.stereotype.Component;
26+
27+
import java.util.List;
28+
29+
@Component
30+
public class OpenRewriteRecipeRunner {
31+
@Autowired
32+
private RewriteMigrationResultMerger resultMerger;
33+
34+
public void run(ProjectContext context, Recipe recipe) {
35+
List<? extends SourceFile> rewriteSourceFiles = context.search(new OpenRewriteSourceFilesFinder());
36+
List<Result> results = recipe.run(rewriteSourceFiles, new InMemoryExecutionContext(
37+
(t) -> {
38+
throw new RuntimeException(t);
39+
}
40+
));
41+
resultMerger.mergeResults(context, results);
42+
}
43+
44+
}

components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapterTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616

1717
package org.springframework.sbm.engine.recipe;
1818

19-
import com.fasterxml.jackson.annotation.JsonIgnore;
20-
import lombok.Getter;
2119
import org.junit.jupiter.api.Test;
2220
import org.springframework.beans.factory.annotation.Autowired;
2321
import org.springframework.boot.test.context.SpringBootTest;
24-
import org.springframework.context.ApplicationEventPublisher;
2522
import org.springframework.sbm.engine.context.ProjectContext;
2623
import org.springframework.sbm.project.RewriteSourceFileWrapper;
2724
import org.springframework.sbm.project.resource.ResourceHelper;
2825
import org.springframework.sbm.project.resource.TestProjectContext;
29-
import org.springframework.stereotype.Component;
3026
import org.springframework.validation.beanvalidation.CustomValidatorBean;
3127

3228
import java.io.IOException;
@@ -41,6 +37,7 @@
4137
ResourceHelper.class,
4238
ActionDeserializerRegistry.class,
4339
RewriteMigrationResultMerger.class,
40+
OpenRewriteRecipeRunner.class,
4441
RewriteSourceFileWrapper.class,
4542
CustomValidatorBean.class
4643
})
@@ -88,7 +85,6 @@ void recipeFromYaml() throws IOException {
8885
);
8986
}
9087

91-
9288
@Test
9389
public void propagateExceptionFromOpenRewriteRecipe() throws IOException {
9490

@@ -123,6 +119,8 @@ public void propagateExceptionFromOpenRewriteRecipe() throws IOException {
123119
.addJavaSource("src/main/java", javaSource)
124120
.build();
125121

126-
assertThrows(RuntimeException.class, () -> recipeAdapter.apply(context));
122+
RuntimeException thrown = assertThrows(RuntimeException.class, () -> recipeAdapter.apply(context));
123+
124+
assertThat(thrown).hasRootCauseMessage("A problem happened whilst visiting");
127125
}
128126
}

components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteNamedRecipeAdapterTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929

3030
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
3132

3233
@SpringBootTest(classes = {
3334
RecipeParser.class,
@@ -36,6 +37,7 @@
3637
ResourceHelper.class,
3738
ActionDeserializerRegistry.class,
3839
RewriteMigrationResultMerger.class,
40+
OpenRewriteRecipeRunner.class,
3941
RewriteSourceFileWrapper.class,
4042
RewriteRecipeLoader.class,
4143
CustomValidatorBean.class
@@ -76,4 +78,33 @@ void recipeFromYaml() throws IOException {
7678
"}\n"
7779
);
7880
}
81+
82+
@Test
83+
public void propagateExceptionFromOpenRewriteRecipe() throws IOException {
84+
85+
String actionDescription =
86+
"- name: test-recipe\n" +
87+
" description: Replace deprecated spring.datasource.* properties\n" +
88+
" condition:\n" +
89+
" type: org.springframework.sbm.common.migration.conditions.TrueCondition\n" +
90+
" actions:\n" +
91+
" - type: org.springframework.sbm.engine.recipe.OpenRewriteNamedRecipeAdapter\n" +
92+
" description: Test recipe producing exception\n" +
93+
" openRewriteRecipeName: org.springframework.sbm.engine.recipe.ErrorClass\n";
94+
95+
Recipe[] recipes = recipeParser.parseRecipe(actionDescription);
96+
assertThat(recipes[0].getActions().get(0)).isInstanceOf(OpenRewriteNamedRecipeAdapter.class);
97+
OpenRewriteNamedRecipeAdapter recipeAdapter = (OpenRewriteNamedRecipeAdapter) recipes[0].getActions().get(0);
98+
99+
String javaSource = "@java.lang.Deprecated\n" +
100+
"public class Foo {}";
101+
102+
ProjectContext context = TestProjectContext.buildProjectContext()
103+
.addJavaSource("src/main/java", javaSource)
104+
.build();
105+
106+
RuntimeException thrown = assertThrows(RuntimeException.class, () -> recipeAdapter.apply(context));
107+
108+
assertThat(thrown).hasRootCauseMessage("A problem happened whilst visiting");
109+
}
79110
}

0 commit comments

Comments
 (0)