Search in sources :

Example 61 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerWithSpecialLocalMethods.

@Test
public void httpInvokerWithSpecialLocalMethods() throws Exception {
    String serviceUrl = "http://myurl";
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl(serviceUrl);
    pfb.setHttpInvokerRequestExecutor(new HttpInvokerRequestExecutor() {

        @Override
        public RemoteInvocationResult executeRequest(HttpInvokerClientConfiguration config, RemoteInvocation invocation) throws IOException {
            throw new IOException("argh");
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    // shouldn't go through to remote service
    assertTrue(proxy.toString().indexOf("HTTP invoker") != -1);
    assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
    assertEquals(proxy.hashCode(), proxy.hashCode());
    assertTrue(proxy.equals(proxy));
    // should go through
    try {
        proxy.setAge(50);
        fail("Should have thrown RemoteAccessException");
    } catch (RemoteAccessException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) ITestBean(org.springframework.tests.sample.beans.ITestBean) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) Test(org.junit.Test)

Example 62 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class RequestScopedProxyTests method testGetFromScopeThroughDynamicProxy.

@Test
public void testGetFromScopeThroughDynamicProxy() throws Exception {
    String name = "requestScopedProxy";
    ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
    assertTrue(AopUtils.isJdkDynamicProxy(bean));
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        assertNull(request.getAttribute("scopedTarget." + name));
        assertEquals("scoped", bean.getName());
        assertNotNull(request.getAttribute("scopedTarget." + name));
        TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
        assertEquals(TestBean.class, target.getClass());
        assertEquals("scoped", target.getName());
        assertSame(bean, this.beanFactory.getBean(name));
        assertEquals(bean.toString(), target.toString());
    } finally {
        RequestContextHolder.setRequestAttributes(null);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 63 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class TxNamespaceHandlerTests method isProxy.

@Test
public void isProxy() throws Exception {
    ITestBean bean = getTestBean();
    assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Test(org.junit.Test)

Example 64 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class TxNamespaceHandlerTests method invokeTransactional.

@Test
public void invokeTransactional() throws Exception {
    ITestBean testBean = getTestBean();
    CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
    // try with transactional
    assertEquals("Should not have any started transactions", 0, ptm.begun);
    testBean.getName();
    assertTrue(ptm.lastDefinition.isReadOnly());
    assertEquals("Should have 1 started transaction", 1, ptm.begun);
    assertEquals("Should have 1 committed transaction", 1, ptm.commits);
    // try with non-transaction
    testBean.haveBirthday();
    assertEquals("Should not have started another transaction", 1, ptm.begun);
    // try with exceptional
    try {
        testBean.exceptional(new IllegalArgumentException("foo"));
        fail("Should NEVER get here");
    } catch (Throwable throwable) {
        assertEquals("Should have another started transaction", 2, ptm.begun);
        assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) Test(org.junit.Test)

Example 65 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class AbstractTransactionAspectTests method noTransaction.

@Test
public void noTransaction() throws Exception {
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    TestBean tb = new TestBean();
    TransactionAttributeSource tas = new MapTransactionAttributeSource();
    // All the methods in this class use the advised() template method
    // to obtain a transaction object, configured with the given PlatformTransactionManager
    // and transaction attribute source
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    checkTransactionStatus(false);
    itb.getName();
    checkTransactionStatus(false);
    // expect no calls
    verifyZeroInteractions(ptm);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Aggregations

ITestBean (org.springframework.tests.sample.beans.ITestBean)221 Test (org.junit.Test)205 TestBean (org.springframework.tests.sample.beans.TestBean)127 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)37 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)24 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 IOException (java.io.IOException)15 Advisor (org.springframework.aop.Advisor)15 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)15 Method (java.lang.reflect.Method)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)14 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)13 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)13 MethodInvocation (org.aopalliance.intercept.MethodInvocation)12 Advised (org.springframework.aop.framework.Advised)12 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)11 LockedException (test.mixin.LockedException)11 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)10