/*
 * 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 org.jdbi.v3.core.statement;

import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Something;
import org.jdbi.v3.core.result.NoResultsException;
import org.jdbi.v3.core.result.ResultProducers;
import org.jdbi.v3.core.rule.H2DatabaseRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestStatements {
    @Rule
    public H2DatabaseRule dbRule = new H2DatabaseRule();

    private Handle h;

    @Before
    public void setUp() throws Exception {
        h = dbRule.openHandle();
    }

    @After
    public void doTearDown() throws Exception {
        if (h != null) {
            h.close();
        }
    }

    @Test
    public void testStatement() {
        int rows = h.createUpdate("insert into something (id, name) values (1, 'eric')").execute();
        assertThat(rows).isEqualTo(1);
    }

    @Test
    public void testSimpleInsert() {
        int c = h.execute("insert into something (id, name) values (1, 'eric')");
        assertThat(c).isEqualTo(1);
    }

    @Test
    public void testUpdate() {
        h.execute("insert into something (id, name) values (1, 'eric')");
        h.createUpdate("update something set name = 'ERIC' where id = 1").execute();
        Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
        assertThat(eric.getName()).isEqualTo("ERIC");
    }

    @Test
    public void testSimpleUpdate() {
        h.execute("insert into something (id, name) values (1, 'eric')");
        h.execute("update something set name = 'cire' where id = 1");
        Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
        assertThat(eric.getName()).isEqualTo("cire");
    }

    @Test
    public void testStatementWithRequiredResults() {
        assertThatThrownBy(() -> h.createQuery("commit").mapTo(Integer.class).findFirst()).isInstanceOf(NoResultsException.class);
    }

    @Test
    public void testStatementWithOptionalResults() {
        h.getConfig(ResultProducers.class).allowNoResults(true);
        assertThat(h.createQuery("commit").mapTo(Integer.class).findFirst()).isEmpty();
    }

    @Test
    public void testStatementWithOptionalBeanResults() {
        h.getConfig(ResultProducers.class).allowNoResults(true);
        assertThat(h.createQuery("commit").mapToBean(Object.class).findFirst()).isEmpty();
    }

    @Test
    public void testStatementWithOptionalMapResults() {
        h.getConfig(ResultProducers.class).allowNoResults(true);
        assertThat(h.createQuery("commit").mapToMap().findFirst()).isEmpty();
    }
}

@@ -584,24 +584,54 @@
 statement;%0A%0A
+import java.sql.SQLException;%0A
 import org.j
@@ -3375,11 +3375,568 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testTimeout() %7B%0A        h.execute(%22CREATE ALIAS slow_Loris FOR %5C%22org.jdbi.v3.core.statement.TestStatements.slowLoris%5C%22;%22);%0A%0A        h.getConfig(SqlStatements.class).setQueryTimeout(1);%0A%0A        assertThatThrownBy(h.createQuery(%22select * from values(slow_loris())%22).mapTo(String.class)::findOnly)%0A            .isInstanceOf(SQLException.class);%0A    %7D%0A%0A    @SuppressWarnings(%22unused%22)%0A    // for testTimeout%0A    public static String slowLoris() throws InterruptedException %7B%0A        Thread.sleep(2000);%0A        return %22loris%22;%0A    %7D
 %0A%7D%0A

test attempt