001
014
015 package com.liferay.portal.test.jdbc;
016
017 import com.liferay.portal.kernel.util.ArrayUtil;
018
019 import java.lang.reflect.InvocationHandler;
020 import java.lang.reflect.InvocationTargetException;
021 import java.lang.reflect.Method;
022
023 import java.sql.Connection;
024 import java.sql.Statement;
025
026
029 public class ResetDatabaseStatementHandler implements InvocationHandler {
030
031 public ResetDatabaseStatementHandler(
032 Connection connection, Statement statement) {
033
034 _connection = connection;
035 _statement = statement;
036 }
037
038 @Override
039 public Object invoke(Object proxy, Method method, Object[] arguments)
040 throws Throwable {
041
042 try {
043 String methodName = method.getName();
044
045 if (methodName.equals("equals")) {
046 if (proxy == arguments[0]) {
047 return true;
048 }
049 else {
050 return false;
051 }
052 }
053
054 if (methodName.equals("hashCode")) {
055 return System.identityHashCode(proxy);
056 }
057
058 if (methodName.equals("addBatch") || methodName.equals("execute") ||
059 methodName.equals("executeQuery") ||
060 methodName.equals("executeUpdate")) {
061
062 if (ArrayUtil.isNotEmpty(arguments)) {
063 ResetDatabaseUtil.processSQL(
064 _connection, (String)arguments[0]);
065 }
066 }
067
068 return method.invoke(_statement, arguments);
069 }
070 catch (InvocationTargetException ite) {
071 throw ite.getTargetException();
072 }
073 }
074
075 private final Connection _connection;
076 private final Statement _statement;
077
078 }