use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.
the class StaticMessageSourceTests method createContext.
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
StaticApplicationContext parent = new StaticApplicationContext();
Map<String, String> m = new HashMap<>();
m.put("name", "Roderick");
parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));
m.put("name", "Albert");
parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));
parent.refresh();
parent.addApplicationListener(parentListener);
this.sac = new StaticApplicationContext(parent);
sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
sac.refresh();
sac.addApplicationListener(listener);
StaticMessageSource messageSource = sac.getStaticMessageSource();
Map<String, String> usMessages = new HashMap<>(3);
usMessages.put("message.format.example1", MSG_TXT1_US);
usMessages.put("message.format.example2", MSG_TXT2_US);
usMessages.put("message.format.example3", MSG_TXT3_US);
messageSource.addMessages(usMessages, Locale.US);
messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK);
return sac;
}
use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.
the class ResourceBundleMessageSourceTests method doTestMessageAccess.
protected void doTestMessageAccess(boolean reloadable, boolean fallbackToSystemLocale, boolean expectGermanFallback, boolean useCodeAsDefaultMessage, boolean alwaysUseMessageFormat) {
StaticApplicationContext ac = new StaticApplicationContext();
if (reloadable) {
StaticApplicationContext parent = new StaticApplicationContext();
parent.refresh();
ac.setParent(parent);
}
MutablePropertyValues pvs = new MutablePropertyValues();
String basepath = "org/springframework/context/support/";
String[] basenames;
if (reloadable) {
basenames = new String[] { "classpath:" + basepath + "messages", "classpath:" + basepath + "more-messages" };
} else {
basenames = new String[] { basepath + "messages", basepath + "more-messages" };
}
pvs.add("basenames", basenames);
if (!fallbackToSystemLocale) {
pvs.add("fallbackToSystemLocale", Boolean.FALSE);
}
if (useCodeAsDefaultMessage) {
pvs.add("useCodeAsDefaultMessage", Boolean.TRUE);
}
if (alwaysUseMessageFormat) {
pvs.add("alwaysUseMessageFormat", Boolean.TRUE);
}
Class<?> clazz = reloadable ? (Class<?>) ReloadableResourceBundleMessageSource.class : ResourceBundleMessageSource.class;
ac.registerSingleton("messageSource", clazz, pvs);
ac.refresh();
Locale.setDefault(expectGermanFallback ? Locale.GERMAN : Locale.CANADA);
assertEquals("message1", ac.getMessage("code1", null, Locale.ENGLISH));
assertEquals(fallbackToSystemLocale && expectGermanFallback ? "nachricht2" : "message2", ac.getMessage("code2", null, Locale.ENGLISH));
assertEquals("nachricht2", ac.getMessage("code2", null, Locale.GERMAN));
assertEquals("nochricht2", ac.getMessage("code2", null, new Locale("DE", "at")));
assertEquals("noochricht2", ac.getMessage("code2", null, new Locale("DE", "at", "oo")));
if (reloadable) {
assertEquals("nachricht2xml", ac.getMessage("code2", null, Locale.GERMANY));
}
MessageSourceAccessor accessor = new MessageSourceAccessor(ac);
LocaleContextHolder.setLocale(new Locale("DE", "at"));
try {
assertEquals("nochricht2", accessor.getMessage("code2"));
} finally {
LocaleContextHolder.setLocale(null);
}
assertEquals("message3", ac.getMessage("code3", null, Locale.ENGLISH));
MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3");
assertEquals("message3", ac.getMessage(resolvable, Locale.ENGLISH));
resolvable = new DefaultMessageSourceResolvable(new String[] { "code4", "code3" });
assertEquals("message3", ac.getMessage(resolvable, Locale.ENGLISH));
assertEquals("message3", ac.getMessage("code3", null, Locale.ENGLISH));
resolvable = new DefaultMessageSourceResolvable(new String[] { "code4", "code3" });
assertEquals("message3", ac.getMessage(resolvable, Locale.ENGLISH));
Object[] args = new Object[] { "Hello", new DefaultMessageSourceResolvable(new String[] { "code1" }) };
assertEquals("Hello, message1", ac.getMessage("hello", args, Locale.ENGLISH));
// test default message without and with args
assertEquals("default", ac.getMessage(null, null, "default", Locale.ENGLISH));
assertEquals("default", ac.getMessage(null, args, "default", Locale.ENGLISH));
assertEquals("{0}, default", ac.getMessage(null, null, "{0}, default", Locale.ENGLISH));
assertEquals("Hello, default", ac.getMessage(null, args, "{0}, default", Locale.ENGLISH));
// test resolvable with default message, without and with args
resolvable = new DefaultMessageSourceResolvable(null, null, "default");
assertEquals("default", ac.getMessage(resolvable, Locale.ENGLISH));
resolvable = new DefaultMessageSourceResolvable(null, args, "default");
assertEquals("default", ac.getMessage(resolvable, Locale.ENGLISH));
resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default");
assertEquals("{0}, default", ac.getMessage(resolvable, Locale.ENGLISH));
resolvable = new DefaultMessageSourceResolvable(null, args, "{0}, default");
assertEquals("Hello, default", ac.getMessage(resolvable, Locale.ENGLISH));
// test message args
assertEquals("Arg1, Arg2", ac.getMessage("hello", new Object[] { "Arg1", "Arg2" }, Locale.ENGLISH));
assertEquals("{0}, {1}", ac.getMessage("hello", null, Locale.ENGLISH));
if (alwaysUseMessageFormat) {
assertEquals("I'm", ac.getMessage("escaped", null, Locale.ENGLISH));
} else {
assertEquals("I''m", ac.getMessage("escaped", null, Locale.ENGLISH));
}
assertEquals("I'm", ac.getMessage("escaped", new Object[] { "some arg" }, Locale.ENGLISH));
try {
assertEquals("code4", ac.getMessage("code4", null, Locale.GERMAN));
if (!useCodeAsDefaultMessage) {
fail("Should have thrown NoSuchMessageException");
}
} catch (NoSuchMessageException ex) {
if (useCodeAsDefaultMessage) {
fail("Should have returned code as default message");
}
}
}
use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.
the class BeanFactoryPostProcessorTests method testMultipleDefinedBeanFactoryPostProcessors.
@Test
public void testMultipleDefinedBeanFactoryPostProcessors() {
StaticApplicationContext ac = new StaticApplicationContext();
ac.registerSingleton("tb1", TestBean.class);
ac.registerSingleton("tb2", TestBean.class);
MutablePropertyValues pvs1 = new MutablePropertyValues();
pvs1.add("initValue", "${key}");
ac.registerSingleton("bfpp1", TestBeanFactoryPostProcessor.class, pvs1);
MutablePropertyValues pvs2 = new MutablePropertyValues();
pvs2.add("properties", "key=value");
ac.registerSingleton("bfpp2", PropertyPlaceholderConfigurer.class, pvs2);
ac.refresh();
TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp1");
assertEquals("value", bfpp.initValue);
assertTrue(bfpp.wasCalled);
}
use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.
the class EventPublicationInterceptorTests method testExpectedBehavior.
@Test
public void testExpectedBehavior() throws Exception {
TestBean target = new TestBean();
final TestListener listener = new TestListener();
class TestContext extends StaticApplicationContext {
@Override
protected void onRefresh() throws BeansException {
addApplicationListener(listener);
}
}
StaticApplicationContext ctx = new TestContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("applicationEventClass", TestEvent.class.getName());
// should automatically receive applicationEventPublisher reference
ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
ctx.refresh();
EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher");
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(0, interceptor);
ITestBean testBean = (ITestBean) factory.getProxy();
// invoke any method on the advised proxy to see if the interceptor has been invoked
testBean.getAge();
// two events: ContextRefreshedEvent and TestEvent
assertTrue("Interceptor must have published 2 events", listener.getEventCount() == 2);
TestListener otherListener = (TestListener) ctx.getBean("&otherListener");
assertTrue("Interceptor must have published 2 events", otherListener.getEventCount() == 2);
}
use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.
the class PropertyResourceConfigurerIntegrationTests method testPropertyPlaceholderConfigurerWithSystemPropertyInLocation.
@Test
public void testPropertyPlaceholderConfigurerWithSystemPropertyInLocation() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("spouse", new RuntimeBeanReference("${ref}"));
ac.registerSingleton("tb", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("location", "${user.dir}/test");
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanInitializationException");
} catch (BeanInitializationException ex) {
// expected
assertTrue(ex.getCause() instanceof FileNotFoundException);
// slight hack for Linux/Unix systems
String userDir = StringUtils.cleanPath(System.getProperty("user.dir"));
if (userDir.startsWith("/")) {
userDir = userDir.substring(1);
}
assertTrue(ex.getMessage().indexOf(userDir) != -1);
}
}
Aggregations