committed by
Mahmoud Ben Hassine
parent
6317124250
commit
dab89ccb97
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.core.test.repository;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
|
||||
/**
|
||||
* @author Henning Pöttker
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class H2CompatibilityModeJobRepositoryIntegrationTests {
|
||||
|
||||
private final String compatibilityMode;
|
||||
|
||||
public H2CompatibilityModeJobRepositoryIntegrationTests(String compatibilityMode) {
|
||||
this.compatibilityMode = compatibilityMode;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJobExecution() throws Exception {
|
||||
var context = new AnnotationConfigApplicationContext();
|
||||
context.register(TestConfiguration.class);
|
||||
context.registerBean(DataSource.class, this::buildDataSource);
|
||||
context.refresh();
|
||||
var jobLauncher = context.getBean(JobLauncher.class);
|
||||
var job = context.getBean(Job.class);
|
||||
|
||||
var jobExecution = jobLauncher.run(job, new JobParameters());
|
||||
|
||||
Assert.assertNotNull(jobExecution);
|
||||
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
|
||||
|
||||
var jdbcTemplate = new JdbcTemplate(context.getBean(DataSource.class));
|
||||
jdbcTemplate.execute("SHUTDOWN");
|
||||
}
|
||||
|
||||
private DataSource buildDataSource() {
|
||||
var connectionUrl = String.format(
|
||||
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=%s",
|
||||
UUID.randomUUID(),
|
||||
this.compatibilityMode
|
||||
);
|
||||
var dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
|
||||
var populator = new ResourceDatabasePopulator();
|
||||
var resource = new DefaultResourceLoader()
|
||||
.getResource("/org/springframework/batch/core/schema-h2.sql");
|
||||
populator.addScript(resource);
|
||||
DatabasePopulatorUtils.execute(populator, dataSource);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
static class TestConfiguration {
|
||||
@Bean
|
||||
Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
|
||||
return jobs.get("job")
|
||||
.start(steps.get("step")
|
||||
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED)
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static List<Object[]> data() throws Exception {
|
||||
return Arrays.stream(org.h2.engine.Mode.ModeEnum.values())
|
||||
.map(mode -> new Object[]{mode.toString()})
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
-- Autogenerated: do not edit this file
|
||||
|
||||
CREATE TABLE BATCH_JOB_INSTANCE (
|
||||
JOB_INSTANCE_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
|
||||
JOB_INSTANCE_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
|
||||
VERSION BIGINT ,
|
||||
JOB_NAME VARCHAR(100) NOT NULL,
|
||||
JOB_KEY VARCHAR(32) NOT NULL,
|
||||
@@ -9,7 +9,7 @@ CREATE TABLE BATCH_JOB_INSTANCE (
|
||||
) ;
|
||||
|
||||
CREATE TABLE BATCH_JOB_EXECUTION (
|
||||
JOB_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
|
||||
JOB_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
|
||||
VERSION BIGINT ,
|
||||
JOB_INSTANCE_ID BIGINT NOT NULL,
|
||||
CREATE_TIME TIMESTAMP NOT NULL,
|
||||
@@ -38,7 +38,7 @@ CREATE TABLE BATCH_JOB_EXECUTION_PARAMS (
|
||||
) ;
|
||||
|
||||
CREATE TABLE BATCH_STEP_EXECUTION (
|
||||
STEP_EXECUTION_ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
|
||||
STEP_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
|
||||
VERSION BIGINT NOT NULL,
|
||||
STEP_NAME VARCHAR(100) NOT NULL,
|
||||
JOB_EXECUTION_ID BIGINT NOT NULL,
|
||||
|
||||
Reference in New Issue
Block a user