use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testCanEscapeBeanReferenceSyntax.
@Test
public void testCanEscapeBeanReferenceSyntax() {
String name = "*name";
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("r.(class)", TestBean.class.getName());
p.setProperty("r.name", "*" + name);
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
TestBean r = (TestBean) lbf.getBean("r");
assertTrue(r.getName().equals(name));
}
use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testSimpleReference.
@Test
public void testSimpleReference() {
String PREFIX = "beans.";
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
p.setProperty(PREFIX + "rod.name", "Rod");
p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
p.setProperty(PREFIX + "kerry.name", "Kerry");
p.setProperty(PREFIX + "kerry.age", "35");
p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");
int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
assertTrue("2 beans registered, not " + count, count == 2);
TestBean kerry = lbf.getBean("kerry", TestBean.class);
assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
ITestBean spouse = kerry.getSpouse();
assertTrue("Kerry spouse is non null", spouse != null);
assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching.
@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("x1.(class)", DummyFactory.class.getName());
// Reset static state
DummyFactory.reset();
p.setProperty("x1.singleton", "false");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
assertEquals(0, beanNames.length);
beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
assertEquals(0, beanNames.length);
assertFalse(lbf.containsSingleton("x1"));
assertTrue(lbf.containsBean("x1"));
assertTrue(lbf.containsBean("&x1"));
assertFalse(lbf.isSingleton("x1"));
assertTrue(lbf.isSingleton("&x1"));
assertTrue(lbf.isPrototype("x1"));
assertFalse(lbf.isPrototype("&x1"));
assertTrue(lbf.isTypeMatch("x1", TestBean.class));
assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
assertEquals(TestBean.class, lbf.getType("x1"));
assertEquals(DummyFactory.class, lbf.getType("&x1"));
assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testNameAlreadyBound.
@Test
public void testNameAlreadyBound() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("kerry.(class)", TestBean.class.getName());
p.setProperty("kerry.age", "35");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
try {
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
} catch (BeanDefinitionStoreException ex) {
assertEquals("kerry", ex.getBeanName());
// expected
}
}
use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testRegisterExistingSingletonWithReference.
@Test
public void testRegisterExistingSingletonWithReference() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty("test.(class)", TestBean.class.getName());
p.setProperty("test.name", "Tony");
p.setProperty("test.age", "48");
p.setProperty("test.spouse(ref)", "singletonObject");
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
Object singletonObject = new TestBean();
lbf.registerSingleton("singletonObject", singletonObject);
assertTrue(lbf.isSingleton("singletonObject"));
assertEquals(TestBean.class, lbf.getType("singletonObject"));
TestBean test = (TestBean) lbf.getBean("test");
assertEquals(singletonObject, lbf.getBean("singletonObject"));
assertEquals(singletonObject, test.getSpouse());
Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true);
assertEquals(2, beansOfType.size());
assertTrue(beansOfType.containsValue(test));
assertTrue(beansOfType.containsValue(singletonObject));
beansOfType = lbf.getBeansOfType(null, false, true);
assertEquals(2, beansOfType.size());
Iterator<String> beanNames = lbf.getBeanNamesIterator();
assertEquals("test", beanNames.next());
assertEquals("singletonObject", beanNames.next());
assertFalse(beanNames.hasNext());
assertTrue(lbf.containsSingleton("test"));
assertTrue(lbf.containsSingleton("singletonObject"));
assertTrue(lbf.containsBeanDefinition("test"));
assertFalse(lbf.containsBeanDefinition("singletonObject"));
}
Aggregations