@Test public void testWriteAndFlush() throws Exception { ps.addBatch(); when(ps.executeBatch()).thenReturn(new int[] { 123 }); writer.write(Collections.singletonList("bar")); assertEquals(2, list.size()); assertTrue(list.contains("SQL")); }
@Test public void testWriteAndFlushWithEmptyUpdate() throws Exception { ps.addBatch(); when(ps.executeBatch()).thenReturn(new int[] { 0 }); try { writer.write(Collections.singletonList("bar")); fail("Expected EmptyResultDataAccessException"); } catch (EmptyResultDataAccessException e) { // expected String message = e.getMessage(); assertTrue("Wrong message: " + message, message.indexOf("did not update") >= 0); } assertEquals(2, list.size()); assertTrue(list.contains("SQL")); }
@SuppressWarnings({ "rawtypes", "serial", "unchecked" }) @Test public void testWriteAndFlushMap() throws Exception { JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>(); mapWriter.setSql(sql); mapWriter.setJdbcTemplate(namedParameterJdbcOperations); mapWriter.afterPropertiesSet(); ArgumentCaptor<Map []> captor = ArgumentCaptor.forClass(Map[].class); when(namedParameterJdbcOperations.batchUpdate(eq(sql), captor.capture())) .thenReturn(new int[] {1}); mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}})); assertEquals(1, captor.getValue().length); Map<String, Object> results = captor.getValue()[0]; assertEquals("bar", results.get("foo")); }
@Test public void testWriteAndFlush() throws Exception { when(namedParameterJdbcOperations.batchUpdate(eq(sql), eqSqlParameterSourceArray(new SqlParameterSource[] {new BeanPropertySqlParameterSource(new Foo("bar"))}))) .thenReturn(new int[] {1}); writer.write(Collections.singletonList(new Foo("bar"))); }
@Test public void testWriteAndFlushWithFailure() throws Exception { final RuntimeException ex = new RuntimeException("ERROR"); when(namedParameterJdbcOperations.batchUpdate(eq(sql), eqSqlParameterSourceArray(new SqlParameterSource[] {new BeanPropertySqlParameterSource(new Foo("bar"))}))) .thenThrow(ex); try { writer.write(Collections.singletonList(new Foo("bar"))); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals("ERROR", e.getMessage()); } }
@Test public void testCustomPreparedStatementSetter() throws Exception { JdbcBatchItemWriter<Map<String, Object>> writer = new JdbcBatchItemWriterBuilder<Map<String, Object>>() .itemPreparedStatementSetter((item, ps) -> { ps.setInt(0, (int) item.get("first")); ps.setString(1, (String) item.get("second")); ps.setString(2, (String) item.get("third")); }) .dataSource(this.dataSource) .sql("INSERT INTO FOO (first, second, third) VALUES (:first, :second, :third)") .build(); writer.afterPropertiesSet(); List<Map<String, Object>> items = buildMapItems(); writer.write(items); verifyWrite(); }
@Test public void testWriteAndFlushWithEmptyUpdate() throws Exception { when(namedParameterJdbcOperations.batchUpdate(eq(sql), eqSqlParameterSourceArray(new SqlParameterSource[] {new BeanPropertySqlParameterSource(new Foo("bar"))}))) .thenReturn(new int[] {0}); try { writer.write(Collections.singletonList(new Foo("bar"))); fail("Expected EmptyResultDataAccessException"); } catch (EmptyResultDataAccessException e) { // expected String message = e.getMessage(); assertTrue("Wrong message: " + message, message.contains("did not update")); } }
@SuppressWarnings({ "rawtypes", "serial", "unchecked" }) @Test public void testWriteAndFlushMapWithItemSqlParameterSourceProvider() throws Exception { JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>(); mapWriter.setSql(sql); mapWriter.setJdbcTemplate(namedParameterJdbcOperations); mapWriter.setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider<Map<String, Object>>() { @Override public SqlParameterSource createSqlParameterSource(Map<String, Object> item) { return new MapSqlParameterSource(item); } }); mapWriter.afterPropertiesSet(); ArgumentCaptor<SqlParameterSource []> captor = ArgumentCaptor.forClass(SqlParameterSource[].class); when(namedParameterJdbcOperations.batchUpdate(any(String.class), captor.capture())) .thenReturn(new int[] {1}); mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}})); assertEquals(1, captor.getValue().length); SqlParameterSource results = captor.getValue()[0]; assertEquals("bar", results.getValue("foo")); }
@Test public void testBasicPojo() throws Exception { JdbcBatchItemWriter<Foo> writer = new JdbcBatchItemWriterBuilder<Foo>() .beanMapped() .dataSource(this.dataSource) .sql("INSERT INTO FOO (first, second, third) VALUES (:first, :second, :third)") .build(); writer.afterPropertiesSet(); List<Foo> items = new ArrayList<>(3); items.add(new Foo(1, "two", "three")); items.add(new Foo(4, "five", "six")); items.add(new Foo(7, "eight", "nine")); writer.write(items); verifyWrite(); }
@Test(expected = EmptyResultDataAccessException.class) public void testAssertUpdates() throws Exception { JdbcBatchItemWriter<Foo> writer = new JdbcBatchItemWriterBuilder<Foo>() .beanMapped() .dataSource(this.dataSource) .sql("UPDATE FOO SET second = :second, third = :third WHERE first = :first") .assertUpdates(true) .build(); writer.afterPropertiesSet(); List<Foo> items = new ArrayList<>(1); items.add(new Foo(1, "two", "three")); writer.write(items); }
@Test public void testBasicMap() throws Exception { JdbcBatchItemWriter<Map<String, Object>> writer = new JdbcBatchItemWriterBuilder<Map<String, Object>>() .columnMapped() .dataSource(this.dataSource) .sql("INSERT INTO FOO (first, second, third) VALUES (:first, :second, :third)") .build(); writer.afterPropertiesSet(); List<Map<String, Object>> items = buildMapItems(); writer.write(items); verifyWrite(); }
@Test public void testCustomPSqlParameterSourceProvider() throws Exception { JdbcBatchItemWriter<Map<String, Object>> writer = new JdbcBatchItemWriterBuilder<Map<String, Object>>() .itemSqlParameterSourceProvider(MapSqlParameterSource::new) .dataSource(this.dataSource) .sql("INSERT INTO FOO (first, second, third) VALUES (:first, :second, :third)") .build(); writer.afterPropertiesSet(); List<Map<String, Object>> items = buildMapItems(); writer.write(items); verifyWrite(); }
@Test public void testCustomJdbcTemplate() throws Exception { NamedParameterJdbcOperations template = new NamedParameterJdbcTemplate(this.dataSource); JdbcBatchItemWriter<Map<String, Object>> writer = new JdbcBatchItemWriterBuilder<Map<String, Object>>() .columnMapped() .namedParametersJdbcTemplate(template) .sql("INSERT INTO FOO (first, second, third) VALUES (:first, :second, :third)") .build(); writer.afterPropertiesSet(); List<Map<String, Object>> items = buildMapItems(); writer.write(items); verifyWrite(); Object usedTemplate = ReflectionTestUtils.getField(writer, "namedParameterJdbcTemplate"); assertTrue(template == usedTemplate); }