Upgraded Mockito
This commit upgrades mockito to the latest currently available version as well as does some additional cleanup.
This commit is contained in:
@@ -88,7 +88,7 @@ allprojects {
|
||||
junitVersion = '4.12'
|
||||
log4jVersion = '1.2.17'
|
||||
mysqlVersion = '5.1.39'
|
||||
mockitoVersion = '1.10.19'
|
||||
mockitoVersion = '2.6.8'
|
||||
postgresqlVersion = '9.4.1211.jre7'
|
||||
quartzVersion = '2.2.3'
|
||||
servletApiVersion = '3.1.0'
|
||||
|
||||
@@ -15,14 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.core.launch.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -34,6 +26,7 @@ import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
@@ -50,20 +43,25 @@ import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.batch.core.launch.JobInstanceAlreadyExistsException;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.launch.NoSuchJobException;
|
||||
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
|
||||
import org.springframework.batch.core.launch.NoSuchJobInstanceException;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.StoppableTasklet;
|
||||
import org.springframework.batch.core.step.tasklet.TaskletStep;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Will Schipp
|
||||
@@ -81,22 +79,13 @@ public class SimpleJobOperatorTests {
|
||||
|
||||
private JobParameters jobParameters;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
job = new JobSupport("foo") {
|
||||
@Override
|
||||
public JobParametersIncrementer getJobParametersIncrementer() {
|
||||
return new JobParametersIncrementer() {
|
||||
@Override
|
||||
public JobParameters getNext(JobParameters parameters) {
|
||||
return jobParameters;
|
||||
}
|
||||
};
|
||||
return parameters -> jobParameters;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -113,17 +102,11 @@ public class SimpleJobOperatorTests {
|
||||
|
||||
@Override
|
||||
public Set<String> getJobNames() {
|
||||
return new HashSet<String>(Arrays.asList(new String[] { "foo", "bar" }));
|
||||
return new HashSet<>(Arrays.asList(new String[] { "foo", "bar" }));
|
||||
}
|
||||
});
|
||||
|
||||
jobOperator.setJobLauncher(new JobLauncher() {
|
||||
@Override
|
||||
public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException,
|
||||
JobRestartException, JobInstanceAlreadyCompleteException {
|
||||
return new JobExecution(new JobInstance(123L, job.getName()), 999L, jobParameters, null);
|
||||
}
|
||||
});
|
||||
jobOperator.setJobLauncher((job, jobParameters) -> new JobExecution(new JobInstance(123L, job.getName()), 999L, jobParameters, null));
|
||||
|
||||
jobExplorer = mock(JobExplorer.class);
|
||||
|
||||
@@ -165,7 +148,6 @@ public class SimpleJobOperatorTests {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.launch.support.SimpleJobOperator#startNextInstance(java.lang.String)}
|
||||
* .
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testStartNextInstanceSunnyDay() throws Exception {
|
||||
@@ -374,7 +356,7 @@ public class SimpleJobOperatorTests {
|
||||
|
||||
when(step.getTasklet()).thenReturn(tasklet);
|
||||
when(step.getName()).thenReturn("test_job.step1");
|
||||
when(jobRegistry.getJob(anyString())).thenReturn(job);
|
||||
when(jobRegistry.getJob(any(String.class))).thenReturn(job);
|
||||
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
|
||||
|
||||
jobOperator.setJobRegistry(jobRegistry);
|
||||
@@ -410,7 +392,7 @@ public class SimpleJobOperatorTests {
|
||||
|
||||
when(step.getTasklet()).thenReturn(tasklet);
|
||||
when(step.getName()).thenReturn("test_job.step1");
|
||||
when(jobRegistry.getJob(anyString())).thenReturn(job);
|
||||
when(jobRegistry.getJob(any(String.class))).thenReturn(job);
|
||||
when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution);
|
||||
|
||||
jobOperator.setJobRegistry(jobRegistry);
|
||||
@@ -453,7 +435,7 @@ public class SimpleJobOperatorTests {
|
||||
|
||||
@Override
|
||||
public Collection<String> getStepNames() {
|
||||
return Arrays.asList("test_job.step1");
|
||||
return Collections.singletonList("test_job.step1");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -60,7 +60,9 @@ public abstract class CheckpointSupport extends ItemStreamSupport{
|
||||
public void open(ExecutionContext executionContext)
|
||||
throws ItemStreamException {
|
||||
try {
|
||||
doOpen((Serializable) executionContext.get(getExecutionContextKey(checkpointKey)));
|
||||
String executionContextKey = getExecutionContextKey(checkpointKey);
|
||||
Serializable checkpoint = (Serializable) executionContext.get(executionContextKey);
|
||||
doOpen(checkpoint);
|
||||
} catch (Exception e) {
|
||||
throw new ItemStreamException(e);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,7 +33,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class MongoItemReaderTests {
|
||||
@@ -46,9 +46,9 @@ public class MongoItemReaderTests {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
reader = new MongoItemReader<String>();
|
||||
reader = new MongoItemReader<>();
|
||||
|
||||
sortOptions = new HashMap<String, Sort.Direction>();
|
||||
sortOptions = new HashMap<>();
|
||||
sortOptions.put("name", Sort.Direction.DESC);
|
||||
|
||||
reader.setTemplate(template);
|
||||
@@ -61,7 +61,7 @@ public class MongoItemReaderTests {
|
||||
|
||||
@Test
|
||||
public void testAfterPropertiesSet() throws Exception{
|
||||
reader = new MongoItemReader<String>();
|
||||
reader = new MongoItemReader<>();
|
||||
|
||||
try {
|
||||
reader.afterPropertiesSet();
|
||||
@@ -114,7 +114,7 @@ public class MongoItemReaderTests {
|
||||
public void testBasicQueryFirstPage() {
|
||||
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
|
||||
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
|
||||
|
||||
assertFalse(reader.doPageRead().hasNext());
|
||||
|
||||
@@ -130,7 +130,7 @@ public class MongoItemReaderTests {
|
||||
reader.page = 2;
|
||||
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
|
||||
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
|
||||
|
||||
assertFalse(reader.doPageRead().hasNext());
|
||||
|
||||
@@ -148,7 +148,7 @@ public class MongoItemReaderTests {
|
||||
reader.setFields("{name : 1, age : 1, _id: 0}");
|
||||
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
|
||||
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
|
||||
|
||||
assertFalse(reader.doPageRead().hasNext());
|
||||
|
||||
@@ -167,7 +167,7 @@ public class MongoItemReaderTests {
|
||||
reader.setHint("{ $natural : 1}");
|
||||
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
|
||||
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
|
||||
|
||||
assertFalse(reader.doPageRead().hasNext());
|
||||
|
||||
@@ -189,7 +189,7 @@ public class MongoItemReaderTests {
|
||||
reader.setQuery("{ name : ?0 }");
|
||||
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
|
||||
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
|
||||
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
|
||||
|
||||
assertFalse(reader.doPageRead().hasNext());
|
||||
|
||||
@@ -212,7 +212,7 @@ public class MongoItemReaderTests {
|
||||
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
|
||||
ArgumentCaptor<String> collectionContainer = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
when(template.find(queryContainer.capture(), eq(String.class), collectionContainer.capture())).thenReturn(new ArrayList<String>());
|
||||
when(template.find(queryContainer.capture(), eq(String.class), collectionContainer.capture())).thenReturn(new ArrayList<>());
|
||||
|
||||
assertFalse(reader.doPageRead().hasNext());
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,14 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.data;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -31,15 +23,21 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class MongoItemWriterTests {
|
||||
|
||||
@@ -51,19 +49,19 @@ public class MongoItemWriterTests {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
writer = new MongoItemWriter<Object>();
|
||||
writer = new MongoItemWriter<>();
|
||||
writer.setTemplate(template);
|
||||
writer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
writer = new MongoItemWriter<Object>();
|
||||
writer = new MongoItemWriter<>();
|
||||
|
||||
try {
|
||||
writer.afterPropertiesSet();
|
||||
fail("Expected exception was not thrown");
|
||||
} catch (IllegalStateException iae) {
|
||||
} catch (IllegalStateException ignore) {
|
||||
}
|
||||
|
||||
writer.setTemplate(template);
|
||||
@@ -112,18 +110,14 @@ public class MongoItemWriterTests {
|
||||
add(new Object());
|
||||
}};
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
} catch (Exception e) {
|
||||
fail("An exception was thrown while writing: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
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));
|
||||
@@ -139,18 +133,14 @@ public class MongoItemWriterTests {
|
||||
|
||||
writer.setCollection("collection");
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
} catch (Exception e) {
|
||||
fail("An exception was thrown while writing: " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
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");
|
||||
@@ -167,17 +157,13 @@ public class MongoItemWriterTests {
|
||||
writer.setCollection("collection");
|
||||
|
||||
try {
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
} catch (Exception ignore) {
|
||||
fail("unexpected exception thrown");
|
||||
}
|
||||
throw new RuntimeException("force rollback");
|
||||
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");
|
||||
@@ -191,7 +177,6 @@ public class MongoItemWriterTests {
|
||||
/**
|
||||
* A pointless use case but validates that the flag is still honored.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testWriteTransactionReadOnly() throws Exception {
|
||||
@@ -205,17 +190,13 @@ public class MongoItemWriterTests {
|
||||
try {
|
||||
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
transactionTemplate.setReadOnly(true);
|
||||
transactionTemplate.execute(new TransactionCallback<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write(items);
|
||||
} catch (Exception ignore) {
|
||||
fail("unexpected exception thrown");
|
||||
}
|
||||
return null;
|
||||
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");
|
||||
@@ -265,37 +246,30 @@ public class MongoItemWriterTests {
|
||||
final int index = i;
|
||||
MongoOperations mongoOperations = mock(MongoOperations.class);
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation)
|
||||
throws Throwable {
|
||||
String val = (String) invocation.getArguments()[0];
|
||||
if(results[index] == null) {
|
||||
results[index] = val;
|
||||
} else {
|
||||
results[index] += val;
|
||||
}
|
||||
return null;
|
||||
doAnswer(invocation -> {
|
||||
String val = (String) invocation.getArguments()[0];
|
||||
if(results[index] == null) {
|
||||
results[index] = val;
|
||||
} else {
|
||||
results[index] += val;
|
||||
}
|
||||
return null;
|
||||
}).when(mongoOperations).save(any(String.class));
|
||||
writers[i] = new MongoItemWriter<String>();
|
||||
writers[i] = new MongoItemWriter<>();
|
||||
writers[i].setTemplate(mongoOperations);
|
||||
}
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
for(int i=0; i< limit; i++) {
|
||||
writers[i].write(Collections.singletonList(String.valueOf(i)));
|
||||
}
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
for(int i=0; i< limit; i++) {
|
||||
writers[i].write(Collections.singletonList(String.valueOf(i)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Unexpected Exception", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Unexpected Exception", e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
for(int i=0; i< limit; i++) {
|
||||
assertEquals(String.valueOf(i), results[i]);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,8 +34,8 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Matchers.isNull;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class Neo4jItemReaderTests {
|
||||
@@ -160,7 +160,7 @@ public class Neo4jItemReaderTests {
|
||||
|
||||
ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
when(template.queryForObjects(eq(String.class), query.capture(), (Map<String, Object>) isNull())).thenReturn(null);
|
||||
when(template.queryForObjects(eq(String.class), query.capture(), isNull())).thenReturn(null);
|
||||
|
||||
assertFalse(itemReader.doPageRead().hasNext());
|
||||
assertEquals("START n=node(*) RETURN * ORDER BY n.age SKIP 0 LIMIT 50", query.getValue());
|
||||
@@ -174,7 +174,7 @@ public class Neo4jItemReaderTests {
|
||||
ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
when(this.sessionFactory.openSession()).thenReturn(this.session);
|
||||
when(this.session.query(eq(String.class), query.capture(), (Map<String, Object>) isNull())).thenReturn(null);
|
||||
when(this.session.query(eq(String.class), query.capture(), isNull())).thenReturn(null);
|
||||
|
||||
assertFalse(itemReader.doPageRead().hasNext());
|
||||
assertEquals("START n=node(*) RETURN * ORDER BY n.age SKIP 0 LIMIT 50", query.getValue());
|
||||
@@ -186,8 +186,8 @@ public class Neo4jItemReaderTests {
|
||||
Neo4jItemReader<String> itemReader = buildTemplateBasedReader();
|
||||
ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
when(template.queryForObjects(eq(String.class), query.capture(), (Map<String, Object>) isNull())).thenReturn(result);
|
||||
when(result.iterator()).thenReturn(Collections.<String>emptyIterator());
|
||||
when(template.queryForObjects(eq(String.class), query.capture(), isNull())).thenReturn(result);
|
||||
when(result.iterator()).thenReturn(Collections.emptyIterator());
|
||||
|
||||
assertFalse(itemReader.doPageRead().hasNext());
|
||||
assertEquals("START n=node(*) RETURN * ORDER BY n.age SKIP 0 LIMIT 50", query.getValue());
|
||||
@@ -200,7 +200,7 @@ public class Neo4jItemReaderTests {
|
||||
ArgumentCaptor<String> query = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
when(this.sessionFactory.openSession()).thenReturn(this.session);
|
||||
when(this.session.query(eq(String.class), query.capture(), (Map<String, Object>) isNull())).thenReturn(result);
|
||||
when(this.session.query(eq(String.class), query.capture(), isNull())).thenReturn(result);
|
||||
when(result.iterator()).thenReturn(Collections.emptyIterator());
|
||||
|
||||
assertFalse(itemReader.doPageRead().hasNext());
|
||||
@@ -241,7 +241,7 @@ public class Neo4jItemReaderTests {
|
||||
@Test
|
||||
public void testResultsWithMatchAndWhereWithParameters() throws Exception {
|
||||
Neo4jItemReader<String> itemReader = buildTemplateBasedReader();
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("foo", "bar");
|
||||
itemReader.setParameterValues(params);
|
||||
itemReader.setMatchStatement("n -- m");
|
||||
|
||||
@@ -15,14 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.item.database;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -32,11 +24,20 @@ import org.hamcrest.Description;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
* @author Will Schipp
|
||||
@@ -44,7 +45,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
*/
|
||||
public class JdbcBatchItemWriterNamedParameterTests {
|
||||
|
||||
private JdbcBatchItemWriter<Foo> writer = new JdbcBatchItemWriter<Foo>();
|
||||
private JdbcBatchItemWriter<Foo> writer = new JdbcBatchItemWriter<>();
|
||||
|
||||
private NamedParameterJdbcOperations namedParameterJdbcOperations;
|
||||
|
||||
@@ -84,7 +85,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
writer.setSql(sql);
|
||||
writer.setJdbcTemplate(namedParameterJdbcOperations);
|
||||
writer.setItemSqlParameterSourceProvider(
|
||||
new BeanPropertyItemSqlParameterSourceProvider<Foo>());
|
||||
new BeanPropertyItemSqlParameterSourceProvider<>());
|
||||
writer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -92,11 +93,10 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.database.JdbcBatchItemWriter#afterPropertiesSet()}
|
||||
* .
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
writer = new JdbcBatchItemWriter<Foo>();
|
||||
writer = new JdbcBatchItemWriter<>();
|
||||
try {
|
||||
writer.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
@@ -104,7 +104,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
String message = e.getMessage();
|
||||
assertTrue("Message does not contain 'NamedParameterJdbcTemplate'.", message.indexOf("NamedParameterJdbcTemplate") >= 0);
|
||||
assertTrue("Message does not contain 'NamedParameterJdbcTemplate'.", message.contains("NamedParameterJdbcTemplate"));
|
||||
}
|
||||
writer.setJdbcTemplate(namedParameterJdbcOperations);
|
||||
try {
|
||||
@@ -114,7 +114,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
String message = e.getMessage().toLowerCase();
|
||||
assertTrue("Message does not contain 'sql'.", message.indexOf("sql") >= 0);
|
||||
assertTrue("Message does not contain 'sql'.", message.contains("sql"));
|
||||
}
|
||||
writer.setSql("select * from foo where id = :id");
|
||||
|
||||
@@ -132,7 +132,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
@SuppressWarnings({ "rawtypes", "serial", "unchecked" })
|
||||
@Test
|
||||
public void testWriteAndFlushMap() throws Exception {
|
||||
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<Map<String,Object>>();
|
||||
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>();
|
||||
|
||||
mapWriter.setSql(sql);
|
||||
mapWriter.setJdbcTemplate(namedParameterJdbcOperations);
|
||||
@@ -162,7 +162,7 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
catch (EmptyResultDataAccessException e) {
|
||||
// expected
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.indexOf("did not update") >= 0);
|
||||
assertTrue("Wrong message: " + message, message.contains("did not update"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,24 +15,24 @@
|
||||
*/
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.batch.api.chunk.ItemReader;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ItemReaderAdapterTests {
|
||||
|
||||
private ItemReaderAdapter<String> adapter;
|
||||
@@ -45,17 +45,18 @@ public class ItemReaderAdapterTests {
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
adapter = new ItemReaderAdapter<String>(delegate);
|
||||
adapter = new ItemReaderAdapter<>(delegate);
|
||||
adapter.setName("jsrReader");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCreateWithNull() {
|
||||
adapter = new ItemReaderAdapter<String>(null);
|
||||
adapter = new ItemReaderAdapter<>(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpen() throws Exception {
|
||||
when(executionContext.get("ItemReader.reader.checkpoint")).thenReturn("checkpoint");
|
||||
when(executionContext.get("jsrReader.reader.checkpoint")).thenReturn("checkpoint");
|
||||
|
||||
adapter.open(executionContext);
|
||||
|
||||
@@ -64,7 +65,7 @@ public class ItemReaderAdapterTests {
|
||||
|
||||
@Test(expected=ItemStreamException.class)
|
||||
public void testOpenException() throws Exception {
|
||||
when(executionContext.get("ItemReader.reader.checkpoint")).thenReturn("checkpoint");
|
||||
when(executionContext.get("jsrReader.reader.checkpoint")).thenReturn("checkpoint");
|
||||
|
||||
doThrow(new Exception("expected")).when(delegate).open("checkpoint");
|
||||
|
||||
@@ -77,7 +78,7 @@ public class ItemReaderAdapterTests {
|
||||
|
||||
adapter.update(executionContext);
|
||||
|
||||
verify(executionContext).put("ItemReader.reader.checkpoint", "checkpoint");
|
||||
verify(executionContext).put("jsrReader.reader.checkpoint", "checkpoint");
|
||||
}
|
||||
|
||||
@Test(expected=ItemStreamException.class)
|
||||
|
||||
@@ -15,24 +15,24 @@
|
||||
*/
|
||||
package org.springframework.batch.jsr.item;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.batch.api.chunk.ItemWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ItemWriterAdapterTests {
|
||||
|
||||
private ItemWriterAdapter<String> adapter;
|
||||
@@ -45,17 +45,18 @@ public class ItemWriterAdapterTests {
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
adapter = new ItemWriterAdapter<String>(delegate);
|
||||
adapter = new ItemWriterAdapter<>(delegate);
|
||||
adapter.setName("jsrWriter");
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCreateWithNull() {
|
||||
adapter = new ItemWriterAdapter<String>(null);
|
||||
adapter = new ItemWriterAdapter<>(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpen() throws Exception {
|
||||
when(executionContext.get("ItemWriter.writer.checkpoint")).thenReturn("checkpoint");
|
||||
when(executionContext.get("jsrWriter.writer.checkpoint")).thenReturn("checkpoint");
|
||||
|
||||
adapter.open(executionContext);
|
||||
|
||||
@@ -64,7 +65,7 @@ public class ItemWriterAdapterTests {
|
||||
|
||||
@Test(expected=ItemStreamException.class)
|
||||
public void testOpenException() throws Exception {
|
||||
when(executionContext.get("ItemWriter.writer.checkpoint")).thenReturn("checkpoint");
|
||||
when(executionContext.get("jsrWriter.writer.checkpoint")).thenReturn("checkpoint");
|
||||
|
||||
doThrow(new Exception("expected")).when(delegate).open("checkpoint");
|
||||
|
||||
@@ -77,7 +78,7 @@ public class ItemWriterAdapterTests {
|
||||
|
||||
adapter.update(executionContext);
|
||||
|
||||
verify(executionContext).put("ItemWriter.writer.checkpoint", "checkpoint");
|
||||
verify(executionContext).put("jsrWriter.writer.checkpoint", "checkpoint");
|
||||
}
|
||||
|
||||
@Test(expected=ItemStreamException.class)
|
||||
|
||||
@@ -15,16 +15,6 @@
|
||||
*/
|
||||
package org.springframework.batch.support.transaction;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.anyObject;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.any;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
@@ -32,13 +22,20 @@ import java.nio.channels.FileChannel;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Michael Minella
|
||||
@@ -55,16 +52,13 @@ public class TransactionAwareBufferedWriterTests {
|
||||
public void init() {
|
||||
fileChannel = mock(FileChannel.class);
|
||||
|
||||
writer = new TransactionAwareBufferedWriter(fileChannel, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
ByteBuffer bb = ByteBuffer.wrap("c".getBytes());
|
||||
fileChannel.write(bb);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
writer = new TransactionAwareBufferedWriter(fileChannel, () -> {
|
||||
try {
|
||||
ByteBuffer bb = ByteBuffer.wrap("c".getBytes());
|
||||
fileChannel.write(bb);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -77,7 +71,6 @@ public class TransactionAwareBufferedWriterTests {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.support.transaction.TransactionAwareBufferedWriter#write(java.lang.String)}
|
||||
* .
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testWriteOutsideTransaction() throws Exception {
|
||||
@@ -126,12 +119,7 @@ public class TransactionAwareBufferedWriterTests {
|
||||
public void testCloseOutsideTransaction() throws Exception {
|
||||
ArgumentCaptor<ByteBuffer> byteBufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
|
||||
|
||||
when(fileChannel.write(byteBufferCaptor.capture())).thenAnswer(new Answer<Integer>() {
|
||||
@Override
|
||||
public Integer answer(InvocationOnMock invocation) throws Throwable {
|
||||
return ((ByteBuffer) invocation.getArguments()[0]).remaining();
|
||||
}
|
||||
});
|
||||
when(fileChannel.write(byteBufferCaptor.capture())).thenAnswer(invocation -> ((ByteBuffer) invocation.getArguments()[0]).remaining());
|
||||
|
||||
writer.write("foo");
|
||||
writer.close();
|
||||
@@ -142,21 +130,18 @@ public class TransactionAwareBufferedWriterTests {
|
||||
|
||||
@Test
|
||||
public void testFlushInTransaction() throws Exception {
|
||||
when(fileChannel.write((ByteBuffer)anyObject())).thenReturn(3);
|
||||
when(fileChannel.write(any(ByteBuffer.class))).thenReturn(3);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("foo");
|
||||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("foo");
|
||||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(fileChannel, never()).force(false);
|
||||
@@ -165,21 +150,18 @@ public class TransactionAwareBufferedWriterTests {
|
||||
@Test
|
||||
public void testFlushInTransactionForceSync() throws Exception {
|
||||
writer.setForceSync(true);
|
||||
when(fileChannel.write((ByteBuffer)anyObject())).thenReturn(3);
|
||||
when(fileChannel.write(any(ByteBuffer.class))).thenReturn(3);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("foo");
|
||||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("foo");
|
||||
writer.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(fileChannel, times(1)).force(false);
|
||||
@@ -190,18 +172,15 @@ public class TransactionAwareBufferedWriterTests {
|
||||
ArgumentCaptor<ByteBuffer> bb = ArgumentCaptor.forClass(ByteBuffer.class);
|
||||
when(fileChannel.write(bb.capture())).thenReturn(3);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
});
|
||||
|
||||
assertEquals(0, writer.getBufferSize());
|
||||
@@ -212,18 +191,15 @@ public class TransactionAwareBufferedWriterTests {
|
||||
ArgumentCaptor<ByteBuffer> bb = ArgumentCaptor.forClass(ByteBuffer.class);
|
||||
when(fileChannel.write(bb.capture())).thenReturn(3);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(3, writer.getBufferSize());
|
||||
return null;
|
||||
});
|
||||
|
||||
assertEquals(0, writer.getBufferSize());
|
||||
@@ -235,18 +211,15 @@ public class TransactionAwareBufferedWriterTests {
|
||||
ArgumentCaptor<ByteBuffer> bb = ArgumentCaptor.forClass(ByteBuffer.class);
|
||||
when(fileChannel.write(bb.capture())).thenReturn(5);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("fóó");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(5, writer.getBufferSize());
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("fóó");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(5, writer.getBufferSize());
|
||||
return null;
|
||||
});
|
||||
|
||||
assertEquals(0, writer.getBufferSize());
|
||||
@@ -260,18 +233,15 @@ public class TransactionAwareBufferedWriterTests {
|
||||
ArgumentCaptor<ByteBuffer> bb = ArgumentCaptor.forClass(ByteBuffer.class);
|
||||
when(fileChannel.write(bb.capture())).thenReturn(6);
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("fóó");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(6, writer.getBufferSize());
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("fóó");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
assertEquals(6, writer.getBufferSize());
|
||||
return null;
|
||||
});
|
||||
|
||||
assertEquals(0, writer.getBufferSize());
|
||||
@@ -280,17 +250,14 @@ public class TransactionAwareBufferedWriterTests {
|
||||
@Test
|
||||
public void testWriteWithRollback() throws Exception {
|
||||
try {
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
throw new RuntimeException("Planned failure");
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
throw new RuntimeException("Planned failure");
|
||||
});
|
||||
fail("Exception was not thrown");
|
||||
}
|
||||
@@ -310,24 +277,18 @@ public class TransactionAwareBufferedWriterTests {
|
||||
|
||||
@Test
|
||||
public void testExceptionOnFlush() throws Exception {
|
||||
writer = new TransactionAwareBufferedWriter(fileChannel, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
}
|
||||
writer = new TransactionAwareBufferedWriter(fileChannel, () -> {
|
||||
});
|
||||
|
||||
try {
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
return null;
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
writer.write("foo");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
fail("Exception was not thrown");
|
||||
@@ -346,37 +307,30 @@ public class TransactionAwareBufferedWriterTests {
|
||||
final int index = i;
|
||||
@SuppressWarnings("resource")
|
||||
FileChannel fileChannel = mock(FileChannel.class);
|
||||
when(fileChannel.write(any(ByteBuffer.class))).thenAnswer(new Answer<Integer>() {
|
||||
@Override
|
||||
public Integer answer(InvocationOnMock invocation)
|
||||
throws Throwable {
|
||||
ByteBuffer buffer = (ByteBuffer) invocation.getArguments()[0];
|
||||
String val = new String(buffer.array(), "UTF-8");
|
||||
if(results[index] == null) {
|
||||
results[index] = val;
|
||||
} else {
|
||||
results[index] += val;
|
||||
}
|
||||
return buffer.limit();
|
||||
when(fileChannel.write(any(ByteBuffer.class))).thenAnswer(invocation -> {
|
||||
ByteBuffer buffer = (ByteBuffer) invocation.getArguments()[0];
|
||||
String val = new String(buffer.array(), "UTF-8");
|
||||
if(results[index] == null) {
|
||||
results[index] = val;
|
||||
} else {
|
||||
results[index] += val;
|
||||
}
|
||||
return buffer.limit();
|
||||
});
|
||||
writers[i] = new TransactionAwareBufferedWriter(fileChannel, null);
|
||||
}
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
|
||||
@Override
|
||||
public Void doInTransaction(TransactionStatus status) {
|
||||
try {
|
||||
for(int i=0; i< limit; i++) {
|
||||
writers[i].write(String.valueOf(i));
|
||||
}
|
||||
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
|
||||
try {
|
||||
for(int i=0; i< limit; i++) {
|
||||
writers[i].write(String.valueOf(i));
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Unexpected IOException", e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
for(int i=0; i< limit; i++) {
|
||||
assertEquals(String.valueOf(i), results[i]);
|
||||
@@ -386,7 +340,6 @@ public class TransactionAwareBufferedWriterTests {
|
||||
private String getStringFromByteBuffer(ByteBuffer bb) {
|
||||
byte[] bytearr = new byte[bb.remaining()];
|
||||
bb.get(bytearr);
|
||||
String s = new String(bytearr);
|
||||
return s;
|
||||
return new String(bytearr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.messaging.MessageHandlingException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
@@ -1,17 +1,5 @@
|
||||
package org.springframework.batch.integration.partition;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.anyObject;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -30,6 +18,17 @@ import org.springframework.integration.core.MessagingTemplate;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Will Schipp
|
||||
@@ -65,11 +64,11 @@ public class MessageChannelPartitionHandlerTests {
|
||||
MessagingTemplate operations = mock(MessagingTemplate.class);
|
||||
Message message = mock(Message.class);
|
||||
//when
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<StepExecution>();
|
||||
stepExecutions.add(new StepExecution("step1", new JobExecution(5l)));
|
||||
when(stepExecutionSplitter.split((StepExecution) anyObject(), eq(1))).thenReturn(stepExecutions);
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<>();
|
||||
stepExecutions.add(new StepExecution("step1", new JobExecution(5L)));
|
||||
when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions);
|
||||
when(message.getPayload()).thenReturn(Collections.emptyList());
|
||||
when(operations.receive((PollableChannel) anyObject())).thenReturn(message);
|
||||
when(operations.receive((PollableChannel) any())).thenReturn(message);
|
||||
//set
|
||||
messageChannelPartitionHandler.setMessagingOperations(operations);
|
||||
|
||||
@@ -91,9 +90,9 @@ public class MessageChannelPartitionHandlerTests {
|
||||
Message message = mock(Message.class);
|
||||
PollableChannel replyChannel = mock(PollableChannel.class);
|
||||
//when
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<StepExecution>();
|
||||
stepExecutions.add(new StepExecution("step1", new JobExecution(5l)));
|
||||
when(stepExecutionSplitter.split((StepExecution) anyObject(), eq(1))).thenReturn(stepExecutions);
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<>();
|
||||
stepExecutions.add(new StepExecution("step1", new JobExecution(5L)));
|
||||
when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions);
|
||||
when(message.getPayload()).thenReturn(Collections.emptyList());
|
||||
when(operations.receive(replyChannel)).thenReturn(message);
|
||||
//set
|
||||
@@ -118,15 +117,15 @@ public class MessageChannelPartitionHandlerTests {
|
||||
MessagingTemplate operations = mock(MessagingTemplate.class);
|
||||
Message message = mock(Message.class);
|
||||
//when
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<StepExecution>();
|
||||
stepExecutions.add(new StepExecution("step1", new JobExecution(5l)));
|
||||
when(stepExecutionSplitter.split((StepExecution) anyObject(), eq(1))).thenReturn(stepExecutions);
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<>();
|
||||
stepExecutions.add(new StepExecution("step1", new JobExecution(5L)));
|
||||
when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions);
|
||||
when(message.getPayload()).thenReturn(Collections.emptyList());
|
||||
//set
|
||||
messageChannelPartitionHandler.setMessagingOperations(operations);
|
||||
|
||||
//execute
|
||||
Collection<StepExecution> executions = messageChannelPartitionHandler.handle(stepExecutionSplitter, masterStepExecution);
|
||||
messageChannelPartitionHandler.handle(stepExecutionSplitter, masterStepExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,17 +133,17 @@ public class MessageChannelPartitionHandlerTests {
|
||||
//execute with no default set
|
||||
messageChannelPartitionHandler = new MessageChannelPartitionHandler();
|
||||
//mock
|
||||
JobExecution jobExecution = new JobExecution(5l, new JobParameters());
|
||||
StepExecution masterStepExecution = new StepExecution("step1", jobExecution, 1l);
|
||||
JobExecution jobExecution = new JobExecution(5L, new JobParameters());
|
||||
StepExecution masterStepExecution = new StepExecution("step1", jobExecution, 1L);
|
||||
StepExecutionSplitter stepExecutionSplitter = mock(StepExecutionSplitter.class);
|
||||
MessagingTemplate operations = mock(MessagingTemplate.class);
|
||||
JobExplorer jobExplorer = mock(JobExplorer.class);
|
||||
//when
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<StepExecution>();
|
||||
StepExecution partition1 = new StepExecution("step1:partition1", jobExecution, 2l);
|
||||
StepExecution partition2 = new StepExecution("step1:partition2", jobExecution, 3l);
|
||||
StepExecution partition3 = new StepExecution("step1:partition3", jobExecution, 4l);
|
||||
StepExecution partition4 = new StepExecution("step1:partition3", jobExecution, 4l);
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<>();
|
||||
StepExecution partition1 = new StepExecution("step1:partition1", jobExecution, 2L);
|
||||
StepExecution partition2 = new StepExecution("step1:partition2", jobExecution, 3L);
|
||||
StepExecution partition3 = new StepExecution("step1:partition3", jobExecution, 4L);
|
||||
StepExecution partition4 = new StepExecution("step1:partition3", jobExecution, 4L);
|
||||
partition1.setStatus(BatchStatus.COMPLETED);
|
||||
partition2.setStatus(BatchStatus.COMPLETED);
|
||||
partition3.setStatus(BatchStatus.STARTED);
|
||||
@@ -152,14 +151,14 @@ public class MessageChannelPartitionHandlerTests {
|
||||
stepExecutions.add(partition1);
|
||||
stepExecutions.add(partition2);
|
||||
stepExecutions.add(partition3);
|
||||
when(stepExecutionSplitter.split((StepExecution) anyObject(), eq(1))).thenReturn(stepExecutions);
|
||||
when(jobExplorer.getStepExecution(eq(5l), anyLong())).thenReturn(partition2, partition1, partition3, partition3, partition3, partition3, partition4);
|
||||
when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions);
|
||||
when(jobExplorer.getStepExecution(eq(5L), any(Long.class))).thenReturn(partition2, partition1, partition3, partition3, partition3, partition3, partition4);
|
||||
|
||||
//set
|
||||
messageChannelPartitionHandler.setMessagingOperations(operations);
|
||||
messageChannelPartitionHandler.setJobExplorer(jobExplorer);
|
||||
messageChannelPartitionHandler.setStepName("step1");
|
||||
messageChannelPartitionHandler.setPollInterval(500l);
|
||||
messageChannelPartitionHandler.setPollInterval(500L);
|
||||
messageChannelPartitionHandler.afterPropertiesSet();
|
||||
|
||||
//execute
|
||||
@@ -172,7 +171,7 @@ public class MessageChannelPartitionHandlerTests {
|
||||
assertTrue(executions.contains(partition4));
|
||||
|
||||
//verify
|
||||
verify(operations, times(3)).send((Message) anyObject());
|
||||
verify(operations, times(3)).send(any(Message.class));
|
||||
}
|
||||
|
||||
@Test(expected = TimeoutException.class)
|
||||
@@ -180,33 +179,33 @@ public class MessageChannelPartitionHandlerTests {
|
||||
//execute with no default set
|
||||
messageChannelPartitionHandler = new MessageChannelPartitionHandler();
|
||||
//mock
|
||||
JobExecution jobExecution = new JobExecution(5l, new JobParameters());
|
||||
StepExecution masterStepExecution = new StepExecution("step1", jobExecution, 1l);
|
||||
JobExecution jobExecution = new JobExecution(5L, new JobParameters());
|
||||
StepExecution masterStepExecution = new StepExecution("step1", jobExecution, 1L);
|
||||
StepExecutionSplitter stepExecutionSplitter = mock(StepExecutionSplitter.class);
|
||||
MessagingTemplate operations = mock(MessagingTemplate.class);
|
||||
JobExplorer jobExplorer = mock(JobExplorer.class);
|
||||
//when
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<StepExecution>();
|
||||
StepExecution partition1 = new StepExecution("step1:partition1", jobExecution, 2l);
|
||||
StepExecution partition2 = new StepExecution("step1:partition2", jobExecution, 3l);
|
||||
StepExecution partition3 = new StepExecution("step1:partition3", jobExecution, 4l);
|
||||
HashSet<StepExecution> stepExecutions = new HashSet<>();
|
||||
StepExecution partition1 = new StepExecution("step1:partition1", jobExecution, 2L);
|
||||
StepExecution partition2 = new StepExecution("step1:partition2", jobExecution, 3L);
|
||||
StepExecution partition3 = new StepExecution("step1:partition3", jobExecution, 4L);
|
||||
partition1.setStatus(BatchStatus.COMPLETED);
|
||||
partition2.setStatus(BatchStatus.COMPLETED);
|
||||
partition3.setStatus(BatchStatus.STARTED);
|
||||
stepExecutions.add(partition1);
|
||||
stepExecutions.add(partition2);
|
||||
stepExecutions.add(partition3);
|
||||
when(stepExecutionSplitter.split((StepExecution) anyObject(), eq(1))).thenReturn(stepExecutions);
|
||||
when(jobExplorer.getStepExecution(eq(5l), anyLong())).thenReturn(partition2, partition1, partition3);
|
||||
when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions);
|
||||
when(jobExplorer.getStepExecution(eq(5L), any(Long.class))).thenReturn(partition2, partition1, partition3);
|
||||
|
||||
//set
|
||||
messageChannelPartitionHandler.setMessagingOperations(operations);
|
||||
messageChannelPartitionHandler.setJobExplorer(jobExplorer);
|
||||
messageChannelPartitionHandler.setStepName("step1");
|
||||
messageChannelPartitionHandler.setTimeout(1000l);
|
||||
messageChannelPartitionHandler.setTimeout(1000L);
|
||||
messageChannelPartitionHandler.afterPropertiesSet();
|
||||
|
||||
//execute
|
||||
Collection<StepExecution> executions = messageChannelPartitionHandler.handle(stepExecutionSplitter, masterStepExecution);
|
||||
messageChannelPartitionHandler.handle(stepExecutionSplitter, masterStepExecution);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user