Search in sources :

Example 6 with TimeStamped

use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.

@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
    final long t = 1001L;
    class Tester implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    // assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertThat(ts.getTimeStamp() == t).isTrue();
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
}
Also used : TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 7 with TimeStamped

use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.

the class CreatesTestBean method jdkIntroduction.

@Test
void jdkIntroduction() {
    ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertThat(nop.getCount()).isEqualTo(0);
    assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
    int age = 5;
    tb.setAge(age);
    assertThat(tb.getAge()).isEqualTo(age);
    boolean condition = tb instanceof TimeStamped;
    assertThat(condition).as("Introduction was made").isTrue();
    assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(3);
    assertThat(tb.getName()).isEqualTo("introductionUsingJdk");
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertThat(lockable1.locked()).isFalse();
    assertThat(lockable2.locked()).isFalse();
    tb.setAge(65);
    assertThat(tb.getAge()).isEqualTo(65);
    lockable1.lock();
    assertThat(lockable1.locked()).isTrue();
    // Shouldn't affect second
    assertThat(lockable2.locked()).isFalse();
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() -> tb.setAge(6));
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimeStamped(org.springframework.core.testfixture.TimeStamped) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Lockable(test.mixin.Lockable) Test(org.junit.jupiter.api.Test)

Example 8 with TimeStamped

use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.

the class CreatesTestBean method jdkIntroductionAppliesToCreatedObjectsNotFactoryBean.

@Test
void jdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
    ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
    assertThat(nop.getCount()).as("NOP should not have done any work yet").isEqualTo(0);
    assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
    int age = 5;
    tb.setAge(age);
    assertThat(tb.getAge()).isEqualTo(age);
    assertThat(tb).as("Introduction was made").isInstanceOf(TimeStamped.class);
    assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(3);
    ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
    // Check two per-instance mixins were distinct
    Lockable lockable1 = (Lockable) tb;
    Lockable lockable2 = (Lockable) tb2;
    assertThat(lockable1.locked()).isFalse();
    assertThat(lockable2.locked()).isFalse();
    tb.setAge(65);
    assertThat(tb.getAge()).isEqualTo(65);
    lockable1.lock();
    assertThat(lockable1.locked()).isTrue();
    // Shouldn't affect second
    assertThat(lockable2.locked()).isFalse();
    // Can still mod second object
    tb2.setAge(12);
    // But can't mod first
    assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() -> tb.setAge(6));
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimeStamped(org.springframework.core.testfixture.TimeStamped) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) Lockable(test.mixin.Lockable) Test(org.junit.jupiter.api.Test)

Example 9 with TimeStamped

use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.

the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.

/**
 * Should see effect immediately on behavior.
 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
    ProxyFactory config = new ProxyFactory(new TestBean());
    assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse();
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to front of interceptor chain
    int oldCount = config.getAdvisors().length;
    config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
    assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
    TimeStamped ts = (TimeStamped) config.getProxy();
    assertThat(ts.getTimeStamp() == time).isTrue();
    // Can remove
    config.removeAdvice(ti);
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    assertThatExceptionOfType(RuntimeException.class).as("Existing object won't implement this interface any more").isThrownBy(// Existing reference will fail
    ts::getTimeStamp);
    assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse();
    // Now check non-effect of removing interceptor that isn't there
    config.removeAdvice(new DebugInterceptor());
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    ITestBean it = (ITestBean) ts;
    DebugInterceptor debugInterceptor = new DebugInterceptor();
    config.addAdvice(0, debugInterceptor);
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(1);
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // not invoked again
    assertThat(debugInterceptor.getCount() == 1).isTrue();
}
Also used : TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Example 10 with TimeStamped

use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testAdviceImplementsIntroductionInfo.

@Test
public void testAdviceImplementsIntroductionInfo() throws Throwable {
    TestBean tb = new TestBean();
    String name = "tony";
    tb.setName(name);
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(di);
    final long ts = 37;
    pc.addAdvice(new DelegatingIntroductionInterceptor((TimeStamped) () -> ts));
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getName()).isEqualTo(name);
    TimeStamped intro = (TimeStamped) proxied;
    assertThat(intro.getTimeStamp()).isEqualTo(ts);
}
Also used : DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)16 TimeStamped (org.springframework.core.testfixture.TimeStamped)16 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)14 TestBean (org.springframework.beans.testfixture.beans.TestBean)11 ProxyFactory (org.springframework.aop.framework.ProxyFactory)8 INestedTestBean (org.springframework.beans.testfixture.beans.INestedTestBean)7 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)7 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)4 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)3 TimestampIntroductionInterceptor (org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor)3 DebugInterceptor (org.springframework.aop.interceptor.DebugInterceptor)2 DelegatingIntroductionInterceptor (org.springframework.aop.support.DelegatingIntroductionInterceptor)2 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)2 Lockable (test.mixin.Lockable)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 IntroductionAdvisor (org.springframework.aop.IntroductionAdvisor)1 IntroductionInterceptor (org.springframework.aop.IntroductionInterceptor)1 Pointcuts (org.springframework.aop.support.Pointcuts)1 IOther (org.springframework.beans.testfixture.beans.IOther)1 Person (org.springframework.beans.testfixture.beans.Person)1