Search in sources :

Example 11 with ITestBean

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

the class NamedPointcutWithArgs method testBindingInPointcutUsedByAdvice.

@Test
public void testBindingInPointcutUsedByAdvice() {
    TestBean tb = new TestBean();
    AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb);
    proxyFactory.addAspect(NamedPointcutWithArgs.class);
    ITestBean proxiedTestBean = proxyFactory.getProxy();
    assertThatIllegalArgumentException().isThrownBy(() -> proxiedTestBean.setName("Supercalifragalisticexpialidocious"));
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 12 with ITestBean

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

the class IntroductionBenchmarkTests method timeManyInvocations.

@Test
public void timeManyInvocations() {
    StopWatch sw = new StopWatch();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.setProxyTargetClass(false);
    pf.addAdvice(new SimpleCounterIntroduction());
    ITestBean proxy = (ITestBean) pf.getProxy();
    Counter counter = (Counter) proxy;
    sw.start(INVOCATIONS + " invocations on proxy, not hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        proxy.getAge();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on proxy, hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        counter.getCount();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on target");
    for (int i = 0; i < INVOCATIONS; i++) {
        target.getAge();
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StopWatch(org.springframework.util.StopWatch) Test(org.junit.jupiter.api.Test)

Example 13 with ITestBean

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

the class ProxyFactoryTests method testGetsAllInterfaces.

@Test
public void testGetsAllInterfaces() {
    // Extend to get new interface
    class TestBeanSubclass extends TestBean implements Comparable<Object> {

        @Override
        public int compareTo(Object arg0) {
            throw new UnsupportedOperationException("compareTo");
        }
    }
    TestBeanSubclass raw = new TestBeanSubclass();
    ProxyFactory factory = new ProxyFactory(raw);
    // System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
    assertThat(factory.getProxiedInterfaces().length).as("Found correct number of interfaces").isEqualTo(5);
    ITestBean tb = (ITestBean) factory.getProxy();
    assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class);
    raw.setAge(25);
    assertThat(tb.getAge() == raw.getAge()).isTrue();
    long t = 555555L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
    Class<?>[] oldProxiedInterfaces = factory.getProxiedInterfaces();
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
    assertThat(newProxiedInterfaces.length).as("Advisor proxies one more interface after introduction").isEqualTo(oldProxiedInterfaces.length + 1);
    TimeStamped ts = (TimeStamped) factory.getProxy();
    assertThat(ts.getTimeStamp() == t).isTrue();
    // Shouldn't fail;
    ((IOther) ts).absquatulate();
}
Also used : TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) IOther(org.springframework.beans.testfixture.beans.IOther) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimeStamped(org.springframework.core.testfixture.TimeStamped) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.jupiter.api.Test)

Example 14 with ITestBean

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

the class ProxyFactoryTests method testInterceptorWithoutJoinpoint.

@Test
public void testInterceptorWithoutJoinpoint() {
    final TestBean target = new TestBean("tb");
    ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, (MethodInterceptor) invocation -> {
        assertThat(invocation.getThis()).isNull();
        return invocation.getMethod().invoke(target, invocation.getArguments());
    });
    assertThat(proxy.getName()).isEqualTo("tb");
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) AopUtils(org.springframework.aop.support.AopUtils) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Disabled(org.junit.jupiter.api.Disabled) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) MethodInvocation(org.aopalliance.intercept.MethodInvocation) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) JFrame(javax.swing.JFrame) Advisor(org.springframework.aop.Advisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Order(org.springframework.core.annotation.Order) RootPaneContainer(javax.swing.RootPaneContainer) Accessible(javax.accessibility.Accessible) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 15 with ITestBean

use of org.springframework.beans.testfixture.beans.ITestBean 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(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 ? new OutOfMemoryError() : 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.beans.testfixture.beans.ITestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Aggregations

ITestBean (org.springframework.beans.testfixture.beans.ITestBean)210 Test (org.junit.jupiter.api.Test)199 TestBean (org.springframework.beans.testfixture.beans.TestBean)121 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)44 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)31 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)23 Advisor (org.springframework.aop.Advisor)22 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)20 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)19 Method (java.lang.reflect.Method)16 TimeStamped (org.springframework.core.testfixture.TimeStamped)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)15 IOException (java.io.IOException)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Advised (org.springframework.aop.framework.Advised)13