@Test public void testWriteNoTransactionNoItems() throws Exception { writer.write(null); verifyZeroInteractions(template); }
@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)); }
@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"); }
@Test public void testRemoveNoTransactionNoCollection() throws Exception { writer.setDelete(true); List<Object> items = new ArrayList<Object>() {{ add(new Object()); add(new Object()); }}; writer.write(items); verify(template).remove(items.get(0)); verify(template).remove(items.get(1)); }
@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"); }
@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)); }
@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"); }
@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)); }
@Test public void testWriteToCollection() throws Exception { MongoItemWriter<String> writer = new MongoItemWriterBuilder<String>().collection("collection") .template(this.template) .build(); writer.write(this.items); verify(this.template).save(this.items.get(0), "collection"); verify(this.template).save(this.items.get(1), "collection"); verify(this.template, never()).remove(this.items.get(0), "collection"); verify(this.template, never()).remove(this.items.get(1), "collection"); }
try { for(int i=0; i< limit; i++) { writers.get(i).write(Collections.singletonList(String.valueOf(i)));
@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); }
/** * 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); }