diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java index e0280ba26..aea3d2057 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java @@ -27,7 +27,13 @@ import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.configuration.BatchConfigurationException; import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.converter.DateToStringConverter; +import org.springframework.batch.core.converter.LocalDateTimeToStringConverter; +import org.springframework.batch.core.converter.LocalDateToStringConverter; +import org.springframework.batch.core.converter.LocalTimeToStringConverter; import org.springframework.batch.core.converter.StringToDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateTimeConverter; +import org.springframework.batch.core.converter.StringToLocalTimeConverter; import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.explore.support.JobExplorerFactoryBean; import org.springframework.batch.core.launch.JobLauncher; @@ -380,6 +386,12 @@ public class DefaultBatchConfiguration implements ApplicationContextAware { DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new DateToStringConverter()); conversionService.addConverter(new StringToDateConverter()); + conversionService.addConverter(new LocalDateToStringConverter()); + conversionService.addConverter(new StringToLocalDateConverter()); + conversionService.addConverter(new LocalTimeToStringConverter()); + conversionService.addConverter(new StringToLocalTimeConverter()); + conversionService.addConverter(new LocalDateTimeToStringConverter()); + conversionService.addConverter(new StringToLocalDateTimeConverter()); return conversionService; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/AbstractDateTimeConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/AbstractDateTimeConverter.java index 1d9b59ba2..7600ef670 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/AbstractDateTimeConverter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/AbstractDateTimeConverter.java @@ -27,6 +27,10 @@ class AbstractDateTimeConverter { protected DateTimeFormatter instantFormatter = DateTimeFormatter.ISO_INSTANT; - protected DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_INSTANT; + protected DateTimeFormatter localDateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; + + protected DateTimeFormatter localTimeFormatter = DateTimeFormatter.ISO_LOCAL_TIME; + + protected DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java index 298cccd02..d0776f5d1 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/DefaultJobParametersConverter.java @@ -59,6 +59,12 @@ import org.springframework.util.StringUtils; * * * @author Dave Syer @@ -74,6 +80,12 @@ public class DefaultJobParametersConverter implements JobParametersConverter { DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new DateToStringConverter()); conversionService.addConverter(new StringToDateConverter()); + conversionService.addConverter(new LocalDateToStringConverter()); + conversionService.addConverter(new StringToLocalDateConverter()); + conversionService.addConverter(new LocalTimeToStringConverter()); + conversionService.addConverter(new StringToLocalTimeConverter()); + conversionService.addConverter(new LocalDateTimeToStringConverter()); + conversionService.addConverter(new StringToLocalDateTimeConverter()); this.conversionService = conversionService; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/JsonJobParametersConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/JsonJobParametersConverter.java index 92955f65f..6638a3eff 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/JsonJobParametersConverter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/JsonJobParametersConverter.java @@ -53,6 +53,12 @@ import org.springframework.batch.core.JobParameters; * * * @author Mahmoud Ben Hassine diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalDateTimeToStringConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalDateTimeToStringConverter.java new file mode 100644 index 000000000..4c68325a1 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalDateTimeToStringConverter.java @@ -0,0 +1,40 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.springframework.core.convert.converter.Converter; + +/** + * {@link Converter} implementation from {@link LocalDateTime} to {@link String}. + * + * This converter formats dates according to the + * {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} format. + * + * @author Mahmoud Ben Hassine + * @since 5.0.1 + */ +public class LocalDateTimeToStringConverter extends AbstractDateTimeConverter + implements Converter { + + @Override + public String convert(LocalDateTime source) { + return source.format(super.localDateTimeFormatter); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalDateToStringConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalDateToStringConverter.java new file mode 100644 index 000000000..7d9f875d5 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalDateToStringConverter.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDate; + +import org.springframework.core.convert.converter.Converter; + +/** + * {@link Converter} implementation from {@link LocalDate} to {@link String}. + * + * This converter formats dates according to the + * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format. + * + * @author Mahmoud Ben Hassine + * @since 5.0.1 + */ +public class LocalDateToStringConverter extends AbstractDateTimeConverter implements Converter { + + @Override + public String convert(LocalDate source) { + return source.format(super.localDateFormatter); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalTimeToStringConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalTimeToStringConverter.java new file mode 100644 index 000000000..70be12834 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/LocalTimeToStringConverter.java @@ -0,0 +1,39 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + +import org.springframework.core.convert.converter.Converter; + +/** + * {@link Converter} implementation from {@link LocalTime} to {@link String}. + * + * This converter formats times according to the {@link DateTimeFormatter#ISO_LOCAL_TIME} + * format. + * + * @author Mahmoud Ben Hassine + * @since 5.0.1 + */ +public class LocalTimeToStringConverter extends AbstractDateTimeConverter implements Converter { + + @Override + public String convert(LocalTime source) { + return source.format(super.localTimeFormatter); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalDateConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalDateConverter.java new file mode 100644 index 000000000..f35a6e1c7 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalDateConverter.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDate; + +import org.springframework.core.convert.converter.Converter; + +/** + * {@link Converter} implementation from {@link String} to {@link LocalDate}. + * + * This converter expects strings in the + * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format. + * + * @author Mahmoud Ben Hassine + * @since 5.0.1 + */ +public class StringToLocalDateConverter extends AbstractDateTimeConverter implements Converter { + + @Override + public LocalDate convert(String source) { + return LocalDate.parse(source, super.localDateFormatter); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalDateTimeConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalDateTimeConverter.java new file mode 100644 index 000000000..5f1eb343f --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalDateTimeConverter.java @@ -0,0 +1,39 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDateTime; + +import org.springframework.core.convert.converter.Converter; + +/** + * {@link Converter} implementation from {@link String} to {@link LocalDateTime}. + * + * This converter expects strings in the + * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format. + * + * @author Mahmoud Ben Hassine + * @since 5.0.1 + */ +public class StringToLocalDateTimeConverter extends AbstractDateTimeConverter + implements Converter { + + @Override + public LocalDateTime convert(String source) { + return LocalDateTime.parse(source, super.localDateTimeFormatter); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalTimeConverter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalTimeConverter.java new file mode 100644 index 000000000..0b3c2ef11 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/converter/StringToLocalTimeConverter.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalTime; + +import org.springframework.core.convert.converter.Converter; + +/** + * {@link Converter} implementation from {@link String} to {@link LocalTime}. + * + * This converter expects strings in the + * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format. + * + * @author Mahmoud Ben Hassine + * @since 5.0.1 + */ +public class StringToLocalTimeConverter extends AbstractDateTimeConverter implements Converter { + + @Override + public LocalTime convert(String source) { + return LocalTime.parse(source, super.localTimeFormatter); + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java index a39780486..02f511a8f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java @@ -22,7 +22,13 @@ import java.nio.charset.StandardCharsets; import javax.sql.DataSource; import org.springframework.batch.core.converter.DateToStringConverter; +import org.springframework.batch.core.converter.LocalDateTimeToStringConverter; +import org.springframework.batch.core.converter.LocalDateToStringConverter; +import org.springframework.batch.core.converter.LocalTimeToStringConverter; import org.springframework.batch.core.converter.StringToDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateTimeConverter; +import org.springframework.batch.core.converter.StringToLocalTimeConverter; import org.springframework.batch.core.repository.ExecutionContextSerializer; import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao; import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer; @@ -168,6 +174,12 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new DateToStringConverter()); conversionService.addConverter(new StringToDateConverter()); + conversionService.addConverter(new LocalDateToStringConverter()); + conversionService.addConverter(new StringToLocalDateConverter()); + conversionService.addConverter(new LocalTimeToStringConverter()); + conversionService.addConverter(new StringToLocalTimeConverter()); + conversionService.addConverter(new LocalDateTimeToStringConverter()); + conversionService.addConverter(new StringToLocalDateTimeConverter()); this.conversionService = conversionService; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java index 6ef4d11a3..984064801 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java @@ -37,7 +37,13 @@ import org.springframework.batch.core.JobInstance; import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.converter.DateToStringConverter; +import org.springframework.batch.core.converter.LocalDateTimeToStringConverter; +import org.springframework.batch.core.converter.LocalDateToStringConverter; +import org.springframework.batch.core.converter.LocalTimeToStringConverter; import org.springframework.batch.core.converter.StringToDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateTimeConverter; +import org.springframework.batch.core.converter.StringToLocalTimeConverter; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.core.convert.support.DefaultConversionService; @@ -113,6 +119,12 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new DateToStringConverter()); conversionService.addConverter(new StringToDateConverter()); + conversionService.addConverter(new LocalDateToStringConverter()); + conversionService.addConverter(new StringToLocalDateConverter()); + conversionService.addConverter(new LocalTimeToStringConverter()); + conversionService.addConverter(new StringToLocalTimeConverter()); + conversionService.addConverter(new LocalDateTimeToStringConverter()); + conversionService.addConverter(new StringToLocalDateTimeConverter()); this.conversionService = conversionService; } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java index 86c3afe80..83e18b1f4 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java @@ -27,7 +27,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.converter.DateToStringConverter; +import org.springframework.batch.core.converter.LocalDateTimeToStringConverter; +import org.springframework.batch.core.converter.LocalDateToStringConverter; +import org.springframework.batch.core.converter.LocalTimeToStringConverter; import org.springframework.batch.core.converter.StringToDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateConverter; +import org.springframework.batch.core.converter.StringToLocalDateTimeConverter; +import org.springframework.batch.core.converter.StringToLocalTimeConverter; import org.springframework.batch.core.repository.ExecutionContextSerializer; import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao; import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer; @@ -241,6 +247,12 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new DateToStringConverter()); conversionService.addConverter(new StringToDateConverter()); + conversionService.addConverter(new LocalDateToStringConverter()); + conversionService.addConverter(new StringToLocalDateConverter()); + conversionService.addConverter(new LocalTimeToStringConverter()); + conversionService.addConverter(new StringToLocalTimeConverter()); + conversionService.addConverter(new LocalDateTimeToStringConverter()); + conversionService.addConverter(new StringToLocalDateTimeConverter()); this.conversionService = conversionService; } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java index f1dde5deb..1b7d96db9 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/DefaultJobParametersConverterTests.java @@ -16,15 +16,12 @@ package org.springframework.batch.core.converter; import java.time.LocalDate; -import java.time.format.DateTimeFormatter; import java.util.Properties; import org.junit.jupiter.api.Test; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.util.StringUtils; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -104,21 +101,12 @@ class DefaultJobParametersConverterTests { @Test void testGetParameters() throws Exception { - LocalDate date = LocalDate.of(2008, 1, 23); String jobKey = "job.key=myKey"; String scheduleDate = "schedule.date=2008-01-23,java.time.LocalDate,true"; String vendorId = "vendor.id=33243243,java.lang.Long,true"; String[] args = new String[] { jobKey, scheduleDate, vendorId }; - DefaultConversionService conversionService = new DefaultConversionService(); - conversionService.addConverter(String.class, LocalDate.class, new Converter() { - @Override - public LocalDate convert(String source) { - return LocalDate.parse(source); - } - }); - factory.setConversionService(conversionService); JobParameters props = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "=")); assertNotNull(props); assertEquals("myKey", props.getString("job.key")); @@ -206,14 +194,6 @@ class DefaultJobParametersConverterTests { .addJobParameter("schedule.date", date, LocalDate.class, true).addString("job.key", "myKey") .addLong("vendor.id", 33243243L).addDouble("double.key", 1.23).toJobParameters(); - DefaultConversionService conversionService = new DefaultConversionService(); - conversionService.addConverter(LocalDate.class, String.class, new Converter() { - @Override - public String convert(LocalDate source) { - return source.format(DateTimeFormatter.ISO_DATE); - } - }); - factory.setConversionService(conversionService); Properties props = factory.getProperties(parameters); assertNotNull(props); assertEquals("myKey,java.lang.String,true", props.getProperty("job.key")); @@ -228,20 +208,6 @@ class DefaultJobParametersConverterTests { String[] args = new String[] { "schedule.date=2008-01-23,java.time.LocalDate", "job.key=myKey", "vendor.id=33243243,java.lang.Long", "double.key=1.23,java.lang.Double" }; - DefaultConversionService conversionService = new DefaultConversionService(); - conversionService.addConverter(String.class, LocalDate.class, new Converter() { - @Override - public LocalDate convert(String source) { - return LocalDate.parse(source); - } - }); - conversionService.addConverter(LocalDate.class, String.class, new Converter() { - @Override - public String convert(LocalDate source) { - return source.format(DateTimeFormatter.ISO_DATE); - } - }); - factory.setConversionService(conversionService); JobParameters parameters = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "=")); Properties props = factory.getProperties(parameters); @@ -258,20 +224,6 @@ class DefaultJobParametersConverterTests { String[] args = new String[] { "schedule.date=2008-01-23,java.time.LocalDate", "job.key=myKey", "vendor.id=33243243,java.lang.Long,false", "double.key=1.23,java.lang.Double" }; - DefaultConversionService conversionService = new DefaultConversionService(); - conversionService.addConverter(String.class, LocalDate.class, new Converter() { - @Override - public LocalDate convert(String source) { - return LocalDate.parse(source); - } - }); - conversionService.addConverter(LocalDate.class, String.class, new Converter() { - @Override - public String convert(LocalDate source) { - return source.format(DateTimeFormatter.ISO_DATE); - } - }); - factory.setConversionService(conversionService); JobParameters parameters = factory.getJobParameters(StringUtils.splitArrayElementsIntoProperties(args, "=")); Properties props = factory.getProperties(parameters); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalDateTimeToStringConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalDateTimeToStringConverterTests.java new file mode 100644 index 000000000..f252925cf --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalDateTimeToStringConverterTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test class for {@link LocalDateTimeToStringConverter}. + * + * @author Mahmoud Ben Hassine + */ +class LocalDateTimeToStringConverterTests { + + private final LocalDateTimeToStringConverter converter = new LocalDateTimeToStringConverter(); + + @Test + void testConvert() { + // given + LocalDateTime localDateTime = LocalDateTime.of(LocalDate.EPOCH, LocalTime.NOON); + + // when + String converted = this.converter.convert(localDateTime); + + // then + Assertions.assertEquals("1970-01-01T12:00:00", converted); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalDateToStringConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalDateToStringConverterTests.java new file mode 100644 index 000000000..28e24313b --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalDateToStringConverterTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDate; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test class for {@link LocalDateToStringConverter}. + * + * @author Mahmoud Ben Hassine + */ +class LocalDateToStringConverterTests { + + private final LocalDateToStringConverter converter = new LocalDateToStringConverter(); + + @Test + void testConvert() { + // given + LocalDate date = LocalDate.EPOCH; + + // when + String converted = this.converter.convert(date); + + // then + Assertions.assertEquals("1970-01-01", converted); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalTimeToStringConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalTimeToStringConverterTests.java new file mode 100644 index 000000000..442b67e1f --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/LocalTimeToStringConverterTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalTime; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test class for {@link LocalTimeToStringConverter}. + * + * @author Mahmoud Ben Hassine + */ +class LocalTimeToStringConverterTests { + + private final LocalTimeToStringConverter converter = new LocalTimeToStringConverter(); + + @Test + void testConvert() { + // given + LocalTime time = LocalTime.NOON; + + // when + String converted = this.converter.convert(time); + + // then + Assertions.assertEquals("12:00:00", converted); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalDateConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalDateConverterTests.java new file mode 100644 index 000000000..d284cb85c --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalDateConverterTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDate; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test class for {@link StringToLocalDateConverter}. + * + * @author Mahmoud Ben Hassine + */ +class StringToLocalDateConverterTests { + + private final StringToLocalDateConverter converter = new StringToLocalDateConverter(); + + @Test + void convert() { + // given + String date = "1970-01-01"; + + // when + LocalDate converted = this.converter.convert(date); + + // then + Assertions.assertEquals(LocalDate.EPOCH, converted); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalDateTimeConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalDateTimeConverterTests.java new file mode 100644 index 000000000..3b0ddcd2a --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalDateTimeConverterTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test class for {@link StringToLocalDateTimeConverter}. + * + * @author Mahmoud Ben Hassine + */ +class StringToLocalDateTimeConverterTests { + + private final StringToLocalDateTimeConverter converter = new StringToLocalDateTimeConverter(); + + @Test + void convert() { + // given + String dateTime = "1970-01-01T12:00:00"; + + // when + LocalDateTime converted = this.converter.convert(dateTime); + + // then + Assertions.assertEquals(LocalDateTime.of(LocalDate.EPOCH, LocalTime.NOON), converted); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalTimeConverterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalTimeConverterTests.java new file mode 100644 index 000000000..4106cfc9d --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/converter/StringToLocalTimeConverterTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 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.converter; + +import java.time.LocalTime; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test class for {@link StringToLocalTimeConverter}. + * + * @author Mahmoud Ben Hassine + */ +class StringToLocalTimeConverterTests { + + private final StringToLocalTimeConverter converter = new StringToLocalTimeConverter(); + + @Test + void convert() { + // given + String time = "12:00:00"; + + // when + LocalTime converted = this.converter.convert(time); + + // then + Assertions.assertEquals(LocalTime.NOON, converted); + } + +} \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDaoTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDaoTests.java index c4f1b9bd1..d7d49c036 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDaoTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDaoTests.java @@ -15,6 +15,9 @@ */ package org.springframework.batch.core.repository.dao; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -109,12 +112,16 @@ public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests { void testJobParametersPersistenceRoundTrip() { // given Date dateParameter = new Date(); + LocalDate localDateParameter = LocalDate.now(); + LocalTime localTimeParameter = LocalTime.now(); + LocalDateTime localDateTimeParameter = LocalDateTime.now(); String stringParameter = "foo"; long longParameter = 1L; double doubleParameter = 2D; JobParameters jobParameters = new JobParametersBuilder().addString("string", stringParameter) .addLong("long", longParameter).addDouble("double", doubleParameter).addDate("date", dateParameter) - .toJobParameters(); + .addLocalDate("localDate", localDateParameter).addLocalTime("localTime", localTimeParameter) + .addLocalDateTime("localDateTime", localDateTimeParameter).toJobParameters(); JobExecution execution = new JobExecution(jobInstance, jobParameters); // when @@ -125,6 +132,9 @@ public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests { JobParameters parameters = execution.getJobParameters(); Assertions.assertNotNull(parameters); Assertions.assertEquals(dateParameter, parameters.getDate("date")); + Assertions.assertEquals(localDateParameter, parameters.getLocalDate("localDate")); + Assertions.assertEquals(localTimeParameter, parameters.getLocalTime("localTime")); + Assertions.assertEquals(localDateTimeParameter, parameters.getLocalDateTime("localDateTime")); Assertions.assertEquals(stringParameter, parameters.getString("string")); Assertions.assertEquals(longParameter, parameters.getLong("long")); Assertions.assertEquals(doubleParameter, parameters.getDouble("double"));