Search in sources :

Example 1 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.

the class FreeMarkerConfigurationFactoryBeanTests method freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
    fcfb.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    fcfb.setFreemarkerSettings(settings);
    fcfb.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(String location) {
            if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
                throw new IllegalArgumentException(location);
            }
            return new ByteArrayResource("test".getBytes(), "test");
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    fcfb.afterPropertiesSet();
    assertThat(fcfb.getObject()).isInstanceOf(Configuration.class);
    Configuration fc = fcfb.getObject();
    Template ft = fc.getTemplate("test");
    assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Resource(cn.taketoday.core.io.Resource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.

the class FreeMarkerConfigurerTests method freeMarkerConfigurerWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurerWithNonFileResourceLoaderPath() throws Exception {
    freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    freeMarkerConfigurer.setFreemarkerSettings(settings);
    freeMarkerConfigurer.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(String location) {
            if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
                throw new IllegalArgumentException(location);
            }
            return new ByteArrayResource("test".getBytes(), "test");
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    freeMarkerConfigurer.afterPropertiesSet();
    assertThat(freeMarkerConfigurer.getConfiguration()).isInstanceOf(Configuration.class);
    Configuration fc = freeMarkerConfigurer.getConfiguration();
    Template ft = fc.getTemplate("test");
    assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Resource(cn.taketoday.core.io.Resource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentPostProcessorTests method postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance.

@Test
void postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance() {
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    this.application.setResourceLoader(resourceLoader);
    willReturn(this.configDataEnvironment).given(this.postProcessor).getConfigDataEnvironment(any(), any(), any());
    this.postProcessor.postProcessEnvironment(this.environment, this.application);
    then(this.postProcessor).should().getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any());
    then(this.configDataEnvironment).should().processAndApply();
    assertThat(this.resourceLoaderCaptor.getValue()).isSameAs(resourceLoader);
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.

the class ApplicationTests method customResourceLoaderFromConstructor.

@Test
void customResourceLoaderFromConstructor() {
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    TestApplication application = new TestApplication(resourceLoader, ExampleWebConfig.class);
    this.context = application.run();
    then(application.getLoader()).should().setResourceLoader(resourceLoader);
}
Also used : DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) ResourceLoader(cn.taketoday.core.io.ResourceLoader) DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-infrastructure by TAKETODAY.

the class ApplicationTests method customResourceLoader.

@Test
void customResourceLoader() {
    TestApplication application = new TestApplication(ExampleConfig.class);
    application.setApplicationType(ApplicationType.NONE_WEB);
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    application.setResourceLoader(resourceLoader);
    this.context = application.run();
    then(application.getLoader()).should().setResourceLoader(resourceLoader);
}
Also used : DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) ResourceLoader(cn.taketoday.core.io.ResourceLoader) DefaultResourceLoader(cn.taketoday.core.io.DefaultResourceLoader) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceLoader (cn.taketoday.core.io.ResourceLoader)22 Test (org.junit.jupiter.api.Test)14 DefaultResourceLoader (cn.taketoday.core.io.DefaultResourceLoader)12 ByteArrayResource (cn.taketoday.core.io.ByteArrayResource)8 Resource (cn.taketoday.core.io.Resource)6 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 FileSystemResource (cn.taketoday.core.io.FileSystemResource)4 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 BeanWrapper (cn.taketoday.beans.BeanWrapper)2 BeansException (cn.taketoday.beans.BeansException)2 PropertyValues (cn.taketoday.beans.PropertyValues)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)2 Environment (cn.taketoday.core.env.Environment)2 PropertySources (cn.taketoday.core.env.PropertySources)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2 FileSystemResourceLoader (cn.taketoday.core.io.FileSystemResourceLoader)2