-
Notifications
You must be signed in to change notification settings - Fork 25
/
MvcConfigTest.java
142 lines (126 loc) · 7.09 KB
/
MvcConfigTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.example.web.config;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractResourceBasedMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.ISpringTemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
// TODO 3-22 このテストを実行して、MvcConfigの実装が正しいかチェックする(addResourceHandlers()のみ、実装の正しさをチェックできません。後ほどブラウザから実行してチェックします)
public class MvcConfigTest {
MvcConfig mvcConfig = new MvcConfig();
@Test
@DisplayName("WebMvcConfigurerを実装している")
public void classTest() {
assertTrue(mvcConfig instanceof WebMvcConfigurer);
}
@Test
@DisplayName("クラスに必要なアノテーションが付加されている")
public void annotationTest() {
EnableWebMvc enableWebMvc = MvcConfig.class.getAnnotation(EnableWebMvc.class);
Configuration configuration = MvcConfig.class.getAnnotation(Configuration.class);
ComponentScan componentScan = MvcConfig.class.getAnnotation(ComponentScan.class);
assertAll("",
() -> assertNotNull(enableWebMvc),
() -> assertNotNull(configuration),
() -> assertNotNull(componentScan),
() -> assertArrayEquals(new String[]{"com.example.web.controller"},
componentScan.basePackages())
);
}
@Test
@DisplayName("templateResolverのBeanが定義されている")
public void templateResolverTest() {
Method templateResolverMethod = assertDoesNotThrow(() -> MvcConfig.class.getMethod("templateResolver"));
Bean bean = templateResolverMethod.getAnnotation(Bean.class);
assertNotNull(bean);
SpringResourceTemplateResolver templateResolver = mvcConfig.templateResolver();
assertAll("templateResolver not configured correctly",
() -> assertEquals("classpath:/templates/", templateResolver.getPrefix()),
() -> assertEquals(".html", templateResolver.getSuffix()),
() -> assertEquals(TemplateMode.HTML, templateResolver.getTemplateMode()),
() -> assertEquals(StandardCharsets.UTF_8.name(),
templateResolver.getCharacterEncoding()),
() -> assertEquals(false, templateResolver.isCacheable())
);
}
@Test
@DisplayName("templateEngineのBeanが定義されている")
public void templateEngineTest() {
Method templateEngineMethod = assertDoesNotThrow(() -> MvcConfig.class.getMethod("templateEngine", SpringResourceTemplateResolver.class));
Bean bean = templateEngineMethod.getAnnotation(Bean.class);
assertNotNull(bean);
SpringResourceTemplateResolver templateResolver = mvcConfig.templateResolver();
SpringTemplateEngine templateEngine = mvcConfig.templateEngine(templateResolver);
Set<ITemplateResolver> templateResolvers = templateEngine.getTemplateResolvers();
String[] actualDialects = templateEngine.getDialects()
.stream()
.map(dialect -> dialect.getClass().getSimpleName())
.sorted()
.toArray(String[]::new);
assertAll("templateEngine not configured correctly",
() -> assertTrue(templateResolvers.contains(templateResolver)),
() -> assertTrue(templateEngine.getEnableSpringELCompiler()),
() -> assertArrayEquals(
new String[]{"Java8TimeDialect", "SpringSecurityDialect", "SpringStandardDialect"},
actualDialects)
);
}
@Test
@DisplayName("viewResolverのBeanが定義されている")
public void viewResolverTest() {
Method viewResolverMethod = assertDoesNotThrow(() -> MvcConfig.class.getMethod("viewResolver", SpringTemplateEngine.class));
Bean bean = viewResolverMethod.getAnnotation(Bean.class);
assertNotNull(bean);
ThymeleafViewResolver viewResolver = mvcConfig.viewResolver(mvcConfig.templateEngine(mvcConfig.templateResolver()));
ISpringTemplateEngine templateEngine = viewResolver.getTemplateEngine();
assertAll("viewResolver not configured correctly",
() -> assertTrue(templateEngine instanceof SpringTemplateEngine),
() -> assertEquals(StandardCharsets.UTF_8.name(), viewResolver.getCharacterEncoding())
);
}
@Test
@DisplayName("addResourceHandlers()の存在のみチェック(注意!実装の正しさはチェックしていません)")
public void addResourceHandlersTest() {
assertDoesNotThrow(() -> MvcConfig.class.getMethod("addResourceHandlers", ResourceHandlerRegistry.class));
}
@Test
@DisplayName("messageSourceのBeanが定義されている")
public void messageSourceTest() {
Method messageSourceMethod = assertDoesNotThrow(() -> MvcConfig.class.getMethod("messageSource"));
Bean bean = messageSourceMethod.getAnnotation(Bean.class);
assertNotNull(bean);
MessageSource messageSource = mvcConfig.messageSource();
assertTrue(messageSource instanceof ResourceBundleMessageSource);
ResourceBundleMessageSource resourceBundleMessageSource = (ResourceBundleMessageSource) messageSource;
assertAll("messageSource not configured correctly",
() -> assertTrue(resourceBundleMessageSource.getBasenameSet().contains("messages")),
() -> {
Field defaultEncodingField = AbstractResourceBasedMessageSource.class.getDeclaredField("defaultEncoding");
defaultEncodingField.setAccessible(true);
assertEquals(StandardCharsets.UTF_8.name(),
defaultEncodingField.get(resourceBundleMessageSource));
}
);
}
}