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);
}
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);
}
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"));
}
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()));
}
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());
}
Aggregations