@Override public Void doInTransaction(TransactionStatus status) { reader.open(ec); reader.close(); return null; } });
@Override public Void doInTransaction(TransactionStatus status) { reader.open(ec); reader.close(); return null; } });
@Test public void testOverrideConnectionAutoCommit() throws Exception { boolean initialAutoCommit= false; boolean neededAutoCommit = true; DataSource ds = mock(DataSource.class); Connection con = mock(Connection.class); when(con.getAutoCommit()).thenReturn(initialAutoCommit); PreparedStatement ps = mock(PreparedStatement.class); when(con.prepareStatement("select foo from bar", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)).thenReturn(ps); when(ds.getConnection()).thenReturn(con); final JdbcCursorItemReader<String> reader = new JdbcCursorItemReader<>(); reader.setDataSource(ds); reader.setSql("select foo from bar"); reader.setConnectionAutoCommit(neededAutoCommit); // Check "open" outside of a transaction (see AbstractStep#execute()) final ExecutionContext ec = new ExecutionContext(); reader.open(ec); ArgumentCaptor<Boolean> autoCommitCaptor = ArgumentCaptor.forClass(Boolean.class); verify(con, times(1)).setAutoCommit(autoCommitCaptor.capture()); assertEquals(neededAutoCommit, autoCommitCaptor.getValue()); reset(con); reader.close(); // Check restored autocommit value autoCommitCaptor = ArgumentCaptor.forClass(Boolean.class); verify(con, times(1)).setAutoCommit(autoCommitCaptor.capture()); assertEquals(initialAutoCommit, autoCommitCaptor.getValue()); }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { JdbcCursorItemReader<Foo> reader = (JdbcCursorItemReader<Foo>) tested; reader.close(); reader.setSql("select ID from T_FOOS where ID < 0"); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@Transactional @Test public void testRead() throws Exception{ itemReader.open(new ExecutionContext()); Foo foo = itemReader.read(); assertEquals(2, foo.getId()); foo = itemReader.read(); assertEquals(3, foo.getId()); assertNull(itemReader.read()); }
@Test public void testQueryArgumentsList() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO WHERE FIRST > ? ORDER BY FIRST") .queryArguments(Arrays.asList(3)) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 4, "5", "6"); validateFoo(reader.read(), 7, "8", "9"); assertNull(reader.read()); }
@Test public void testSimpleScenario() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO ORDER BY FIRST") .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 1, "2", "3"); validateFoo(reader.read(), 4, "5", "6"); validateFoo(reader.read(), 7, "8", "9"); assertNull(reader.read()); }
@Test public void testMaxItemCount() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO ORDER BY FIRST") .maxItemCount(2) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 1, "2", "3"); validateFoo(reader.read(), 4, "5", "6"); assertNull(reader.read()); }
@Test public void testCurrentItemCount() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO ORDER BY FIRST") .currentItemCount(1) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 4, "5", "6"); validateFoo(reader.read(), 7, "8", "9"); assertNull(reader.read()); }
@Test public void testQueryArgumentsArray() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO WHERE FIRST > ? ORDER BY FIRST") .queryArguments(3) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 4, "5", "6"); validateFoo(reader.read(), 7, "8", "9"); assertNull(reader.read()); }
@Test public void testQueryArgumentsTypedArray() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO WHERE FIRST > ? ORDER BY FIRST") .queryArguments(new Integer[] {3}, new int[] {Types.BIGINT}) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 4, "5", "6"); validateFoo(reader.read(), 7, "8", "9"); assertNull(reader.read()); }
@Test public void testPreparedStatementSetter() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO WHERE FIRST > ? ORDER BY FIRST") .preparedStatementSetter(new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setInt(1, 3); } }) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 4, "5", "6"); validateFoo(reader.read(), 7, "8", "9"); assertNull(reader.read()); }
@Test public void testMaxRows() throws Exception { JdbcCursorItemReader<Foo> reader = new JdbcCursorItemReaderBuilder<Foo>() .dataSource(this.dataSource) .name("fooReader") .sql("SELECT * FROM FOO ORDER BY FIRST") .maxRows(2) .saveState(false) .rowMapper((rs, rowNum) -> { Foo foo = new Foo(); foo.setFirst(rs.getInt("FIRST")); foo.setSecond(rs.getString("SECOND")); foo.setThird(rs.getString("THIRD")); return foo; }) .build(); ExecutionContext executionContext = new ExecutionContext(); reader.open(executionContext); validateFoo(reader.read(), 1, "2", "3"); validateFoo(reader.read(), 4, "5", "6"); assertNull(reader.read()); reader.close(); assertEquals(0, executionContext.size()); }