/** * Validates and builds a {@link MongoItemWriter}. * * @return a {@link MongoItemWriter} */ public MongoItemWriter<T> build() { Assert.notNull(this.template, "template is required."); MongoItemWriter<T> writer = new MongoItemWriter<>(); writer.setTemplate(this.template); writer.setDelete(this.delete); writer.setCollection(this.collection); return writer; }
/** * If a transaction is active, buffer items to be written just before commit. * Otherwise write items using the provided template. * * @see org.springframework.batch.item.ItemWriter#write(List) */ @Override public void write(List<? extends T> items) throws Exception { if(!transactionActive()) { doWrite(items); return; } List<T> bufferedItems = getCurrentBuffer(); bufferedItems.addAll(items); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); writer = new MongoItemWriter<>(); writer.setTemplate(template); writer.afterPropertiesSet(); }
@Test public void testRemoveNoTransactionWithCollection() throws Exception { writer.setDelete(true); List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); writer.write(items); verify(template).remove(items.get(0), "collection"); verify(template).remove(items.get(1), "collection"); }
writers.add(i, new MongoItemWriter<>()); writers.get(i).setTemplate(mongoOperations); try { for(int i=0; i< limit; i++) { writers.get(i).write(Collections.singletonList(String.valueOf(i)));
@Test public void testWriteNoTransactionWithCollection() throws Exception { List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); writer.write(items); verify(template).save(items.get(0), "collection"); verify(template).save(items.get(1), "collection"); }
@Override public void beforeCommit(boolean readOnly) { List<T> items = (List<T>) TransactionSynchronizationManager.getResource(bufferKey); if(!CollectionUtils.isEmpty(items)) { if(!readOnly) { doWrite(items); } } }
@Test public void testWriteNoTransactionNoItems() throws Exception { writer.write(null); verifyZeroInteractions(template); }
@Test public void testWriteTransactionWithCollection() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception e) { fail("An exception was thrown while writing: " + e.getMessage()); } return null; }); verify(template).save(items.get(0), "collection"); verify(template).save(items.get(1), "collection"); }
@Override public void beforeCommit(boolean readOnly) { List<T> items = (List<T>) TransactionSynchronizationManager.getResource(bufferKey); if(!CollectionUtils.isEmpty(items)) { if(!readOnly) { doWrite(items); } } }
@Test public void testWriteNoTransactionNoCollection() throws Exception { List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.write(items); verify(template).save(items.get(0)); verify(template).save(items.get(1)); }
/** * Validates and builds a {@link MongoItemWriter}. * * @return a {@link MongoItemWriter} */ public MongoItemWriter<T> build() { Assert.notNull(this.template, "template is required."); MongoItemWriter<T> writer = new MongoItemWriter<>(); writer.setTemplate(this.template); writer.setDelete(this.delete); writer.setCollection(this.collection); return writer; }
@Test public void testAfterPropertiesSet() throws Exception { writer = new MongoItemWriter<>(); try { writer.afterPropertiesSet(); fail("Expected exception was not thrown"); } catch (IllegalStateException ignore) { } writer.setTemplate(template); writer.afterPropertiesSet(); }
/** * If a transaction is active, buffer items to be written just before commit. * Otherwise write items using the provided template. * * @see org.springframework.batch.item.ItemWriter#write(List) */ @Override public void write(List<? extends T> items) throws Exception { if(!transactionActive()) { doWrite(items); return; } List<T> bufferedItems = getCurrentBuffer(); bufferedItems.addAll(items); }
@Test public void testWriteTransactionFails() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); try { new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception ignore) { fail("unexpected exception thrown"); } throw new RuntimeException("force rollback"); }); } catch (RuntimeException re) { assertEquals(re.getMessage(), "force rollback"); } catch (Throwable t) { fail("Unexpected exception was thrown"); } verifyZeroInteractions(template); }
@Test public void testBasicWrite() throws Exception { MongoItemWriter<String> writer = new MongoItemWriterBuilder<String>().template(this.template).build(); writer.write(this.items); verify(this.template).save(this.items.get(0)); verify(this.template).save(this.items.get(1)); verify(this.template, never()).remove(this.items.get(0)); verify(this.template, never()).remove(this.items.get(1)); }
/** * A pointless use case but validates that the flag is still honored. * */ @Test public void testWriteTransactionReadOnly() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.setCollection("collection"); try { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setReadOnly(true); transactionTemplate.execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception ignore) { fail("unexpected exception thrown"); } return null; }); } catch (Throwable t) { fail("Unexpected exception was thrown"); } verifyZeroInteractions(template); }
@Test public void testWriteTransactionNoCollection() throws Exception { final List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> { try { writer.write(items); } catch (Exception e) { fail("An exception was thrown while writing: " + e.getMessage()); } return null; }); verify(template).save(items.get(0)); verify(template).save(items.get(1)); }
@Test public void testDelete() throws Exception { MongoItemWriter<String> writer = new MongoItemWriterBuilder<String>().template(this.template) .delete(true) .build(); writer.write(this.items); verify(this.template).remove(this.items.get(0)); verify(this.template).remove(this.items.get(1)); verify(this.template, never()).save(this.items.get(0)); verify(this.template, never()).save(this.items.get(1)); }