Add default converters for java.time types

Resolves #4257
This commit is contained in:
Mahmoud Ben Hassine
2023-02-15 23:50:45 +01:00
parent 30563cd25c
commit 9883cbc30e
21 changed files with 582 additions and 50 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -59,6 +59,12 @@ import org.springframework.util.StringUtils;
* <ul>
* <li>{@link java.util.Date}: in the
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format</li>
* <li>{@link java.time.LocalDate}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format</li>
* <li>{@link java.time.LocalTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format</li>
* <li>{@link java.time.LocalDateTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format</li>
* </ul>
*
* @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;
}

View File

@@ -53,6 +53,12 @@ import org.springframework.batch.core.JobParameters;
* <ul>
* <li>{@link java.util.Date}: in the
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format</li>
* <li>{@link java.time.LocalDate}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format</li>
* <li>{@link java.time.LocalTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format</li>
* <li>{@link java.time.LocalDateTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format</li>
* </ul>
*
* @author Mahmoud Ben Hassine

View File

@@ -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<LocalDateTime, String> {
@Override
public String convert(LocalDateTime source) {
return source.format(super.localDateTimeFormatter);
}
}

View File

@@ -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<LocalDate, String> {
@Override
public String convert(LocalDate source) {
return source.format(super.localDateFormatter);
}
}

View File

@@ -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<LocalTime, String> {
@Override
public String convert(LocalTime source) {
return source.format(super.localTimeFormatter);
}
}

View File

@@ -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<String, LocalDate> {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, super.localDateFormatter);
}
}

View File

@@ -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<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, super.localDateTimeFormatter);
}
}

View File

@@ -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<String, LocalTime> {
@Override
public LocalTime convert(String source) {
return LocalTime.parse(source, super.localTimeFormatter);
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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<String, LocalDate>() {
@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<LocalDate, String>() {
@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<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source);
}
});
conversionService.addConverter(LocalDate.class, String.class, new Converter<LocalDate, String>() {
@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<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source);
}
});
conversionService.addConverter(LocalDate.class, String.class, new Converter<LocalDate, String>() {
@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);

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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"));