Merge branch '5.3.x'

# Conflicts:
#	spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java
This commit is contained in:
Sam Brannen 2022-07-05 13:27:09 +02:00
commit b61ac6315a
2 changed files with 71 additions and 53 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,9 +16,10 @@
package org.springframework.web.reactive.result.view.freemarker; package org.springframework.web.reactive.result.view.freemarker;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -32,7 +33,6 @@ import reactor.core.publisher.Mono;
import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
@ -45,6 +45,7 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.testfixture.server.MockServerWebExchange; import org.springframework.web.testfixture.server.MockServerWebExchange;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -66,12 +67,17 @@ public class FreeMarkerMacroTests {
private Configuration freeMarkerConfig; private Configuration freeMarkerConfig;
private Path templateLoaderPath;
@BeforeEach @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
this.templateLoaderPath = Files.createTempDirectory("webflux-").toAbsolutePath();
this.applicationContext.refresh(); this.applicationContext.refresh();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir")); configurer.setTemplateLoaderPaths("classpath:/", "file://" + this.templateLoaderPath);
this.freeMarkerConfig = configurer.createConfiguration(); this.freeMarkerConfig = configurer.createConfiguration();
} }
@ -333,13 +339,8 @@ public class FreeMarkerMacroTests {
return getOutput(); return getOutput();
} }
private String fetchMacro(String name) throws Exception { private static String fetchMacro(String name) throws Exception {
ClassPathResource resource = new ClassPathResource(TEMPLATE_FILE, getClass()); for (String macro : loadMacros()) {
assertThat(resource.exists()).isTrue();
String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
all = all.replace("\r\n", "\n");
String[] macros = StringUtils.delimitedListToStringArray(all, "\n\n");
for (String macro : macros) {
if (macro.startsWith(name)) { if (macro.startsWith(name)) {
return macro.substring(macro.indexOf("\n")).trim(); return macro.substring(macro.indexOf("\n")).trim();
} }
@ -347,9 +348,17 @@ public class FreeMarkerMacroTests {
return null; return null;
} }
private static String[] loadMacros() throws IOException {
ClassPathResource resource = new ClassPathResource(TEMPLATE_FILE, FreeMarkerMacroTests.class);
assertThat(resource.exists()).isTrue();
String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
all = all.replace("\r\n", "\n");
return StringUtils.delimitedListToStringArray(all, "\n\n");
}
private void storeTemplateInTempDir(String macro) throws IOException { private void storeTemplateInTempDir(String macro) throws IOException {
FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl"); Files.write(this.templateLoaderPath.resolve("tmp.ftl"),
FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath())); ("<#import \"spring.ftl\" as spring />\n" + macro).getBytes(UTF_8));
} }
private List<String> getOutput() { private List<String> getOutput() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,10 @@
package org.springframework.web.servlet.view.freemarker; package org.springframework.web.servlet.view.freemarker;
import java.io.FileWriter; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -25,7 +27,6 @@ import freemarker.template.Configuration;
import freemarker.template.SimpleHash; import freemarker.template.SimpleHash;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -33,7 +34,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
@ -48,6 +48,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse; import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.testfixture.servlet.MockServletContext; import org.springframework.web.testfixture.servlet.MockServletContext;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -60,34 +61,33 @@ public class FreeMarkerMacroTests {
private static final String TEMPLATE_FILE = "test.ftl"; private static final String TEMPLATE_FILE = "test.ftl";
private StaticWebApplicationContext wac; private final StaticWebApplicationContext wac = new StaticWebApplicationContext();
private MockHttpServletRequest request; private final MockServletContext servletContext = new MockServletContext();
private MockHttpServletResponse response; private final MockHttpServletRequest request = new MockHttpServletRequest();
private FreeMarkerConfigurer fc; private final MockHttpServletResponse response = new MockHttpServletResponse();
private final FreeMarkerConfigurer fc = new FreeMarkerConfigurer();
private Path templateLoaderPath;
@BeforeEach @BeforeEach
public void setup() throws Exception { public void setUp() throws Exception {
ServletContext sc = new MockServletContext(); this.templateLoaderPath = Files.createTempDirectory("servlet-").toAbsolutePath();
wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
// final Template expectedTemplate = new Template(); fc.setTemplateLoaderPaths("classpath:/", "file://" + this.templateLoaderPath);
fc = new FreeMarkerConfigurer();
fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
fc.afterPropertiesSet(); fc.afterPropertiesSet();
wac.setServletContext(servletContext);
wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc); wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
wac.refresh(); wac.refresh();
request = new MockHttpServletRequest();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
response = new MockHttpServletResponse();
} }
@ -280,9 +280,7 @@ public class FreeMarkerMacroTests {
private String getMacroOutput(String name) throws Exception { private String getMacroOutput(String name) throws Exception {
String macro = fetchMacro(name); String macro = fetchMacro(name);
assertThat(macro).isNotNull(); assertThat(macro).isNotNull();
storeTemplateInTempDir(macro);
FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");
FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));
DummyMacroRequestContext rc = new DummyMacroRequestContext(request); DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
Map<String, String> msgMap = new HashMap<>(); Map<String, String> msgMap = new HashMap<>();
@ -322,23 +320,15 @@ public class FreeMarkerMacroTests {
view.setUrl("tmp.ftl"); view.setUrl("tmp.ftl");
view.setExposeSpringMacroHelpers(false); view.setExposeSpringMacroHelpers(false);
view.setConfiguration(config); view.setConfiguration(config);
view.setServletContext(new MockServletContext()); view.setServletContext(servletContext);
view.render(model, request, response); view.render(model, request, response);
// tokenize output and ignore whitespace return getOutput();
String output = response.getContentAsString();
output = output.replace("\r\n", "\n");
return output.trim();
} }
private String fetchMacro(String name) throws Exception { private static String fetchMacro(String name) throws Exception {
ClassPathResource resource = new ClassPathResource("test.ftl", getClass()); for (String macro : loadMacros()) {
assertThat(resource.exists()).isTrue();
String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
all = all.replace("\r\n", "\n");
String[] macros = StringUtils.delimitedListToStringArray(all, "\n\n");
for (String macro : macros) {
if (macro.startsWith(name)) { if (macro.startsWith(name)) {
return macro.substring(macro.indexOf("\n")).trim(); return macro.substring(macro.indexOf("\n")).trim();
} }
@ -346,4 +336,23 @@ public class FreeMarkerMacroTests {
return null; return null;
} }
private static String[] loadMacros() throws IOException {
ClassPathResource resource = new ClassPathResource("test.ftl", FreeMarkerMacroTests.class);
assertThat(resource.exists()).isTrue();
String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream()));
all = all.replace("\r\n", "\n");
return StringUtils.delimitedListToStringArray(all, "\n\n");
}
private void storeTemplateInTempDir(String macro) throws IOException {
Files.write(this.templateLoaderPath.resolve("tmp.ftl"),
("<#import \"spring.ftl\" as spring />\n" + macro).getBytes(UTF_8));
}
private String getOutput() throws IOException {
String output = response.getContentAsString();
output = output.replace("\r\n", "\n").replaceAll(" +"," ");
return output.trim();
}
} }