/** * Create a new DebugInterceptor with dynamic or static logger, * according to the given flag. * @param useDynamicLogger whether to use a dynamic logger or a static logger * @see #setUseDynamicLogger */ public DebugInterceptor(boolean useDynamicLogger) { setUseDynamicLogger(useDynamicLogger); }
private void checkCallCountTotal(DebugInterceptor interceptor) { assertEquals("Intercepted call count not being incremented correctly", 1, interceptor.getCount()); }
private void resetInterceptor() { DebugInterceptor interceptor = getInterceptor(); interceptor.resetCount(); }
config.removeAdvice(new DebugInterceptor()); assertTrue(config.getAdvisors().length == oldCount); DebugInterceptor debugInterceptor = new DebugInterceptor(); config.addAdvice(0, debugInterceptor); it.getSpouse(); assertTrue(debugInterceptor.getCount() == 0); it = (ITestBean) factory.getBean("test2"); it.getSpouse(); assertEquals(1, debugInterceptor.getCount()); config.removeAdvice(debugInterceptor); it.getSpouse(); assertEquals(2, debugInterceptor.getCount()); assertEquals(2, debugInterceptor.getCount());
@Test public void testDetectsInterfaces() throws Exception { ProxyFactoryBean fb = new ProxyFactoryBean(); fb.setTarget(new TestBean()); fb.addAdvice(new DebugInterceptor()); fb.setBeanFactory(new DefaultListableBeanFactory()); ITestBean proxy = (ITestBean) fb.getObject(); assertTrue(AopUtils.isJdkDynamicProxy(proxy)); }
@Test public void testSunnyDayPathLogsCorrectly() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); DebugInterceptor interceptor = new StubDebugInterceptor(log); interceptor.invoke(methodInvocation); checkCallCountTotal(interceptor); verify(log, times(2)).trace(anyString()); }
config.removeAdvice(new DebugInterceptor()); DebugInterceptor debugInterceptor = new DebugInterceptor(); config.addAdvice(0, debugInterceptor); it.getSpouse(); assertEquals(1, debugInterceptor.getCount()); config.removeAdvice(debugInterceptor); it.getSpouse(); assertTrue(debugInterceptor.getCount() == 1);
@Test public void testProxyIsBoundBeforeTargetSourceInvoked() { final TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(new DebugInterceptor()); pf.setExposeProxy(true); final ITestBean proxy = (ITestBean) createProxy(pf); Advised config = (Advised) proxy; // This class just checks proxy is bound before getTarget() call config.setTargetSource(new TargetSource() { @Override public Class<?> getTargetClass() { return TestBean.class; } @Override public boolean isStatic() { return false; } @Override public Object getTarget() throws Exception { assertEquals(proxy, AopContext.currentProxy()); return target; } @Override public void releaseTarget(Object target) throws Exception { } }); // Just test anything: it will fail if context wasn't found assertEquals(0, proxy.getAge()); }
@Test public void testExceptionPathStillLogsCorrectly() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); IllegalArgumentException exception = new IllegalArgumentException(); given(methodInvocation.proceed()).willThrow(exception); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); DebugInterceptor interceptor = new StubDebugInterceptor(log); try { interceptor.invoke(methodInvocation); fail("Must have propagated the IllegalArgumentException."); } catch (IllegalArgumentException expected) { } checkCallCountTotal(interceptor); verify(log).trace(anyString()); verify(log).trace(anyString(), eq(exception)); }
/** * Create a new DebugInterceptor with dynamic or static logger, * according to the given flag. * @param useDynamicLogger whether to use a dynamic logger or a static logger * @see #setUseDynamicLogger */ public DebugInterceptor(boolean useDynamicLogger) { setUseDynamicLogger(useDynamicLogger); }
private void assertInterceptorCount(int count) { DebugInterceptor interceptor = getInterceptor(); assertEquals("Interceptor count is incorrect", count, interceptor.getCount()); }
/** * Create a new DebugInterceptor with dynamic or static logger, * according to the given flag. * @param useDynamicLogger whether to use a dynamic logger or a static logger * @see #setUseDynamicLogger */ public DebugInterceptor(boolean useDynamicLogger) { setUseDynamicLogger(useDynamicLogger); }
@Test public void appliesAspectToClassWithComplexConstructor() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("ObjenesisProxyTests-context.xml", getClass()); ClassWithComplexConstructor bean = context.getBean(ClassWithComplexConstructor.class); bean.method(); DebugInterceptor interceptor = context.getBean(DebugInterceptor.class); assertThat(interceptor.getCount(), is(1L)); assertThat(bean.getDependency().getValue(), is(1)); }
/** * Create a new DebugInterceptor with dynamic or static logger, * according to the given flag. * @param useDynamicLogger whether to use a dynamic logger or a static logger * @see #setUseDynamicLogger */ public DebugInterceptor(boolean useDynamicLogger) { setUseDynamicLogger(useDynamicLogger); }