|
17 | 17 | package org.springframework.batch.test;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.net.URI; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.FileSystemNotFoundException; |
| 23 | +import java.nio.file.FileSystems; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.Collections; |
20 | 27 | import java.util.List;
|
| 28 | +import java.util.stream.Collectors; |
| 29 | +import java.util.stream.Stream; |
21 | 30 |
|
22 | 31 | import javax.sql.DataSource;
|
23 | 32 |
|
24 |
| -import org.apache.commons.io.IOUtils; |
25 | 33 | import org.apache.commons.logging.Log;
|
26 | 34 | import org.apache.commons.logging.LogFactory;
|
27 | 35 | import org.springframework.beans.factory.BeanInitializationException;
|
@@ -126,8 +134,8 @@ public Void doInTransaction(TransactionStatus status) {
|
126 | 134 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
127 | 135 | String[] scripts;
|
128 | 136 | try {
|
129 |
| - scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource |
130 |
| - .getInputStream(), "UTF-8")), ";"); |
| 137 | + scripts = StringUtils |
| 138 | + .delimitedListToStringArray(stripComments(getScriptLines(scriptResource)), ";"); |
131 | 139 | }
|
132 | 140 | catch (IOException e) {
|
133 | 141 | throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
|
@@ -155,6 +163,24 @@ public Void doInTransaction(TransactionStatus status) {
|
155 | 163 |
|
156 | 164 | }
|
157 | 165 |
|
| 166 | + private List<String> getScriptLines(Resource scriptResource) throws IOException { |
| 167 | + URI uri = scriptResource.getURI(); |
| 168 | + initFileSystem(uri); |
| 169 | + return Files.readAllLines(Paths.get(uri), StandardCharsets.UTF_8); |
| 170 | + } |
| 171 | + |
| 172 | + private void initFileSystem(URI uri) throws IOException { |
| 173 | + try { |
| 174 | + FileSystems.getFileSystem(uri); |
| 175 | + } |
| 176 | + catch (FileSystemNotFoundException e) { |
| 177 | + FileSystems.newFileSystem(uri, Collections.emptyMap()); |
| 178 | + } |
| 179 | + catch (IllegalArgumentException e) { |
| 180 | + FileSystems.getDefault(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
158 | 184 | private String stripComments(List<String> list) {
|
159 | 185 | StringBuilder buffer = new StringBuilder();
|
160 | 186 | for (String line : list) {
|
|
0 commit comments