use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure 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);
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
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();
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure 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);
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInSubclass.
@Test
public void testAutomaticInterfaceRecognitionInSubclass() throws Exception {
final long t = 1001L;
@SuppressWarnings("serial")
class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester {
@Override
public void foo() throws Exception {
}
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new TestII();
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
assertThat(ia.isPerInstance()).isTrue();
pf.addAdvisor(0, ia);
// assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
TimeStamped ts = (TimeStamped) pf.getProxy();
assertThat(ts).isInstanceOf(TimeStamped.class);
// Shouldn't proxy framework interfaces
assertThat(!(ts instanceof MethodInterceptor)).isTrue();
assertThat(!(ts instanceof IntroductionInterceptor)).isTrue();
assertThat(ts.getTimeStamp() == t).isTrue();
((ITester) ts).foo();
((ITestBean) ts).getAge();
// Test removal
ii.suppressInterface(TimeStamped.class);
// Note that we need to construct a new proxy factory,
// or suppress the interface on the proxy factory
pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
Object o = pf.getProxy();
assertThat(!(o instanceof TimeStamped)).isTrue();
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorDoesntReplaceToString.
@SuppressWarnings("serial")
@Test
public void testIntroductionInterceptorDoesntReplaceToString() throws Exception {
TestBean raw = new TestBean();
assertThat(!(raw instanceof TimeStamped)).isTrue();
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = new SerializableTimeStamped(0);
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts) {
@Override
public String toString() {
throw new UnsupportedOperationException("Shouldn't be invoked");
}
}));
TimeStamped tsp = (TimeStamped) factory.getProxy();
assertThat(tsp.getTimeStamp()).isEqualTo(0);
assertThat(tsp.toString()).isEqualTo(raw.toString());
}
Aggregations