package com.poc.restfulpoc.config;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.util.ReflectionUtils;

import lombok.extern.slf4j.Slf4j;
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;

@TestComponent
@Slf4j
public class DatasourceProxyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {

		if (bean instanceof DataSource) {

			log.info("Inside Proxy Creation :{}", bean);

			final ProxyFactory proxyFactory = new ProxyFactory(bean);

			proxyFactory.setProxyTargetClass(true);
			proxyFactory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean));

			return proxyFactory.getProxy();
		}
		return bean;
	}

	private static class ProxyDataSourceInterceptor implements MethodInterceptor {

		private final DataSource dataSource;

		public ProxyDataSourceInterceptor(final DataSource dataSource) {
			super();
			this.dataSource = ProxyDataSourceBuilder.create(dataSource)
					.name("DATA_SOURCE_PROXY").logQueryBySlf4j(SLF4JLogLevel.INFO)
					.multiline().countQuery().logSlowQueryBySlf4j(5, TimeUnit.SECONDS)
					.build();
		}

		@Override
		public Object invoke(final MethodInvocation invocation) throws Throwable {

			final Method proxyMethod = ReflectionUtils.findMethod(
					this.dataSource.getClass(), invocation.getMethod().getName());

			if (proxyMethod != null) {
				return proxyMethod.invoke(this.dataSource, invocation.getArguments());
			}

			return invocation.proceed();
		}

	}

}

@@ -1,141 +1,913 @@
-package com.poc.restfulpoc.config;%0A%0Aimport java.lang.reflect.Method;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport javax.sql.DataSource;%0A
+/*%0A * Copyright 2015-2018 the original author or authors.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.poc.restfulpoc.config;%0A%0Aimport java.lang.reflect.Method;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport javax.sql.DataSource;%0A%0Aimport lombok.extern.slf4j.Slf4j;%0Aimport net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;%0Aimport net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
 %0Aimp
@@ -1006,16 +1006,17 @@
 cation;%0A
+%0A
 import o
@@ -1243,159 +1243,8 @@
 s;%0A%0A
-import lombok.extern.slf4j.Slf4j;%0Aimport net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;%0Aimport net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;%0A%0A
 @Tes
@@ -1880,23 +1880,16 @@
 rce;%0A%0A%09%09
-public 
 ProxyDat

Fix Junits.