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.rule.callback;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBManagerUtil;
019    import com.liferay.portal.kernel.dao.db.DBType;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    
024    import java.sql.Connection;
025    import java.sql.SQLException;
026    import java.sql.Statement;
027    
028    import org.junit.runner.Description;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class SybaseDumpTransactionLogTestCallback
034            extends BaseTestCallback<Void, Void> {
035    
036            public static final SybaseDumpTransactionLogTestCallback INSTANCE =
037                    new SybaseDumpTransactionLogTestCallback();
038    
039            @Override
040            public Void beforeClass(Description description) throws SQLException {
041                    SybaseDumpTransactionLog sybaseDumpTransactionLog =
042                            description.getAnnotation(SybaseDumpTransactionLog.class);
043    
044                    if (sybaseDumpTransactionLog != null) {
045                            SybaseDump[] sybaseDumps = sybaseDumpTransactionLog.dumpBefore();
046    
047                            if (!ArrayUtil.contains(sybaseDumps, SybaseDump.CLASS)) {
048                                    return null;
049                            }
050                    }
051    
052                    _dumpTransactionLog();
053    
054                    return null;
055            }
056    
057            @Override
058            public Void beforeMethod(Description description, Object target)
059                    throws SQLException {
060    
061                    Class<?> testClass = description.getTestClass();
062    
063                    SybaseDumpTransactionLog sybaseDumpTransactionLog =
064                            testClass.getAnnotation(SybaseDumpTransactionLog.class);
065    
066                    if (sybaseDumpTransactionLog != null) {
067                            SybaseDump[] sybaseDumps = sybaseDumpTransactionLog.dumpBefore();
068    
069                            if (ArrayUtil.contains(sybaseDumps, SybaseDump.METHOD)) {
070                                    _dumpTransactionLog();
071                            }
072                    }
073    
074                    return null;
075            }
076    
077            private SybaseDumpTransactionLogTestCallback() {
078            }
079    
080            private void _dumpTransactionLog() throws SQLException {
081                    DB db = DBManagerUtil.getDB();
082    
083                    if (db.getDBType() != DBType.SYBASE) {
084                            return;
085                    }
086    
087                    try (Connection connection = DataAccess.getConnection();
088                            Statement statement = connection.createStatement()) {
089    
090                            statement.execute(
091                                    "dump transaction " + connection.getCatalog() + " with no_log");
092                    }
093            }
094    
095    }