use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.
the class SimpleScopeTests method setUp.
@Before
public void setUp() {
beanFactory = new DefaultListableBeanFactory();
Scope scope = new NoOpScope() {
private int index;
private List<TestBean> objects = new LinkedList<>();
{
objects.add(new TestBean());
objects.add(new TestBean());
}
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
if (index >= objects.size()) {
index = 0;
}
return objects.get(index++);
}
};
beanFactory.registerScope("myScope", scope);
String[] scopeNames = beanFactory.getRegisteredScopeNames();
assertEquals(1, scopeNames.length);
assertEquals("myScope", scopeNames[0]);
assertSame(scope, beanFactory.getRegisteredScope("myScope"));
XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory);
xbdr.loadBeanDefinitions(CONTEXT);
}
use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testTargetSourceNotAtEndOfInterceptorNamesIsRejected.
@Test
public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() {
try {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS));
bf.getBean("targetSourceNotLast");
fail("TargetSource or non-advised object must be last in interceptorNames");
} catch (BeanCreationException ex) {
// Root cause of the problem must be an AOP exception
AopConfigException aex = (AopConfigException) ex.getCause();
assertTrue(aex.getMessage().contains("interceptorNames"));
}
}
use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testPrototypeAdvisor.
@Test
public void testPrototypeAdvisor() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(CONTEXT, CLASS));
ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxy");
ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxy");
bean1.setAge(3);
bean2.setAge(4);
assertEquals(3, bean1.getAge());
assertEquals(4, bean2.getAge());
((Lockable) bean1).lock();
try {
bean1.setAge(5);
fail("expected LockedException");
} catch (LockedException ex) {
// expected
}
try {
bean2.setAge(6);
} catch (LockedException ex) {
fail("did not expect LockedException");
}
}
use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testSerializableSingletonProxyFactoryBean.
@Test
public void testSerializableSingletonProxyFactoryBean() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("serializableSingleton");
ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&serializableSingleton");
ProxyFactoryBean pfb2 = (ProxyFactoryBean) SerializationTestUtils.serializeAndDeserialize(pfb);
Person p2 = (Person) pfb2.getObject();
assertEquals(p, p2);
assertNotSame(p, p2);
assertEquals("serializableSingleton", p2.getName());
}
use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.
the class ScopedProxyTests method testCglibScopedProxy.
@Test
public void testCglibScopedProxy() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT);
bf.setSerializationId("Y");
SimpleMapScope scope = new SimpleMapScope();
bf.registerScope("request", scope);
TestBean tb = (TestBean) bf.getBean("testBean");
assertTrue(AopUtils.isCglibProxy(tb.getFriends()));
assertTrue(tb.getFriends() instanceof ScopedObject);
ScopedObject scoped = (ScopedObject) tb.getFriends();
assertEquals(ArrayList.class, scoped.getTargetObject().getClass());
tb.getFriends().add("myFriend");
assertTrue(scope.getMap().containsKey("scopedTarget.scopedList"));
assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass());
ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends());
assertNotNull(deserialized);
assertTrue(AopUtils.isCglibProxy(deserialized));
assertTrue(deserialized.contains("myFriend"));
assertTrue(deserialized instanceof ScopedObject);
ScopedObject scopedDeserialized = (ScopedObject) deserialized;
assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass());
bf.setSerializationId(null);
}
Aggregations