001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
027     * @author Shuyang Zhou
028     */
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    }