Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jun 23, 2024
1 parent 6b456b6 commit 5f765fc
Showing 1 changed file with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
Expand All @@ -48,16 +47,25 @@
*/
class ViewResolutionIntegrationTests {

private static final String EXPECTED_BODY = "<html><body>Hello World!</body></html>";


@Test
void freemarker() throws Exception {
MockHttpServletResponse response = runTest(FreeMarkerWebConfig.class);
assertThat(response.getContentAsString()).isEqualTo("<html><body>Hello World!</body></html>");
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}

@Test // SPR-12013
void freemarkerWithExistingViewResolver() throws Exception {
MockHttpServletResponse response = runTest(ExistingViewResolverConfig.class);
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}

@Test
void groovyMarkup() throws Exception {
MockHttpServletResponse response = runTest(GroovyMarkupWebConfig.class);
assertThat(response.getContentAsString()).isEqualTo("<html><body>Hello World!</body></html>");
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}

@Test
Expand All @@ -74,14 +82,6 @@ void groovyMarkupInvalidConfig() {
.withMessageContaining("In addition to a Groovy markup view resolver ");
}

// SPR-12013

@Test
void existingViewResolver() throws Exception {
MockHttpServletResponse response = runTest(ExistingViewResolverConfig.class);
assertThat(response.getContentAsString()).isEqualTo("<html><body>Hello World!</body></html>");
}


private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
String basePath = "org/springframework/web/servlet/config/annotation";
Expand All @@ -104,7 +104,7 @@ private MockHttpServletResponse runTest(Class<?> configClass) throws ServletExce
@Controller
static class SampleController {

@RequestMapping(value = "/", method = RequestMethod.GET)
@GetMapping
public String sample(ModelMap model) {
model.addAttribute("hello", "Hello World!");
return "index";
Expand Down Expand Up @@ -136,6 +136,25 @@ public FreeMarkerConfigurer freeMarkerConfigurer() {
}
}

/**
* Test @EnableWebMvc in the presence of a pre-existing ViewResolver.
*/
@Configuration
static class ExistingViewResolverConfig extends AbstractWebConfig {

@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
return new FreeMarkerViewResolver("", ".ftl");
}

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
return configurer;
}
}

@Configuration
static class GroovyMarkupWebConfig extends AbstractWebConfig {

Expand Down Expand Up @@ -170,23 +189,4 @@ public void configureViewResolvers(ViewResolverRegistry registry) {
}
}

/**
* Test @EnableWebMvc in the presence of pre-existing ViewResolver.
*/
@Configuration
static class ExistingViewResolverConfig extends AbstractWebConfig {

@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
return new FreeMarkerViewResolver("", ".ftl");
}

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
return configurer;
}
}

}

0 comments on commit 5f765fc

Please sign in to comment.