Skip to content

Commit 7e37765

Browse files
committed
Apply Spring 6, Spring Batch 5 and more
* Change MyBatisBatchItemWriter * Apply jakarta.transaction-api 2.0.1 * Apply jakarta.servlet-api 6.0.0 * Fix error on tests Fixes gh-715 gh-716
1 parent 5847bb0 commit 7e37765

File tree

9 files changed

+87
-47
lines changed

9 files changed

+87
-47
lines changed

pom.xml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@
106106
<osgi.dynamicImport>*</osgi.dynamicImport>
107107

108108
<mybatis.version>3.5.10</mybatis.version>
109-
<spring.version>5.3.22</spring.version>
110-
<spring-batch.version>4.3.6</spring-batch.version>
109+
<spring.version>6.0.0-SNAPSHOT</spring.version>
110+
<spring-batch.version>5.0.0-SNAPSHOT</spring-batch.version>
111111
<module.name>org.mybatis.spring</module.name>
112112

113113
<junit.version>5.9.0</junit.version>
@@ -295,13 +295,13 @@
295295
<dependency>
296296
<groupId>jakarta.transaction</groupId>
297297
<artifactId>jakarta.transaction-api</artifactId>
298-
<version>1.3.3</version>
298+
<version>2.0.1</version>
299299
<scope>test</scope>
300300
</dependency>
301301
<dependency>
302302
<groupId>jakarta.servlet</groupId>
303303
<artifactId>jakarta.servlet-api</artifactId>
304-
<version>4.0.4</version>
304+
<version>6.0.0</version>
305305
<scope>test</scope>
306306
</dependency>
307307

@@ -371,6 +371,40 @@
371371
<name>Sonatype OSS Snapshots Repository</name>
372372
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
373373
</repository>
374+
<repository>
375+
<id>spring-snapshot</id>
376+
<name>Spring Snapshots</name>
377+
<url>https://repo.spring.io/snapshot</url>
378+
<releases>
379+
<enabled>false</enabled>
380+
</releases>
381+
</repository>
382+
<repository>
383+
<id>spring-milestone</id>
384+
<name>Spring Milestone</name>
385+
<url>https://repo.spring.io/milestone</url>
386+
<snapshots>
387+
<enabled>false</enabled>
388+
</snapshots>
389+
</repository>
374390
</repositories>
375391

392+
<pluginRepositories>
393+
<pluginRepository>
394+
<id>spring-snapshot</id>
395+
<name>Spring Snapshots</name>
396+
<url>https://repo.spring.io/snapshot</url>
397+
<releases>
398+
<enabled>false</enabled>
399+
</releases>
400+
</pluginRepository>
401+
<pluginRepository>
402+
<id>spring-milestone</id>
403+
<name>Spring Milestone</name>
404+
<url>https://repo.spring.io/milestone</url>
405+
<snapshots>
406+
<enabled>false</enabled>
407+
</snapshots>
408+
</pluginRepository>
409+
</pluginRepositories>
376410
</project>

