<commit_before>/*
 * Copyright 2016 NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.navercorp.pinpoint.plugin.jdbc.mariadb;

import ch.vorburger.mariadb4j.DB;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author <NAME>
 */
public class MariaDB_IT_Base {

    private static final int PORT = 13306;
    protected static final String URL = "127.0.0.1:" + PORT;
    protected static final String DATABASE_NAME = "test";

    protected static final String JDBC_URL = "jdbc:mariadb://" + URL + "/" + DATABASE_NAME;

    // for Statement
    protected static final String STATEMENT_QUERY = "SELECT count(1) FROM playground";
    protected static final String STATEMENT_NORMALIZED_QUERY = "SELECT count(0#) FROM playground";

    // for Prepared Statement
    protected static final String PREPARED_STATEMENT_QUERY = "SELECT * FROM playground where id = ?";

    // for Callable Statement
    protected static final String PROCEDURE_NAME = "getPlaygroundByName";
    protected static final String CALLABLE_STATEMENT_QUERY = "{ CALL " + PROCEDURE_NAME + "(?, ?) }";
    protected static final String CALLABLE_STATEMENT_INPUT_PARAM = "TWO";
    protected static final int CALLABLE_STATMENT_OUTPUT_PARAM_TYPE = Types.INTEGER;

    private static DB TEST_DATABASE;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        TEST_DATABASE = DB.newEmbeddedDB(PORT);
        TEST_DATABASE.start();
        TEST_DATABASE.createDB("test");
        TEST_DATABASE.source("jdbc/mariadb/init.sql");
    }

    protected final void executeStatement() throws Exception {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(JDBC_URL, "root", null);

            Statement statement = null;
            try {
                statement = connection.createStatement();

                ResultSet result = null;
                try {
                    result = statement.executeQuery(STATEMENT_QUERY);
                    result.first();
                    assertEquals(3, result.getInt(1));
                } finally {
                    closeResultSet(result);
                }
            } finally {
                closeStatement(statement);
            }
        } finally {
            closeConnection(connection);
        }
    }

    protected final void executePreparedStatement() throws Exception {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(JDBC_URL, "root", null);

            PreparedStatement ps = null;
            try {
                ps = connection.prepareStatement(PREPARED_STATEMENT_QUERY);
                ps.setInt(1, 3);

                ResultSet result = null;
                try {
                    result = ps.executeQuery();
                    result.first();
                    assertEquals("THREE", result.getString(2));
                } finally {
                    closeResultSet(result);
                }
            } finally {
                closeStatement(ps);
            }
        } finally {
            closeConnection(connection);
        }
    }

    protected final void executeCallableStatement() throws Exception {

        final int expectedTotalCount = 3;
        final int expectedMatchingCount = 1;
        final int expectedMatchingId = 2;
        final String outputParamCountName = "outputParamCount";

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(JDBC_URL, "root", null);

            CallableStatement cs = null;
            try {
                cs = conn.prepareCall(CALLABLE_STATEMENT_QUERY);
                cs.setString(1, CALLABLE_STATEMENT_INPUT_PARAM);
                cs.registerOutParameter(2, CALLABLE_STATMENT_OUTPUT_PARAM_TYPE);

                ResultSet rs = null;
                try {
                    rs = cs.executeQuery();
                    int matchingCount = 0;
                    while (rs.next()) {
                        ++matchingCount;
                        if (matchingCount > expectedMatchingCount) {
                            fail();
                        }
                        assertEquals(expectedMatchingId, rs.getInt(1));
                        assertEquals(CALLABLE_STATEMENT_INPUT_PARAM, rs.getString(2));
                    }
                    assertEquals(expectedMatchingCount, matchingCount);<commit_msg>Fix mariadb plugin integration test mariadb connector 1.5.8 seems to throw an exception when resultset cursor is moved to an absolute index when Statement is created using their default methods.<commit_after>/*
 * Copyright 2016 NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.navercorp.pinpoint.plugin.jdbc.mariadb;

import ch.vorburger.mariadb4j.DB;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author <NAME>
 */
public class MariaDB_IT_Base {

    private static final int PORT = 13306;
    protected static final String URL = "127.0.0.1:" + PORT;
    protected static final String DATABASE_NAME = "test";

    protected static final String JDBC_URL = "jdbc:mariadb://" + URL + "/" + DATABASE_NAME;

    // for Statement
    protected static final String STATEMENT_QUERY = "SELECT count(1) FROM playground";
    protected static final String STATEMENT_NORMALIZED_QUERY = "SELECT count(0#) FROM playground";

    // for Prepared Statement
    protected static final String PREPARED_STATEMENT_QUERY = "SELECT * FROM playground where id = ?";

    // for Callable Statement
    protected static final String PROCEDURE_NAME = "getPlaygroundByName";
    protected static final String CALLABLE_STATEMENT_QUERY = "{ CALL " + PROCEDURE_NAME + "(?, ?) }";
    protected static final String CALLABLE_STATEMENT_INPUT_PARAM = "TWO";
    protected static final int CALLABLE_STATMENT_OUTPUT_PARAM_TYPE = Types.INTEGER;

    private static DB TEST_DATABASE;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        TEST_DATABASE = DB.newEmbeddedDB(PORT);
        TEST_DATABASE.start();
        TEST_DATABASE.createDB("test");
        TEST_DATABASE.source("jdbc/mariadb/init.sql");
    }

    protected final void executeStatement() throws Exception {
        final int expectedResultSize = 1;
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(JDBC_URL, "root", null);

            Statement statement = null;
            try {
                statement = connection.createStatement();

                ResultSet rs = null;
                try {
                    rs = statement.executeQuery(STATEMENT_QUERY);
                    int resultCount = 0;
                    while (rs.next()) {
                        ++resultCount;
                        if (resultCount > expectedResultSize) {
                            fail();
                        }
                        assertEquals(3, rs.getInt(1));
                    }
                    assertEquals(expectedResultSize, resultCount);
                } finally {
                    closeResultSet(rs);
                }
            } finally {
                closeStatement(statement);
            }
        } finally {
            closeConnection(connection);
        }
    }

    protected final void executePreparedStatement() throws Exception {
        final int expectedResultSize = 1;
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(JDBC_URL, "root", null);

            PreparedStatement ps = null;
            try {
                ps = connection.prepareStatement(PREPARED_STATEMENT_QUERY);
                ps.setInt(1, 3);

                ResultSet rs = null;
                try {
                    rs = ps.executeQuery();
                    int resultCount = 0;
                    while (rs.next()) {
                        ++resultCount;
                        if (resultCount > expectedResultSize) {
                            fail();
                        }
                        assertEquals("THREE", rs.getString(2));
                    }
                    assertEquals(expectedResultSize, resultCount);
                } finally {
                    closeResultSet(rs);
                }
            } finally {
                closeStatement(ps);
            }
        } finally {
            closeConnection(connection);
        }
    }

    protected final void executeCallableStatement() throws Exception {

        final int expectedResultSize = 1;
        final int expectedTotalCount = 3;
        final int expectedMatchingId = 2;
        final String outputParamCountName = "outputParamCount";

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(JDBC_URL, "root", null);

            CallableStatement cs = null;
            try {
                cs = conn.prepareCall(CALLABLE_STATEMENT_QUERY);
                cs.setString(1, CALLABLE_STATEMENT_INPUT_PARAM);
                cs.registerOutParameter(2, CALLABLE_STATMENT_OUTPUT_PARAM_TYPE);

                ResultSet rs = null;
                try {
                    rs = cs.executeQuery();
                    int resultCount = 0;
                    while (rs.next()) {
                        ++resultCount;
                        if (resultCount > expectedResultSize) {
                            fail();
                        }
                        assertEquals(expectedMatchingId, rs.getInt(1));
                        assertEquals(CALLABLE_STATEMENT_INPUT_PARAM, rs.getString(2));
                    }
                    assertEquals(expectedResultSize, resultCount);
{'fraction_non_alphanumeric': 0.080024595924104, 'fraction_numerical': 0.006763879128601546, 'mean_word_length': 1.964071856287425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1922565', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2743, 'n_words': 770}