<commit_before>package org.zanata.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import lombok.Cleanup;

import org.assertj.core.api.Fail;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.junit.Before;
import org.junit.Test;
import org.zanata.ZanataDbunitJpaTest;

public class WrappedConnectionProviderTest extends ZanataDbunitJpaTest {

    private Session session;

    @Override
    protected void prepareDBUnitOperations() {
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/ProjectsData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/TextFlowTestData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/LocalesData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/AccountData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
    }

    @Before
    public void setup() {
        session = getSession();
    }

    @Test
    public void testWrapperWithNestedExecute() throws Exception {
        @Cleanup
        ScrollableResults scroll1 = streamQuery("from HTextFlow");
        try {
            session.doWork(new Work() {
                @Override
                public void execute(Connection connection) throws SQLException {
                    Statement statement = connection.createStatement();
                    statement.executeUpdate("delete from HTextFlow where 0=1");
                }
            });
            concurrentResultSetNotDetected();
        } catch (JDBCException e) {
            checkExceptionType(e);
        }
    }

    @Test
    public void testWrapperWithNestedStreaming() throws Exception {
        @Cleanup
        ScrollableResults scroll1 = streamQuery("from HTextFlow");
        try {
            @Cleanup
            ScrollableResults scroll2 = streamQuery("from HTextFlowTarget");
            concurrentResultSetNotDetected();
        } catch (JDBCException e) {
            checkExceptionType(e);
        }
    }

    @Test
    public void testWrapperWithNestedResults() throws Exception {
        @Cleanup
        ScrollableResults scroll1 = streamQuery("from HTextFlow");
        try {
            @Cleanup
            ScrollableResults scroll2 = scrollQuery("from HTextFlowTarget");
            concurrentResultSetNotDetected();
        } catch (JDBCException e) {
            checkExceptionType(e);
        }
    }

    private void concurrentResultSetNotDetected() {<commit_msg>Add warning log before expected exception<commit_after>package org.zanata.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import lombok.Cleanup;

import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.Fail;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.junit.Before;
import org.junit.Test;
import org.zanata.ZanataDbunitJpaTest;

@Slf4j
public class WrappedConnectionProviderTest extends ZanataDbunitJpaTest {

    private Session session;

    @Override
    protected void prepareDBUnitOperations() {
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/ProjectsData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/TextFlowTestData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/LocalesData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
        beforeTestOperations.add(new DataSetOperation(
                "org/zanata/test/model/AccountData.dbunit.xml",
                DatabaseOperation.CLEAN_INSERT));
    }

    @Before
    public void setup() {
        session = getSession();
    }

    @Test
    public void testWrapperWithNestedExecute() throws Exception {
        @Cleanup
        ScrollableResults scroll1 = streamQuery("from HTextFlow");
        try {
            log.warn("This test is about to trigger a StreamingResultSetSQLException");
            session.doWork(new Work() {
                @Override
                public void execute(Connection connection) throws SQLException {
                    Statement statement = connection.createStatement();
                    statement.executeUpdate("delete from HTextFlow where 0=1");
                }
            });
            concurrentResultSetNotDetected();
        } catch (JDBCException e) {
            checkExceptionType(e);
        }
    }

    @Test
    public void testWrapperWithNestedStreaming() throws Exception {
        @Cleanup
        ScrollableResults scroll1 = streamQuery("from HTextFlow");
        try {
            log.warn("This test is about to trigger a StreamingResultSetSQLException");
            @Cleanup
            ScrollableResults scroll2 = streamQuery("from HTextFlowTarget");
            concurrentResultSetNotDetected();
        } catch (JDBCException e) {
            checkExceptionType(e);
        }
    }

    @Test
    public void testWrapperWithNestedResults() throws Exception {
        @Cleanup
        ScrollableResults scroll1 = streamQuery("from HTextFlow");
        try {
            log.warn("This test is about to trigger a StreamingResultSetSQLException");
            @Cleanup
            ScrollableResults scroll2 = scrollQuery("from HTextFlowTarget");
            concurrentResultSetNotDetected();
        } catch (JDBCException e) {
            checkExceptionType(e);
        }
    }

    private void concurrentResultSetNotDetected() {
{'fraction_non_alphanumeric': 0.08268281025967819, 'fraction_numerical': 0.002708300143380596, 'mean_word_length': 2.536901408450704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, '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': '1697744', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1605, 'n_words': 326}