Skip to content

Commit ca2fb0c

Browse files
authored
Provide test helper for section builder tests (#332)
- extracts report section into its own template
1 parent 838e9bf commit ca2fb0c

File tree

5 files changed

+177
-24
lines changed

5 files changed

+177
-24
lines changed

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/checks/CommonsMultipartResolverSectionBuilder.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,37 @@ public boolean isApplicable(ProjectContext projectContext) {
4141
@Override
4242
public Section build(ProjectContext projectContext) {
4343
List<MatchingMethod> matchingMethods = finder.findMatches(projectContext);
44+
45+
46+
4447
return ChangeSection.RelevantChangeSection.builder()
4548
.title("`CommonsMultipartResolver` support has been removed")
4649
.paragraph("Support for Spring Framework’s `CommonsMultipartResolver` has been removed following its removal in Spring Framework 6")
4750
.relevanceSection()
48-
.paragraph("The scan found bean declarations of type `CommonsMultipartResolver`")
51+
.paragraph("The scan found bean declarations of type `CommonsMultipartResolver`.")
4952
.todoSection()
50-
.todoList(
51-
TodoList.builder()
52-
.todo(
53-
TodoList.Todo.builder()
54-
.text("Remove beans of type `CommonsMultipartResolver` and rely on Spring Boot auto-configuration")
55-
.build()
56-
)
53+
.paragraph("Remove beans of type `CommonsMultipartResolver` and rely on Spring Boot auto-configuration")
54+
.todoList(this.buildTodoList(matchingMethods))
55+
.build();
56+
}
57+
58+
private TodoList buildTodoList(List<MatchingMethod> matchingMethods) {
59+
TodoList.TodoListBuilder todoListBuilder = TodoList.builder();
60+
matchingMethods.forEach(m -> {
61+
todoListBuilder.todo(
62+
TodoList.Todo.builder()
63+
.text(String.format("Remove from class `%s`", m.getType().getFullyQualifiedName()))
64+
.build()
65+
);
66+
});
67+
return todoListBuilder.build();
68+
}
69+
70+
private TodoList createTodo(String s) {
71+
return TodoList.builder()
72+
.todo(
73+
TodoList.Todo.builder()
74+
.text(String.format("Remove from class `%s`", s))
5775
.build()
5876
)
5977
.build();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<#import "todo.ftl" as todo>
2+
<#import "relevance.ftl" as relevance>
3+
<#import "paragraph.ftl" as p/>
4+
<#--<#macro section sections>-->
5+
<#if sections?has_content>
6+
<#list sections as changeSection>
7+
=== ${changeSection.title}
8+
<@p.paragraph changeSection.paragraphs/>
9+
<#if changeSection.relevanceSection?has_content>
10+
<@relevance.relevance changeSection.relevanceSection/>
11+
</#if>
12+
<#if changeSection.todoSection?has_content>
13+
<@todo.todos changeSection.todoSection/>
14+
</#if>
15+
</#list>
16+
<#else>
17+
No Changes found.
18+
</#if>
19+
<#--</#macro>-->

components/sbm-recipes-boot-upgrade/src/main/resources/templates/upgrade-asciidoc.ftl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,5 @@ apply boot-2.4-2.5-upgrade-report
6868

6969
== Relevant Changes
7070

71-
<#if changeSections?has_content>
72-
<#list changeSections as changeSection>
73-
=== ${changeSection.title}
74-
<@p.paragraph changeSection.paragraphs/>
75-
<#if changeSection.relevanceSection?has_content>
76-
<@relevance.relevance changeSection.relevanceSection/>
77-
</#if>
78-
<#if changeSection.todoSection?has_content>
79-
<@todo.todos changeSection.todoSection/>
80-
</#if>
81-
82-
83-
</#list>
84-
<#else>
85-
No Changes found.
86-
</#if>
71+
<#-- Bad practice? How to import another rendered template? -->
72+
<#include "section.ftl"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.boot.upgrade_27_30.checks;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.sbm.boot.asciidoctor.Section;
21+
import org.springframework.sbm.boot.common.finder.MatchingMethod;
22+
import org.springframework.sbm.engine.context.ProjectContext;
23+
import org.springframework.sbm.java.api.Type;
24+
25+
import java.util.List;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.mockito.ArgumentMatchers.matches;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.when;
31+
32+
class CommonsMultipartResolverSectionBuilderTest {
33+
34+
@Test
35+
void test_renameMe() {
36+
CommonsMultipartResolverBeanFinder finder = mock(CommonsMultipartResolverBeanFinder.class);
37+
38+
Type type = mock(Type.class);
39+
Type type2 = mock(Type.class);
40+
41+
ProjectContext context = mock(ProjectContext.class);
42+
CommonsMultipartResolverSectionBuilder sut = new CommonsMultipartResolverSectionBuilder(finder);
43+
List<MatchingMethod> matches = List.of(
44+
new MatchingMethod(null, type, null),
45+
new MatchingMethod(null, type2, null)
46+
);
47+
48+
when(finder.findMatches(context)).thenReturn(matches);
49+
when(type.getFullyQualifiedName()).thenReturn("com.foo.bar.SomeClass");
50+
when(type2.getFullyQualifiedName()).thenReturn("com.foo.baz.AnotherClass");
51+
52+
Section section = sut.build(context);
53+
54+
String rendered = SectionRendererTestUtil.render(section);
55+
assertThat(rendered).isEqualTo(
56+
"""
57+
=== `CommonsMultipartResolver` support has been removed
58+
Support for Spring Framework’s `CommonsMultipartResolver` has been removed following its removal in Spring Framework 6
59+
60+
==== Relevance
61+
62+
The scan found bean declarations of type `CommonsMultipartResolver`.
63+
64+
==== Todo
65+
66+
Remove beans of type `CommonsMultipartResolver` and rely on Spring Boot auto-configuration
67+
68+
69+
* [ ] Remove from class `com.foo.bar.SomeClass`
70+
* [ ] Remove from class `com.foo.baz.AnotherClass`
71+
"""
72+
);
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.boot.upgrade_27_30.checks;
18+
19+
import freemarker.cache.FileTemplateLoader;
20+
import freemarker.template.Configuration;
21+
import freemarker.template.Template;
22+
import freemarker.template.Version;
23+
import org.springframework.sbm.boot.asciidoctor.Section;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.io.StringWriter;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
public class SectionRendererTestUtil {
33+
public static String render(Section section) {
34+
Version version = new Version("2.3.0");
35+
Configuration configuration = new Configuration(version);
36+
try {
37+
configuration.setTemplateLoader(new FileTemplateLoader(new File("./src/main/resources/templates")));
38+
Map<String, Object> params = new HashMap<>();
39+
params.put("sections", List.of(section));
40+
// params.put("className", className);
41+
42+
StringWriter writer = new StringWriter();
43+
try {
44+
Template template = configuration.getTemplate("section.ftl");
45+
template.process(params, writer);
46+
String output = writer.toString();
47+
return output;
48+
} catch (Exception e) {
49+
throw new RuntimeException(e);
50+
}
51+
} catch (IOException e) {
52+
throw new RuntimeException(e);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)