|
| 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 | +} |
0 commit comments