|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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 | + |
| 17 | +package org.springframework.batch.extensions.excel.streaming; |
| 18 | + |
| 19 | +import java.util.Locale; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.batch.extensions.excel.mapping.PassThroughRowMapper; |
| 24 | +import org.springframework.batch.item.ExecutionContext; |
| 25 | +import org.springframework.core.io.ClassPathResource; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +public class StreamingXlsxTypesTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void shouldBeAbleToReadMultipleTypes() throws Exception { |
| 33 | + var reader = new StreamingXlsxItemReader<String[]>(); |
| 34 | + reader.setResource(new ClassPathResource("types.xlsx")); |
| 35 | + reader.setRowMapper(new PassThroughRowMapper()); |
| 36 | + reader.setLinesToSkip(1); // Skip header |
| 37 | + reader.setUserLocale(Locale.US); // Use a Locale to not be dependent on environment |
| 38 | + reader.afterPropertiesSet(); |
| 39 | + |
| 40 | + reader.open(new ExecutionContext()); |
| 41 | + |
| 42 | + var row1 = reader.read(); |
| 43 | + var row2 = reader.read(); |
| 44 | + assertThat(row1).containsExactly("1", "1.0", "5/12/24", "13:14:55", "5/12/24 13:14", "hello world"); |
| 45 | + assertThat(row2).containsExactly("2", "2.5", "8/8/23", "11:12:13", "8/8/23 11:12", "world hello"); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void shouldBeAbleToReadMultipleTypesWithDatesAsIso() throws Exception { |
| 51 | + var reader = new StreamingXlsxItemReader<String[]>(); |
| 52 | + reader.setResource(new ClassPathResource("types.xlsx")); |
| 53 | + reader.setRowMapper(new PassThroughRowMapper()); |
| 54 | + reader.setLinesToSkip(1); // Skip header |
| 55 | + reader.setUserLocale(Locale.US); // Use a Locale to not be dependent on environment |
| 56 | + reader.setDatesAsIso(true); |
| 57 | + reader.afterPropertiesSet(); |
| 58 | + |
| 59 | + reader.open(new ExecutionContext()); |
| 60 | + |
| 61 | + var row1 = reader.read(); |
| 62 | + var row2 = reader.read(); |
| 63 | + assertThat(row1).containsExactly("1", "1.0", "2024-05-12T00:00:00", "1899-12-31T13:14:55", "2024-05-12T13:14:55", "hello world"); |
| 64 | + assertThat(row2).containsExactly("2", "2.5", "2023-08-08T00:00:00", "1899-12-31T11:12:13", "2023-08-08T11:12:13", "world hello"); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments