Search in sources :

Example 96 with TestBean

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

the class ProxyFactoryTests method testInterceptorInclusionMethods.

@Test
public void testInterceptorInclusionMethods() {
    class MyInterceptor implements MethodInterceptor {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw new UnsupportedOperationException();
        }
    }
    NopInterceptor di = new NopInterceptor();
    NopInterceptor diUnused = new NopInterceptor();
    ProxyFactory factory = new ProxyFactory(new TestBean());
    factory.addAdvice(0, di);
    assertThat(factory.getProxy(), instanceOf(ITestBean.class));
    assertTrue(factory.adviceIncluded(di));
    assertTrue(!factory.adviceIncluded(diUnused));
    assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1);
    assertTrue(factory.countAdvicesOfType(MyInterceptor.class) == 0);
    factory.addAdvice(0, diUnused);
    assertTrue(factory.adviceIncluded(diUnused));
    assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 97 with TestBean

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

the class ProxyFactoryTests method testIndexOfMethods.

@Test
public void testIndexOfMethods() {
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    NopInterceptor nop = new NopInterceptor();
    Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
    Advised advised = (Advised) pf.getProxy();
    // Can use advised and ProxyFactory interchangeably
    advised.addAdvice(nop);
    pf.addAdvisor(advisor);
    assertEquals(-1, pf.indexOf(new NopInterceptor()));
    assertEquals(0, pf.indexOf(nop));
    assertEquals(1, pf.indexOf(advisor));
    assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null)));
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 98 with TestBean

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

the class ConcurrencyThrottleInterceptorTests method testMultipleThreads.

private void testMultipleThreads(int concurrencyLimit) {
    TestBean tb = new TestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(new Class[] { ITestBean.class });
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    cti.setConcurrencyLimit(concurrencyLimit);
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    Thread[] threads = new Thread[NR_OF_THREADS];
    for (int i = 0; i < NR_OF_THREADS; i++) {
        threads[i] = new ConcurrencyThread(proxy, null);
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS / 10; i++) {
        try {
            Thread.sleep(5);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        threads[i] = new ConcurrencyThread(proxy, i % 2 == 0 ? (Throwable) new OutOfMemoryError() : (Throwable) new IllegalStateException());
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
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) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 99 with TestBean

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

the class ClassUtilsTests method getShortNameForCglibClass.

@Test
public void getShortNameForCglibClass() {
    TestBean tb = new TestBean();
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(tb);
    pf.setProxyTargetClass(true);
    TestBean proxy = (TestBean) pf.getProxy();
    String className = ClassUtils.getShortName(proxy.getClass());
    assertEquals("Class name did not match", "TestBean", className);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 100 with TestBean

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

the class ControlFlowPointcutTests method testMatches.

@Test
public void testMatches() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));
    // Not advised, not under One
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(0, nop.getCount());
    // Will be advised
    assertEquals(target.getAge(), new One().getAge(proxied));
    assertEquals(1, nop.getCount());
    // Won't be advised
    assertEquals(target.getAge(), new One().nomatch(proxied));
    assertEquals(1, nop.getCount());
    assertEquals(3, cflow.getEvaluations());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27