Search in sources :

Example 26 with TimeStamped

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

the class ProxyFactoryTests method testAddRepeatedInterface.

@Test
public void testAddRepeatedInterface() {
    TimeStamped tst = () -> {
        throw new UnsupportedOperationException("getTimeStamp");
    };
    ProxyFactory pf = new ProxyFactory(tst);
    // We've already implicitly added this interface.
    // This call should be ignored without error
    pf.addInterface(TimeStamped.class);
    // All cool
    assertThat(pf.getProxy()).isInstanceOf(TimeStamped.class);
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) Test(org.junit.jupiter.api.Test)

Example 27 with TimeStamped

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

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(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 28 with TimeStamped

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

the class DelegatingIntroductionInterceptorTests method testIntroductionMasksTargetImplementation.

// Test when target implements the interface: should get interceptor by preference.
@Test
public void testIntroductionMasksTargetImplementation() throws Exception {
    final long t = 1001L;
    @SuppressWarnings("serial")
    class TestII extends DelegatingIntroductionInterceptor implements TimeStamped {

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new TestII();
    // != t
    TestBean target = new TargetClass(t + 1);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    TimeStamped ts = (TimeStamped) pf.getProxy();
    // From introduction interceptor, not target
    assertThat(ts.getTimeStamp() == t).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 29 with TimeStamped

use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure 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 30 with TimeStamped

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithSuperInterface.

@Test
public void testIntroductionInterceptorWithSuperInterface() throws Exception {
    TestBean raw = new TestBean();
    assertThat(!(raw instanceof TimeStamped)).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), TimeStamped.class));
    TimeStamped tsp = (TimeStamped) factory.getProxy();
    assertThat(!(tsp instanceof SubTimeStamped)).isTrue();
    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