Search in sources :

Example 36 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project n2o-framework by i-novus-llc.

the class DataControllerTestBase method setUp.

@Before
public void setUp() {
    N2oEnvironment environment = new N2oEnvironment();
    environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
    environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("n2o_messages", "messages");
    messageSource.setDefaultEncoding("UTF-8");
    environment.setMessageSource(new MessageSourceAccessor(messageSource));
    OverrideProperties properties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
    properties.put("n2o.engine.mapper", "spel");
    SimplePropertyResolver propertyResolver = new SimplePropertyResolver(properties);
    setUpStaticProperties(propertyResolver);
    environment.setSystemProperties(propertyResolver);
    builder = new N2oApplicationBuilder(environment);
    configure(builder);
    CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
Also used : N2oEnvironment(net.n2oapp.framework.config.compile.pipeline.N2oEnvironment) PersisterFactoryByMap(net.n2oapp.framework.config.selective.persister.PersisterFactoryByMap) MessageSourceAccessor(org.springframework.context.support.MessageSourceAccessor) N2oApplicationBuilder(net.n2oapp.framework.config.N2oApplicationBuilder) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) OverrideProperties(net.n2oapp.properties.OverrideProperties) SimplePropertyResolver(net.n2oapp.framework.config.test.SimplePropertyResolver) Before(org.junit.Before)

Example 37 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project n2o-framework by i-novus-llc.

the class IOProcessorTest method testProps.

@Test
public void testProps() throws Exception {
    // test properties
    ReaderFactoryByMap readerFactory = new ReaderFactoryByMap();
    readerFactory.register(new BodyNamespaceEntityIO());
    IOProcessorImpl p = new IOProcessorImpl(readerFactory);
    Properties properties = new Properties();
    properties.setProperty("testProp1", "testProp1");
    PropertyResolver systemProperties = new SimplePropertyResolver(properties);
    p.setSystemProperties(systemProperties);
    testElementWithProperty(p, "testProp1");
    // test params
    HashMap<String, String> params = new HashMap<>();
    params.put("testProp1", "testProp2");
    p = new IOProcessorImpl(readerFactory);
    try {
        MetadataParamHolder.setParams(params);
        testElementWithProperty(p, "testProp2");
    } finally {
        MetadataParamHolder.setParams(null);
    }
    // test messages
    p = new IOProcessorImpl(readerFactory);
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("test_messages");
    messageSource.setDefaultEncoding("UTF-8");
    p.setMessageSourceAccessor(new MessageSourceAccessor(messageSource));
    testElementWithProperty(p, "testProp3");
    // test override
    p = new IOProcessorImpl(readerFactory);
    p.setSystemProperties(systemProperties);
    p.setMessageSourceAccessor(new MessageSourceAccessor(messageSource));
    try {
        MetadataParamHolder.setParams(params);
        // самый приоритетный params
        testElementWithProperty(p, "testProp2");
    } finally {
        MetadataParamHolder.setParams(null);
    }
    // test fail fast
    p = new IOProcessorImpl(readerFactory);
    p.setFailFast(true);
    // p.setSystemProperties(systemProperties); not set to fail
    try {
        testElementWithProperty(p, "testProp1");
        fail();
    } catch (N2oException ignored) {
    }
    // test fail tolerance
    p = new IOProcessorImpl(readerFactory);
    p.setFailFast(false);
    // p.setSystemProperties(systemProperties); not set to fail
    try {
        Element in = dom("net/n2oapp/framework/config/io/ioprocessor22.xml");
        BaseEntity baseEntity = new BaseEntity();
        p.element(in, "attr", baseEntity::getAttr, baseEntity::setAttr);
        assertThat(baseEntity.getAttr(), equalTo("${testProp1}"));
    } catch (N2oException e) {
        fail();
    }
}
Also used : N2oException(net.n2oapp.framework.api.exception.N2oException) HashMap(java.util.HashMap) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) Properties(java.util.Properties) PropertyResolver(org.springframework.core.env.PropertyResolver) SimplePropertyResolver(net.n2oapp.framework.config.test.SimplePropertyResolver) MessageSourceAccessor(org.springframework.context.support.MessageSourceAccessor) SimplePropertyResolver(net.n2oapp.framework.config.test.SimplePropertyResolver) Test(org.junit.Test)

Example 38 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project n2o-framework by i-novus-llc.

the class N2oTestBase method setUp.

public void setUp() throws Exception {
    N2oEnvironment environment = new N2oEnvironment();
    environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
    environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.addBasenames("n2o_api_messages", "n2o_api_messages", "n2o_config_messages", "test_messages", "messages");
    messageSource.setDefaultEncoding("UTF-8");
    Locale locale = new Locale("ru");
    LocaleContextHolder.setLocale(locale);
    environment.setMessageSource(new MessageSourceAccessor(messageSource));
    OverrideProperties n2oProperties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
    OverrideProperties appProperties = PropertiesReader.getPropertiesFromClasspath("application.properties");
    appProperties.setBaseProperties(n2oProperties);
    environment.setSystemProperties(new SimplePropertyResolver(appProperties));
    builder = new N2oApplicationBuilder(environment);
    configure(builder);
    CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
Also used : Locale(java.util.Locale) N2oEnvironment(net.n2oapp.framework.config.compile.pipeline.N2oEnvironment) PersisterFactoryByMap(net.n2oapp.framework.config.selective.persister.PersisterFactoryByMap) MessageSourceAccessor(org.springframework.context.support.MessageSourceAccessor) N2oApplicationBuilder(net.n2oapp.framework.config.N2oApplicationBuilder) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) OverrideProperties(net.n2oapp.properties.OverrideProperties)

Example 39 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project SpringMVCFinal by tonysodeep.

the class WebAppContextConfig method messageSource.

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource msg = new ResourceBundleMessageSource();
    msg.setBasename("messages");
    return msg;
}
Also used : ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) Bean(org.springframework.context.annotation.Bean)

Example 40 with ResourceBundleMessageSource

use of org.springframework.context.support.ResourceBundleMessageSource in project solarnetwork-node by SolarNetwork.

the class SolarBackupResourceProvider method messageSource.

private synchronized MessageSource messageSource() {
    if (messageSource != null) {
        return messageSource;
    }
    File f = new File(resourceBundleDir);
    try {
        ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
        ms.setBundleClassLoader(new URLClassLoader(new URL[] { f.toURI().toURL() }));
        ms.setBasename(name);
        messageSource = ms;
        return ms;
    } catch (MalformedURLException e) {
        log.warn("Error getting message bundle resource [{}]: {}", f, e.toString());
        return null;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) File(java.io.File) URL(java.net.URL)

Aggregations

ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)169 Bean (org.springframework.context.annotation.Bean)89 Before (org.junit.Before)16 MessageSourceAccessor (org.springframework.context.support.MessageSourceAccessor)11 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)9 ValidatorFactory (javax.validation.ValidatorFactory)6 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)6 PlatformResourceBundleLocator (org.hibernate.validator.resourceloading.PlatformResourceBundleLocator)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 IOException (java.io.IOException)5 List (java.util.List)5 Locale (java.util.Locale)5 ReaderFactoryByMap (net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap)5 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)5 MessageSource (org.springframework.context.MessageSource)5 ArrayList (java.util.ArrayList)4 N2oApplicationBuilder (net.n2oapp.framework.config.N2oApplicationBuilder)4 N2oEnvironment (net.n2oapp.framework.config.compile.pipeline.N2oEnvironment)4 PersisterFactoryByMap (net.n2oapp.framework.config.selective.persister.PersisterFactoryByMap)4