Skip to content

Commit dab89cc

Browse files
hpoettkerfmbenhassine
authored andcommitted
Adjust h2 schema to work with v2.0.x
Issue #4043
1 parent 6317124 commit dab89cc

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.test.repository;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.UUID;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.Parameterized;
28+
import org.junit.runners.Parameterized.Parameters;
29+
30+
import org.springframework.batch.core.ExitStatus;
31+
import org.springframework.batch.core.Job;
32+
import org.springframework.batch.core.JobParameters;
33+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
34+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
35+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
36+
import org.springframework.batch.core.launch.JobLauncher;
37+
import org.springframework.batch.repeat.RepeatStatus;
38+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
39+
import org.springframework.context.annotation.Bean;
40+
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.core.io.DefaultResourceLoader;
42+
import org.springframework.jdbc.core.JdbcTemplate;
43+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
44+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
45+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
46+
47+
/**
48+
* @author Henning Pöttker
49+
*/
50+
@RunWith(Parameterized.class)
51+
public class H2CompatibilityModeJobRepositoryIntegrationTests {
52+
53+
private final String compatibilityMode;
54+
55+
public H2CompatibilityModeJobRepositoryIntegrationTests(String compatibilityMode) {
56+
this.compatibilityMode = compatibilityMode;
57+
}
58+
59+
@Test
60+
public void testJobExecution() throws Exception {
61+
var context = new AnnotationConfigApplicationContext();
62+
context.register(TestConfiguration.class);
63+
context.registerBean(DataSource.class, this::buildDataSource);
64+
context.refresh();
65+
var jobLauncher = context.getBean(JobLauncher.class);
66+
var job = context.getBean(Job.class);
67+
68+
var jobExecution = jobLauncher.run(job, new JobParameters());
69+
70+
Assert.assertNotNull(jobExecution);
71+
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
72+
73+
var jdbcTemplate = new JdbcTemplate(context.getBean(DataSource.class));
74+
jdbcTemplate.execute("SHUTDOWN");
75+
}
76+
77+
private DataSource buildDataSource() {
78+
var connectionUrl = String.format(
79+
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=%s",
80+
UUID.randomUUID(),
81+
this.compatibilityMode
82+
);
83+
var dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
84+
var populator = new ResourceDatabasePopulator();
85+
var resource = new DefaultResourceLoader()
86+
.getResource("/org/springframework/batch/core/schema-h2.sql");
87+
populator.addScript(resource);
88+
DatabasePopulatorUtils.execute(populator, dataSource);
89+
return dataSource;
90+
}
91+
92+
@Configuration
93+
@EnableBatchProcessing
94+
static class TestConfiguration {
95+
@Bean
96+
Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
97+
return jobs.get("job")
98+
.start(steps.get("step")
99+
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
100+
.build())
101+
.build();
102+
}
103+
}
104+
105+
@Parameters
106+
public static List<Object[]> data() throws Exception {
107+
return Arrays.stream(org.h2.engine.Mode.ModeEnum.values())
108+
.map(mode -> new Object[]{mode.toString()})
109+
.toList();
110+
}
111+
}

spring-batch-core/src/main/resources/org/springframework/batch/core/schema-h2.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
-- Autogenerated: do not edit this file
22

33
CREATE TABLE BATCH_JOB_INSTANCE (
4-
JOB_INSTANCE_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
4+
JOB_INSTANCE_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
55
VERSION BIGINT ,
66
JOB_NAME VARCHAR(100) NOT NULL,
77
JOB_KEY VARCHAR(32) NOT NULL,
88
constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY)
99
) ;
1010

1111
CREATE TABLE BATCH_JOB_EXECUTION (
12-
JOB_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
12+
JOB_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
1313
VERSION BIGINT ,
1414
JOB_INSTANCE_ID BIGINT NOT NULL,
1515
CREATE_TIME TIMESTAMP NOT NULL,
@@ -38,7 +38,7 @@ CREATE TABLE BATCH_JOB_EXECUTION_PARAMS (
3838
) ;
3939

4040
CREATE TABLE BATCH_STEP_EXECUTION (
41-
STEP_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
41+
STEP_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
4242
VERSION BIGINT NOT NULL,
4343
STEP_NAME VARCHAR(100) NOT NULL,
4444
JOB_EXECUTION_ID BIGINT NOT NULL,

0 commit comments

Comments
 (0)