package org.fluentjdbc.internal.query;

import org.fluentjdbc.api.query.UpdateResult;
import org.fluentjdbc.internal.support.Ints;
import org.junit.Assert;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class FluentJdbcBatchTest extends UpdateTestBase {
    @Test
    public void batch() throws SQLException {
        int[] expectedUpdated = new int[]{1, 1};
        Iterator<List<Object>> params = Arrays.<List<Object>>asList(
            Arrays.asList(param1),
            Arrays.asList(param2)
        ).iterator();
        when(preparedStatement.executeBatch()).thenReturn(expectedUpdated);

        List<UpdateResult> updated = fluentJdbc.query().batch(query).params(params).run();

        assertThat(updated.size(), is(2));
        assertThat(updated.get(0).updated(), is(equalTo((long) expectedUpdated[0])));
        assertThat(updated.get(1).updated(), is(equalTo((long) expectedUpdated[1])));

        verify(preparedStatement).setObject(1, param1);
        verify(preparedStatement).setObject(1, param2);
        verify(preparedStatement, times(2)).addBatch();
        verify(preparedStatement).executeBatch();
        verify(preparedStatement).close();

    }
}

@@ -83,78 +83,8 @@
 lt;%0A
-import org.fluentjdbc.internal.support.Ints;%0Aimport org.junit.Assert;%0A
 impo
@@ -98,24 +98,24 @@
 junit.Test;%0A
+
 %0Aimport java
@@ -973,212 +973,52 @@
     
-assertThat(updated.size(), is(2));%0A        assertThat(updated.get(0).updated(), is(equalTo((long) expectedUpdated%5B0%5D)));%0A        assertThat(updated.get(1).updated(), is(equalTo((long) expectedU
+verifyUpdateResults(expectedUpdated, u
 pdated
-%5B1%5D))
 );%0A%0A
@@ -1274,21 +1274,1291 @@
 t).close();%0A
+    %7D%0A%0A    @Test%0A    public void batchUpdateWithBatchSize() throws SQLException %7B%0A        String thirdParam = %223%22;%0A        int%5B%5D expectedUpdated = new int%5B%5D%7B1, 1, 1%7D;%0A        Iterator%3CList%3CObject%3E%3E params = Arrays.%3CList%3CObject%3E%3EasList(%0A                Arrays.asList(param1),%0A                Arrays.asList(param2),%0A                Arrays.asList(thirdParam)%0A        ).iterator();%0A        when(preparedStatement.executeBatch()).thenReturn(new int%5B%5D%7B1, 1%7D, new int%5B%5D%7B1%7D);%0A        List%3CUpdateResult%3E updated = fluentJdbc.query().batch(query).batchSize(2).params(params).run();%0A%0A        verifyUpdateResults(expectedUpdated, updated);%0A%0A        verify(preparedStatement).setObject(1, param1);%0A        verify(preparedStatement).setObject(1, param2);%0A        verify(preparedStatement).setObject(1, thirdParam);%0A        verify(preparedStatement, times(3)).addBatch();%0A        verify(preparedStatement, times(2)).executeBatch();%0A        verify(preparedStatement).close();%0A    %7D%0A%0A    private void verifyUpdateResults(int%5B%5D expectedUpdated, List%3CUpdateResult%3E updated) %7B%0A        assertThat(updated.size(), is(expectedUpdated.length));%0A        for(int i = 0; i %3C updated.size(); ++i) %7B%0A            assertThat(updated.get(i).updated(), is(equalTo((long) expectedUpdated%5Bi%5D)));%0A        %7D
 %0A    %7D%0A%7D%0A

Test coverage for batch size