Search in sources :

Example 11 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-framework 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 12 with ResourceLoader

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

the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenUsingCustomResourceLoader.

@Test
void runWhenUsingCustomResourceLoader() {
    this.application.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(String location) {
            if (location.equals("classpath:/custom.properties")) {
                return new ByteArrayResource("the.property: fromcustom".getBytes(), location);
            }
            return new ClassPathResource("doesnotexist");
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    ConfigurableApplicationContext context = this.application.run("--context.config.name=custom");
    String property = context.getEnvironment().getProperty("the.property");
    assertThat(property).isEqualTo("fromcustom");
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) ResourceLoader(cn.taketoday.core.io.ResourceLoader) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 13 with ResourceLoader

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

the class TestPropertySourceUtilsTests method addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile.

@Test
void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() {
    ConfigurableEnvironment environment = new MockEnvironment();
    PropertySources propertySources = environment.getPropertySources();
    propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
    assertThat(propertySources.size()).isEqualTo(0);
    String pair = "key = value";
    ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
    ResourceLoader resourceLoader = mock(ResourceLoader.class);
    given(resourceLoader.getResource(anyString())).willReturn(resource);
    addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
    assertThat(propertySources.size()).isEqualTo(1);
    assertThat(environment.getProperty("key")).isEqualTo("value");
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) TestPropertySourceUtils.buildMergedTestPropertySources(cn.taketoday.test.context.support.TestPropertySourceUtils.buildMergedTestPropertySources) PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) MockEnvironment(cn.taketoday.mock.env.MockEnvironment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 14 with ResourceLoader

use of cn.taketoday.core.io.ResourceLoader in project today-framework 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)

Example 15 with ResourceLoader

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

the class OnResourceCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionEvaluationContext context, AnnotatedTypeMetadata metadata) {
    MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnResource.class.getName(), true);
    ResourceLoader loader = context.getResourceLoader();
    List<String> locations = new ArrayList<>();
    collectValues(locations, attributes.get("value"));
    Assert.isTrue(!locations.isEmpty(), "@ConditionalOnResource annotations must specify at least one resource location");
    ArrayList<String> missing = new ArrayList<>();
    for (String location : locations) {
        String resource = context.getEnvironment().resolvePlaceholders(location);
        if (!loader.getResource(resource).exists()) {
            missing.add(location);
        }
    }
    if (!missing.isEmpty()) {
        return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnResource.class).didNotFind("resource", "resources").items(Style.QUOTE, missing));
    }
    return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnResource.class).found("location", "locations").items(locations));
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) ArrayList(java.util.ArrayList)

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