src/main/java/org/mybatis/spring/batch/MyBatisBatchItemWriter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.mybatis.logging.Logger;
2828
import org.mybatis.logging.LoggerFactory;
2929
import org.mybatis.spring.SqlSessionTemplate;
30+
import org.springframework.batch.item.Chunk;
3031
import org.springframework.batch.item.ItemWriter;
3132
import org.springframework.beans.factory.InitializingBean;
3233
import org.springframework.core.convert.converter.Converter;
@@ -136,7 +137,7 @@ public void afterPropertiesSet() {
136137
* {@inheritDoc}
137138
*/
138139
@Override
139-
public void write(final List<? extends T> items) {
140+
public void write(final Chunk<? extends T> items) {
140141

141142
if (!items.isEmpty()) {
142143
LOGGER.debug(() -> "Executing batch with " + items.size() + " items.");
@@ -158,8 +159,8 @@ public void write(final List<? extends T> items) {
158159
for (int i = 0; i < updateCounts.length; i++) {
159160
int value = updateCounts[i];
160161
if (value == 0) {
161-
throw new EmptyResultDataAccessException(
162-
"Item " + i + " of " + updateCounts.length + " did not update any rows: [" + items.get(i) + "]", 1);
162+
throw new EmptyResultDataAccessException("Item " + i + " of " + updateCounts.length
163+
+ " did not update any rows: [" + items.getItems().get(i) + "]", 1);
163164
}
164165
}
165166
}

src/test/java/org/mybatis/spring/MyBatisSpringTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020
import static org.junit.jupiter.api.Assertions.fail;
2121

22-
import com.mockrunner.mock.ejb.MockUserTransaction;
2322
import com.mockrunner.mock.jdbc.MockConnection;
2423
import com.mockrunner.mock.jdbc.MockDataSource;
2524
import com.mockrunner.mock.jdbc.MockPreparedStatement;
@@ -32,12 +31,15 @@
3231
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
3332
import org.junit.jupiter.api.AfterEach;
3433
import org.junit.jupiter.api.Test;
34+
import org.mockito.Mockito;
3535
import org.springframework.dao.DataAccessException;
3636
import org.springframework.dao.TransientDataAccessResourceException;
3737
import org.springframework.transaction.TransactionStatus;
3838
import org.springframework.transaction.jta.JtaTransactionManager;
3939
import org.springframework.transaction.support.DefaultTransactionDefinition;
4040

41+
import jakarta.transaction.UserTransaction;
42+
4143
class MyBatisSpringTest extends AbstractMyBatisSpringTest {
4244

4345
private SqlSession session;
@@ -275,7 +277,7 @@ void testChangeExecutorTypeInTxRequiresNew() throws Exception {
275277

276278
@Test
277279
void testWithJtaTxManager() {
278-
JtaTransactionManager jtaManager = new JtaTransactionManager(new MockUserTransaction());
280+
JtaTransactionManager jtaManager = new JtaTransactionManager(Mockito.mock(UserTransaction.class));
279281

280282
DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
281283
txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");
@@ -304,7 +306,7 @@ void testWithJtaTxManagerAndNonSpringTxManager() throws java.sql.SQLException {
304306
Environment nonSpring = new Environment("non-spring", new ManagedTransactionFactory(), mockDataSource);
305307
sqlSessionFactory.getConfiguration().setEnvironment(nonSpring);
306308

307-
JtaTransactionManager jtaManager = new JtaTransactionManager(new MockUserTransaction());
309+
JtaTransactionManager jtaManager = new JtaTransactionManager(Mockito.mock(UserTransaction.class));
308310

309311
DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
310312
txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");

src/test/java/org/mybatis/spring/batch/MyBatisBatchItemWriterTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
import java.time.Instant;
2323
import java.time.LocalDateTime;
2424
import java.time.ZoneId;
25-
import java.util.Arrays;
2625
import java.util.Collections;
2726
import java.util.HashMap;
2827
import java.util.List;
2928
import java.util.Map;
3029

3130
import org.apache.ibatis.executor.BatchResult;
3231
import org.apache.ibatis.session.ExecutorType;
33-
import org.assertj.core.util.Lists;
3432
import org.junit.jupiter.api.BeforeEach;
3533
import org.junit.jupiter.api.Test;
3634
import org.mockito.InjectMocks;
@@ -39,6 +37,7 @@
3937
import org.mockito.MockitoAnnotations;
4038
import org.mybatis.spring.SqlSessionTemplate;
4139
import org.mybatis.spring.batch.domain.Employee;
40+
import org.springframework.batch.item.Chunk;
4241
import org.springframework.dao.EmptyResultDataAccessException;
4342
import org.springframework.dao.InvalidDataAccessResourceUsageException;
4443

@@ -60,8 +59,8 @@ void setUp() {
6059

6160
@Test
6261
void testZeroBatchResultShouldThrowException() {
63-
List<Employee> employees = Arrays.asList(new Employee(), new Employee());
64-
List<BatchResult> batchResults = Lists.emptyList();
62+
Chunk<Employee> employees = Chunk.of(new Employee(), new Employee());
63+
List<BatchResult> batchResults = Collections.emptyList();
6564

6665
given(mockSqlSessionTemplate.flushStatements()).willReturn(batchResults);
6766

@@ -70,7 +69,7 @@ void testZeroBatchResultShouldThrowException() {
7069

7170
@Test
7271
void testZeroUpdateCountShouldThrowException() {
73-
List<Employee> employees = Arrays.asList(new Employee(), new Employee());
72+
Chunk<Employee> employees = Chunk.of(new Employee(), new Employee());
7473

7574
BatchResult batchResult = new BatchResult(null, null);
7675
batchResult.setUpdateCounts(new int[] { 1, 0 });
@@ -87,7 +86,7 @@ void testItemToParameterConverterIsDefault() {
8786
this.writer.setStatementId("updateEmployee");
8887

8988
Employee employee = new Employee();
90-
List<Employee> employees = Collections.singletonList(employee);
89+
Chunk<Employee> employees = Chunk.of(employee);
9190
writer.write(employees);
9291

9392
Mockito.verify(this.mockSqlSessionTemplate).update("updateEmployee", employee);
@@ -105,7 +104,7 @@ void testSetItemToParameterConverter() {
105104
});
106105

107106
Employee employee = new Employee();
108-
List<Employee> employees = Collections.singletonList(employee);
107+
Chunk<Employee> employees = Chunk.of(employee);
109108
writer.write(employees);
110109

111110
Map<String, Object> parameter = new HashMap<>();

src/test/java/org/mybatis/spring/batch/SpringBatchTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import java.util.ArrayList;
21-
import java.util.List;
22-
2320
import org.apache.ibatis.session.SqlSession;
2421
import org.junit.jupiter.api.Test;
2522
import org.mybatis.spring.batch.domain.Employee;
23+
import org.springframework.batch.item.Chunk;
2624
import org.springframework.beans.factory.annotation.Autowired;
2725
import org.springframework.beans.factory.annotation.Qualifier;
2826
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -56,7 +54,7 @@ class SpringBatchTest {
5654
@Test
5755
@Transactional
5856
void shouldDuplicateSalaryOfAllEmployees() throws Exception {
59-
List<Employee> employees = new ArrayList<>();
57+
Chunk<Employee> employees = new Chunk<>();
6058
Employee employee = pagingNoNestedItemReader.read();
6159
while (employee != null) {
6260
employee.setSalary(employee.getSalary() * 2);
@@ -73,7 +71,7 @@ void shouldDuplicateSalaryOfAllEmployees() throws Exception {
7371
@Transactional
7472
void checkPagingReadingWithNestedInResultMap() throws Exception {
7573
// This test is here to show that PagingReader can return wrong result in case of nested result maps
76-
List<Employee> employees = new ArrayList<>();
74+
Chunk<Employee> employees = new Chunk<>();
7775
Employee employee = pagingNestedItemReader.read();
7876
while (employee != null) {
7977
employee.setSalary(employee.getSalary() * 2);
@@ -91,7 +89,7 @@ void checkPagingReadingWithNestedInResultMap() throws Exception {
9189
void checkCursorReadingWithoutNestedInResultMap() throws Exception {
9290
cursorNoNestedItemReader.doOpen();
9391
try {
94-
List<Employee> employees = new ArrayList<>();
92+
Chunk<Employee> employees = new Chunk<>();
9593
Employee employee = cursorNoNestedItemReader.read();
9694
while (employee != null) {
9795
employee.setSalary(employee.getSalary() * 2);
@@ -112,7 +110,7 @@ void checkCursorReadingWithoutNestedInResultMap() throws Exception {
112110
void checkCursorReadingWithNestedInResultMap() throws Exception {
113111
cursorNestedItemReader.doOpen();
114112
try {
115-
List<Employee> employees = new ArrayList<>();
113+
Chunk<Employee> employees = new Chunk<>();
116114
Employee employee = cursorNestedItemReader.read();
117115
while (employee != null) {
118116
employee.setSalary(employee.getSalary() * 2);

src/test/java/org/mybatis/spring/batch/builder/MyBatisBatchItemWriterBuilderTest.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.mockito.MockitoAnnotations;
3838
import org.mybatis.spring.SqlSessionTemplate;
3939
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
40+
import org.springframework.batch.item.Chunk;
4041

4142
/**
4243
* Tests for {@link MyBatisBatchItemWriterBuilder}.
@@ -84,13 +85,13 @@ void testConfigurationUsingSqlSessionFactory() {
8485
// @formatter:on
8586
itemWriter.afterPropertiesSet();
8687

87-
List<Foo> foos = getFoos();
88+
Chunk<Foo> foos = getFoos();
8889

8990
itemWriter.write(foos);
9091

91-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(0));
92-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(1));
93-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(2));
92+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(0));
93+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(1));
94+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(2));
9495

9596
}
9697

@@ -105,13 +106,13 @@ void testConfigurationUsingSqlSessionTemplate() {
105106
// @formatter:on
106107
itemWriter.afterPropertiesSet();
107108

108-
List<Foo> foos = getFoos();
109+
Chunk<Foo> foos = getFoos();
109110

110111
itemWriter.write(foos);
111112

112-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(0));
113-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(1));
114-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(2));
113+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(0));
114+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(1));
115+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(2));
115116

116117
}
117118

@@ -129,13 +130,13 @@ void testConfigurationAssertUpdatesIsFalse() {
129130
// @formatter:on
130131
itemWriter.afterPropertiesSet();
131132

132-
List<Foo> foos = getFoos();
133+
Chunk<Foo> foos = getFoos();
133134

134135
itemWriter.write(foos);
135136

136-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(0));
137-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(1));
138-
Mockito.verify(this.sqlSession).update("updateFoo", foos.get(2));
137+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(0));
138+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(1));
139+
Mockito.verify(this.sqlSession).update("updateFoo", foos.getItems().get(2));
139140

140141
}
141142

@@ -156,22 +157,22 @@ void testConfigurationSetItemToParameterConverter() {
156157
// @formatter:on
157158
itemWriter.afterPropertiesSet();
158159

159-
List<Foo> foos = getFoos();
160+
Chunk<Foo> foos = getFoos();
160161

161162
itemWriter.write(foos);
162163

163164
Map<String, Object> parameter = new HashMap<>();
164165
parameter.put("now", LocalDateTime.now(Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault())));
165-
parameter.put("item", foos.get(0));
166+
parameter.put("item", foos.getItems().get(0));
166167
Mockito.verify(this.sqlSession).update("updateFoo", parameter);
167-
parameter.put("item", foos.get(1));
168+
parameter.put("item", foos.getItems().get(1));
168169
Mockito.verify(this.sqlSession).update("updateFoo", parameter);
169-
parameter.put("item", foos.get(2));
170+
parameter.put("item", foos.getItems().get(2));
170171
Mockito.verify(this.sqlSession).update("updateFoo", parameter);
171172
}
172173

173-
private List<Foo> getFoos() {
174-
return Arrays.asList(new Foo("foo1"), new Foo("foo2"), new Foo("foo3"));
174+
private Chunk<Foo> getFoos() {
175+
return Chunk.of(new Foo("foo1"), new Foo("foo2"), new Foo("foo3"));
175176
}
176177

177178
private static class Foo {

src/test/java/org/mybatis/spring/sample/AbstractSampleJobTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.junit.jupiter.api.Assertions;
2424
import org.junit.jupiter.api.Test;
25+
import org.springframework.batch.core.Job;
2526
import org.springframework.batch.core.JobExecution;
2627
import org.springframework.batch.test.JobLauncherTestUtils;
2728
import org.springframework.beans.factory.annotation.Autowired;
@@ -97,8 +98,10 @@ void testJob() throws Exception {
9798
@Configuration
9899
static class LocalContext {
99100
@Bean
100-
JobLauncherTestUtils jobLauncherTestUtils() {
101-
return new JobLauncherTestUtils();
101+
JobLauncherTestUtils jobLauncherTestUtils(Job job) {
102+
JobLauncherTestUtils utils = new JobLauncherTestUtils();
103+
utils.setJob(job);
104+
return utils;
102105
}
103106

104107
@Bean

src/test/java/org/mybatis/spring/sample/config/SampleJobConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ public Job importUserJob() throws Exception {
133133
public Step step1() throws Exception {
134134
// @formatter:off
135135
return stepBuilderFactory.get("step1")
136-
.transactionManager(transactionalManager())
137136
.<User, Person>chunk(10)
138137
.reader(reader())
139138
.processor(processor())
140139
.writer(writer())
140+
.transactionManager(transactionalManager())
141141
.build();
142142
// @formatter:on
143143
}

src/test/java/org/mybatis/spring/submitted/xa/UserServiceTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20-
import javax.transaction.UserTransaction;
21-
20+
import org.junit.jupiter.api.Disabled;
2221
import org.junit.jupiter.api.Test;
2322
import org.junit.jupiter.api.extension.ExtendWith;
2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.test.context.junit.jupiter.SpringExtension;
2625
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2726

27+
import jakarta.transaction.UserTransaction;
28+
29+
@Disabled("Yet not found OSS implementation that supported Jakarta EE 9+ APIs")
2830
@ExtendWith(SpringExtension.class)
2931
@SpringJUnitConfig(locations = "classpath:org/mybatis/spring/submitted/xa/applicationContext.xml")
3032
class UserServiceTest {

0 commit comments

Comments
 (0)