Search in sources :

Example 11 with TimeStamped

use of cn.taketoday.core.testfixture.TimeStamped in project today-framework by TAKETODAY.

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithInterfaceHierarchy.

@Test
public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception {
    TestBean raw = new TestBean();
    assertThat(!(raw instanceof SubTimeStamped)).isTrue();
    ProxyFactory factory = new ProxyFactory(raw);
    TimeStamped ts = mock(SubTimeStamped.class);
    long timestamp = 111L;
    given(ts.getTimeStamp()).willReturn(timestamp);
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
    SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
    assertThat(tsp.getTimeStamp() == timestamp).isTrue();
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) INestedTestBean(cn.taketoday.beans.testfixture.beans.INestedTestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 12 with TimeStamped

use of cn.taketoday.core.testfixture.TimeStamped in project today-framework by TAKETODAY.

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(cn.taketoday.beans.testfixture.beans.ITestBean) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) Lockable(test.aop.Lockable) Test(org.junit.jupiter.api.Test)

Example 13 with TimeStamped

use of cn.taketoday.core.testfixture.TimeStamped in project today-framework by TAKETODAY.

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(cn.taketoday.aop.support.DelegatingIntroductionInterceptor) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 14 with TimeStamped

use of cn.taketoday.core.testfixture.TimeStamped in project today-framework by TAKETODAY.

the class AbstractAopProxyTests method testIntroductionThrowsUncheckedException.

/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constrained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    @SuppressWarnings("serial")
    class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {

        /**
         * @see TimeStamped#getTimeStamp()
         */
        @Override
        public long getTimeStamp() {
            throw new UnsupportedOperationException();
        }
    }
    pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));
    TimeStamped ts = (TimeStamped) createProxy(pc);
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(ts::getTimeStamp);
}
Also used : DelegatingIntroductionInterceptor(cn.taketoday.aop.support.DelegatingIntroductionInterceptor) TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Pointcuts(cn.taketoday.aop.support.Pointcuts) Test(org.junit.jupiter.api.Test)

Example 15 with TimeStamped

use of cn.taketoday.core.testfixture.TimeStamped in project today-framework by TAKETODAY.

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithDelegation.

@Test
public void testIntroductionInterceptorWithDelegation() throws Exception {
    TestBean raw = new TestBean();
    assertThat(!(raw instanceof TimeStamped)).isTrue();
    ProxyFactory factory = new ProxyFactory(raw);
    TimeStamped ts = mock(TimeStamped.class);
    long timestamp = 111L;
    given(ts.getTimeStamp()).willReturn(timestamp);
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
    TimeStamped tsp = (TimeStamped) factory.getProxy();
    assertThat(tsp.getTimeStamp() == timestamp).isTrue();
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) NestedTestBean(cn.taketoday.beans.testfixture.beans.NestedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) INestedTestBean(cn.taketoday.beans.testfixture.beans.INestedTestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Aggregations

TimeStamped (cn.taketoday.core.testfixture.TimeStamped)32 Test (org.junit.jupiter.api.Test)32 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)28 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)22 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)16 INestedTestBean (cn.taketoday.beans.testfixture.beans.INestedTestBean)14 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)8 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)6 TimestampIntroductionInterceptor (cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor)6 DebugInterceptor (cn.taketoday.aop.interceptor.DebugInterceptor)4 DelegatingIntroductionInterceptor (cn.taketoday.aop.support.DelegatingIntroductionInterceptor)4 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)4 Lockable (test.aop.Lockable)4 IntroductionAdvisor (cn.taketoday.aop.IntroductionAdvisor)2 IntroductionInterceptor (cn.taketoday.aop.IntroductionInterceptor)2 Pointcuts (cn.taketoday.aop.support.Pointcuts)2 IOther (cn.taketoday.beans.testfixture.beans.IOther)2 Person (cn.taketoday.beans.testfixture.beans.Person)2 SerializablePerson (cn.taketoday.beans.testfixture.beans.SerializablePerson)2