use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class NamedPointcutWithArgs method testBindingInPointcutUsedByAdvice.
@Test
public void testBindingInPointcutUsedByAdvice() {
TestBean tb = new TestBean();
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb);
proxyFactory.addAspect(NamedPointcutWithArgs.class);
ITestBean proxiedTestBean = proxyFactory.getProxy();
assertThatIllegalArgumentException().isThrownBy(() -> proxiedTestBean.setName("Supercalifragalisticexpialidocious"));
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class IntroductionBenchmarkTests method timeManyInvocations.
@Test
public void timeManyInvocations() {
StopWatch sw = new StopWatch();
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.setProxyTargetClass(false);
pf.addAdvice(new SimpleCounterIntroduction());
ITestBean proxy = (ITestBean) pf.getProxy();
Counter counter = (Counter) proxy;
sw.start(INVOCATIONS + " invocations on proxy, not hitting introduction");
for (int i = 0; i < INVOCATIONS; i++) {
proxy.getAge();
}
sw.stop();
sw.start(INVOCATIONS + " invocations on proxy, hitting introduction");
for (int i = 0; i < INVOCATIONS; i++) {
counter.getCount();
}
sw.stop();
sw.start(INVOCATIONS + " invocations on target");
for (int i = 0; i < INVOCATIONS; i++) {
target.getAge();
}
sw.stop();
System.out.println(sw.prettyPrint());
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
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();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ProxyFactoryTests method testInterceptorWithoutJoinpoint.
@Test
public void testInterceptorWithoutJoinpoint() {
final TestBean target = new TestBean("tb");
ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, (MethodInterceptor) invocation -> {
assertThat(invocation.getThis()).isNull();
return invocation.getMethod().invoke(target, invocation.getArguments());
});
assertThat(proxy.getName()).isEqualTo("tb");
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class ConcurrencyThrottleInterceptorTests method testMultipleThreads.
private void testMultipleThreads(int concurrencyLimit) {
TestBean tb = new TestBean();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(ITestBean.class);
ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
cti.setConcurrencyLimit(concurrencyLimit);
proxyFactory.addAdvice(cti);
proxyFactory.setTarget(tb);
ITestBean proxy = (ITestBean) proxyFactory.getProxy();
Thread[] threads = new Thread[NR_OF_THREADS];
for (int i = 0; i < NR_OF_THREADS; i++) {
threads[i] = new ConcurrencyThread(proxy, null);
threads[i].start();
}
for (int i = 0; i < NR_OF_THREADS / 10; i++) {
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
threads[i] = new ConcurrencyThread(proxy, i % 2 == 0 ? new OutOfMemoryError() : new IllegalStateException());
threads[i].start();
}
for (int i = 0; i < NR_OF_THREADS; i++) {
try {
threads[i].join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
Aggregations