Search in sources :

Example 76 with XmlBeanDefinitionReader

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

the class ScopedProxyTests method testSimpleProxy.

@Test
public void testSimpleProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(MAP_CONTEXT);
    Object simpleMap = bf.getBean("simpleMap");
    assertTrue(simpleMap instanceof Map);
    assertTrue(simpleMap instanceof HashMap);
}
Also used : HashMap(java.util.HashMap) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 77 with XmlBeanDefinitionReader

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

the class ScopedProxyTests method testJdkScopedProxy.

@Test
public void testJdkScopedProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT);
    bf.setSerializationId("X");
    SimpleMapScope scope = new SimpleMapScope();
    bf.registerScope("request", scope);
    ITestBean bean = (ITestBean) bf.getBean("testBean");
    assertNotNull(bean);
    assertTrue(AopUtils.isJdkDynamicProxy(bean));
    assertTrue(bean instanceof ScopedObject);
    ScopedObject scoped = (ScopedObject) bean;
    assertEquals(TestBean.class, scoped.getTargetObject().getClass());
    bean.setAge(101);
    assertTrue(scope.getMap().containsKey("testBeanTarget"));
    assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass());
    ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean);
    assertNotNull(deserialized);
    assertTrue(AopUtils.isJdkDynamicProxy(deserialized));
    assertEquals(101, bean.getAge());
    assertTrue(deserialized instanceof ScopedObject);
    ScopedObject scopedDeserialized = (ScopedObject) deserialized;
    assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass());
    bf.setSerializationId(null);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SimpleMapScope(org.springframework.tests.context.SimpleMapScope) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 78 with XmlBeanDefinitionReader

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

the class ProxyFactoryBeanTests method testCanAddThrowsAdviceWithoutAdvisor.

@Test
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
    DefaultListableBeanFactory f = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(f).loadBeanDefinitions(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
    MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
    CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
    assertEquals(0, cba.getCalls());
    assertEquals(0, th.getCalls());
    IEcho echo = (IEcho) f.getBean("throwsAdvised");
    int i = 12;
    echo.setA(i);
    assertEquals(i, echo.getA());
    assertEquals(2, cba.getCalls());
    assertEquals(0, th.getCalls());
    Exception expected = new Exception();
    try {
        echo.echoException(1, expected);
        fail();
    } catch (Exception ex) {
        assertEquals(expected, ex);
    }
    // No throws handler method: count should still be 0
    assertEquals(0, th.getCalls());
    // Handler knows how to handle this exception
    expected = new FileNotFoundException();
    try {
        echo.echoException(1, expected);
        fail();
    } catch (IOException ex) {
        assertEquals(expected, ex);
    }
    // One match
    assertEquals(1, th.getCalls("ioException"));
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) MyThrowsHandler(org.springframework.tests.aop.advice.MyThrowsHandler) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LockedException(test.mixin.LockedException) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 79 with XmlBeanDefinitionReader

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

the class ProxyFactoryBeanTests method testGetObjectTypeWithDirectTarget.

@Test
public void testGetObjectTypeWithDirectTarget() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
    // We have a counting before advice here
    CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice");
    assertEquals(0, cba.getCalls());
    ITestBean tb = (ITestBean) bf.getBean("directTarget");
    assertTrue(tb.getName().equals("Adam"));
    assertEquals(1, cba.getCalls());
    ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget");
    assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) 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) ClassPathResource(org.springframework.core.io.ClassPathResource) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 80 with XmlBeanDefinitionReader

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

the class ProxyFactoryBeanTests method testFrozenFactoryBean.

@Test
public void testFrozenFactoryBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(FROZEN_CONTEXT, CLASS));
    Advised advised = (Advised) bf.getBean("frozen");
    assertTrue("The proxy should be frozen", advised.isFrozen());
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) 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