Search in sources :

Example 26 with XmlBeanDefinitionReader

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");
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) LockedException(test.mixin.LockedException) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Lockable(test.mixin.Lockable) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 27 with XmlBeanDefinitionReader

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());
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Person(org.springframework.tests.sample.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 28 with XmlBeanDefinitionReader

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);
}
Also used : SimpleMapScope(org.springframework.tests.context.SimpleMapScope) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 29 with XmlBeanDefinitionReader

use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.

the class ScopedProxyTests method testScopedOverride.

@Test
public void testScopedOverride() throws Exception {
    GenericApplicationContext ctx = new GenericApplicationContext();
    new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
    SimpleMapScope scope = new SimpleMapScope();
    ctx.getBeanFactory().registerScope("request", scope);
    ctx.refresh();
    ITestBean bean = (ITestBean) ctx.getBean("testBean");
    assertEquals("male", bean.getName());
    assertEquals(99, bean.getAge());
    assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
    assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Test(org.junit.Test)

Example 30 with XmlBeanDefinitionReader

use of org.springframework.beans.factory.xml.XmlBeanDefinitionReader in project spring-framework by spring-projects.

the class CommonsPool2TargetSourceTests method setUp.

@Before
public void setUp() throws Exception {
    this.beanFactory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass()));
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Aggregations

XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)138 Test (org.junit.Test)79 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)74 ClassPathResource (org.springframework.core.io.ClassPathResource)59 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)36 Before (org.junit.Before)24 ITestBean (org.springframework.tests.sample.beans.ITestBean)18 TestBean (org.springframework.tests.sample.beans.TestBean)11 BeansException (org.springframework.beans.BeansException)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 Resource (org.springframework.core.io.Resource)7 UrlResource (org.springframework.core.io.UrlResource)7 URL (java.net.URL)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)6 HashMap (java.util.HashMap)5 Person (org.springframework.tests.sample.beans.Person)5 ArrayList (java.util.ArrayList)4 BeanCreationException (org.springframework.beans.factory.BeanCreationException)4 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)4 Map (java.util.Map)3