/** * Builds the {@link RepositoryItemWriter}. * * @return a {@link RepositoryItemWriter} */ public RepositoryItemWriter<T> build() { if (this.repositoryMethodReference != null) { this.methodName = this.repositoryMethodReference.getMethodName(); this.repository = this.repositoryMethodReference.getRepository(); } Assert.hasText(this.methodName, "methodName is required."); Assert.notNull(this.repository, "repository is required."); RepositoryItemWriter<T> writer = new RepositoryItemWriter<>(); writer.setMethodName(this.methodName); writer.setRepository(this.repository); return writer; }
/** * Performs the actual write to the repository. This can be overridden by * a subclass if necessary. * * @param items the list of items to be persisted. * * @throws Exception thrown if error occurs during writing. */ protected void doWrite(List<? extends T> items) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Writing to the repository with " + items.size() + " items."); } MethodInvoker invoker = createMethodInvoker(repository, methodName); for (T object : items) { invoker.setArguments(new Object [] {object}); doInvoke(invoker); } }
/** * Write all items to the data store via a Spring Data repository. * * @see org.springframework.batch.item.ItemWriter#write(java.util.List) */ @Override public void write(List<? extends T> items) throws Exception { if(!CollectionUtils.isEmpty(items)) { doWrite(items); } }
@Test public void testAfterPropertiesSet() throws Exception { writer.afterPropertiesSet(); writer.setRepository(null); try { writer.afterPropertiesSet(); fail(); } catch (IllegalStateException e) { } }
@Test public void testWriteItems() throws Exception { List<String> items = Collections.singletonList("foo"); writer.write(items); verify(repository).save("foo"); } }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); writer = new RepositoryItemWriter<>(); writer.setMethodName("save"); writer.setRepository(repository); }
/** * Performs the actual write to the repository. This can be overridden by * a subclass if necessary. * * @param items the list of items to be persisted. * * @throws Exception thrown if error occurs during writing. */ protected void doWrite(List<? extends T> items) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Writing to the repository with " + items.size() + " items."); } MethodInvoker invoker = createMethodInvoker(repository, methodName); for (T object : items) { invoker.setArguments(new Object [] {object}); doInvoke(invoker); } }
/** * Write all items to the data store via a Spring Data repository. * * @see org.springframework.batch.item.ItemWriter#write(java.util.List) */ @Override public void write(List<? extends T> items) throws Exception { if(!CollectionUtils.isEmpty(items)) { doWrite(items); } }
@Test public void testWriteItemsTestRepository() throws Exception { RepositoryItemWriter<String> writer = new RepositoryItemWriterBuilder<String>() .methodName("foo") .repository(this.repository) .build(); List<String> items = Collections.singletonList("foo"); writer.write(items); verify(this.repository).foo("foo"); }
/** * Builds the {@link RepositoryItemWriter}. * * @return a {@link RepositoryItemWriter} */ public RepositoryItemWriter<T> build() { if (this.repositoryMethodReference != null) { this.methodName = this.repositoryMethodReference.getMethodName(); this.repository = this.repositoryMethodReference.getRepository(); } Assert.hasText(this.methodName, "methodName is required."); Assert.notNull(this.repository, "repository is required."); RepositoryItemWriter<T> writer = new RepositoryItemWriter<>(); writer.setMethodName(this.methodName); writer.setRepository(this.repository); return writer; }
@Test public void testWriteItems() throws Exception { RepositoryItemWriter<String> writer = new RepositoryItemWriterBuilder<String>() .methodName("save") .repository(this.repository) .build(); List<String> items = Collections.singletonList("foo"); writer.write(items); verify(this.repository).save("foo"); }
@Test public void testWriteItemsTestRepositoryMethodIs() throws Exception { RepositoryItemWriterBuilder.RepositoryMethodReference<TestRepository> repositoryMethodReference = new RepositoryItemWriterBuilder.RepositoryMethodReference<>( this.repository); repositoryMethodReference.methodIs().foo(null); RepositoryItemWriter<String> writer = new RepositoryItemWriterBuilder<String>() .methodName("foo") .repository(repositoryMethodReference) .build(); List<String> items = Collections.singletonList("foo"); writer.write(items); verify(this.repository).foo("foo"); }