Search in sources :

Example 16 with XmlBeanDefinitionReader

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);
}
Also used : ObjectFactory(org.springframework.beans.factory.ObjectFactory) TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) List(java.util.List) LinkedList(java.util.LinkedList) Before(org.junit.Before)

Example 17 with XmlBeanDefinitionReader

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

Example 18 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 19 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 20 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)

Aggregations

XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)152 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)82 Test (org.junit.Test)80 ClassPathResource (org.springframework.core.io.ClassPathResource)62 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)38 Before (org.junit.Before)30 ITestBean (org.springframework.tests.sample.beans.ITestBean)18 TestBean (org.springframework.tests.sample.beans.TestBean)11 BeansException (org.springframework.beans.BeansException)9 Resource (org.springframework.core.io.Resource)8 UrlResource (org.springframework.core.io.UrlResource)8 URL (java.net.URL)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 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 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)4 ResourceEntityResolver (org.springframework.beans.factory.xml.ResourceEntityResolver)4