Search in sources :

Example 21 with TimeStamped

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

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

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

the class ProxyFactoryBeanTests method testCanAddAndRemoveAspectInterfacesOnPrototype.

/**
 * Try adding and removing interfaces and interceptors on prototype.
 * Changes will only affect future references obtained from the factory.
 * Each instance will be independent.
 */
@Test
public void testCanAddAndRemoveAspectInterfacesOnPrototype() {
    assertThat(factory.getBean("test2")).as("Shouldn't implement TimeStamped before manipulation").isNotInstanceOf(TimeStamped.class);
    ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2");
    long time = 666L;
    TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
    ti.setTime(time);
    // Add to head 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) factory.getBean("test2");
    assertThat(ts.getTimeStamp()).isEqualTo(time);
    // Can remove
    config.removeAdvice(ti);
    assertThat(config.getAdvisors().length == oldCount).isTrue();
    // Check no change on existing object reference
    assertThat(ts.getTimeStamp() == time).isTrue();
    assertThat(factory.getBean("test2")).as("Should no longer implement TimeStamped").isNotInstanceOf(TimeStamped.class);
    // 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();
    // Won't affect existing reference
    assertThat(debugInterceptor.getCount() == 0).isTrue();
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(1);
    config.removeAdvice(debugInterceptor);
    it.getSpouse();
    // Still invoked with old reference
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // not invoked with new object
    it = (ITestBean) factory.getBean("test2");
    it.getSpouse();
    assertThat(debugInterceptor.getCount()).isEqualTo(2);
    // Our own timestamped reference should still work
    assertThat(ts.getTimeStamp()).isEqualTo(time);
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TimestampIntroductionInterceptor(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) DebugInterceptor(cn.taketoday.aop.interceptor.DebugInterceptor) Test(org.junit.jupiter.api.Test)

Example 24 with TimeStamped

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

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

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(cn.taketoday.aop.testfixture.interceptor.TimestampIntroductionInterceptor) IOther(cn.taketoday.beans.testfixture.beans.IOther) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) 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) 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