Polish contribution

See gh-23073
This commit is contained in:
Sam Brannen 2019-06-08 22:20:58 +03:00
parent ab36a0a82f
commit 9b084bb3ea
2 changed files with 17 additions and 33 deletions

View File

@ -16,9 +16,13 @@
package org.springframework.ui.freemarker;
import java.util.HashMap;
import java.util.Properties;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.ByteArrayResource;
@ -27,42 +31,38 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.util.HashMap;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIOException;
/**
* @author Juergen Hoeller
* @author Issam El-atif
* @author Sam Brannen
*/
public class FreeMarkerConfigurationFactoryBeanTests {
private final FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
@Test
public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
fcfb.setConfigLocation(new FileSystemResource("myprops.properties"));
Properties props = new Properties();
props.setProperty("myprop", "/mydir");
fcfb.setFreemarkerSettings(props);
assertThatIOException().isThrownBy(
fcfb::afterPropertiesSet);
assertThatIOException().isThrownBy(fcfb::afterPropertiesSet);
}
@Test
public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception {
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
fcfb.setTemplateLoaderPath("file:/mydir");
fcfb.afterPropertiesSet();
Configuration cfg = fcfb.getObject();
assertThat(cfg.getTemplateLoader() instanceof SpringTemplateLoader).isTrue();
assertThat(cfg.getTemplateLoader()).isInstanceOf(SpringTemplateLoader.class);
}
@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
fcfb.setTemplateLoaderPath("file:/mydir");
Properties settings = new Properties();
settings.setProperty("localized_lookup", "false");
@ -96,7 +96,7 @@ public class FreeMarkerConfigurationFactoryBeanTests {
RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class);
configDef.getPropertyValues().add("templateLoader", loaderDef);
beanFactory.registerBeanDefinition("freeMarkerConfig", configDef);
beanFactory.getBean(Configuration.class);
assertThat(beanFactory.getBean(Configuration.class)).isNotNull();
}
}

View File

@ -25,10 +25,7 @@ import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
@ -41,23 +38,23 @@ import static org.assertj.core.api.Assertions.assertThatIOException;
/**
* @author Juergen Hoeller
* @author Issam El-atif
* @author Sam Brannen
*/
public class FreeMarkerConfigurerTests {
private final FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
@Test
public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
public void freeMarkerConfigurerWithConfigLocation() throws Exception {
freeMarkerConfigurer.setConfigLocation(new FileSystemResource("myprops.properties"));
Properties props = new Properties();
props.setProperty("myprop", "/mydir");
freeMarkerConfigurer.setFreemarkerSettings(props);
assertThatIOException().isThrownBy(
freeMarkerConfigurer::afterPropertiesSet);
assertThatIOException().isThrownBy(freeMarkerConfigurer::afterPropertiesSet);
}
@Test
public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
public void freeMarkerConfigurerWithResourceLoaderPath() throws Exception {
freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
freeMarkerConfigurer.afterPropertiesSet();
Configuration cfg = freeMarkerConfigurer.getConfiguration();
@ -69,8 +66,7 @@ public class FreeMarkerConfigurerTests {
@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
public void freeMarkerConfigurerWithNonFileResourceLoaderPath() throws Exception {
freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
Properties settings = new Properties();
settings.setProperty("localized_lookup", "false");
@ -95,16 +91,4 @@ public class FreeMarkerConfigurerTests {
assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
@Test // SPR-12448
public void freeMarkerConfigurationAsBean() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RootBeanDefinition loaderDef = new RootBeanDefinition(SpringTemplateLoader.class);
loaderDef.getConstructorArgumentValues().addGenericArgumentValue(new DefaultResourceLoader());
loaderDef.getConstructorArgumentValues().addGenericArgumentValue("/freemarker");
RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class);
configDef.getPropertyValues().add("templateLoader", loaderDef);
beanFactory.registerBeanDefinition("freeMarkerConfig", configDef);
beanFactory.getBean(Configuration.class);
}
}