TSK-765: reduced amount of reader class initialization for test data generation

This commit is contained in:
Mustapha Zorgati 2019-01-03 15:49:55 +01:00 committed by Holger Hagen
parent e69f36b457
commit 4b39cafd50
2 changed files with 18 additions and 23 deletions

View File

@ -15,6 +15,7 @@ import java.time.format.DateTimeFormatter;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -151,7 +152,7 @@ public class TestDataGenerator {
attachmentSql = parseAndReplace(getClass().getResourceAsStream(ATTACHMENT), isDb2); attachmentSql = parseAndReplace(getClass().getResourceAsStream(ATTACHMENT), isDb2);
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
monitoringTestDataSql = parseAndReplace(getClass().getResourceAsStream(MONITOR_SAMPLE_DATA), monitoringTestDataSql = parseAndReplace(getClass().getResourceAsStream(MONITOR_SAMPLE_DATA),
x -> convertToRelativeTime(now, x), isDb2); x -> replaceRelativeTimeFunction(now, x), isDb2);
} }
/** /**
@ -166,7 +167,7 @@ public class TestDataGenerator {
* @param sql sql statement which may contain the above declared custom function. * @param sql sql statement which may contain the above declared custom function.
* @return sql statement with the given function resolved, if the 'sql' parameter contained any. * @return sql statement with the given function resolved, if the 'sql' parameter contained any.
*/ */
private static String convertToRelativeTime(LocalDateTime now, String sql) { private static String replaceRelativeTimeFunction(LocalDateTime now, String sql) {
Matcher m = RELATIVE_DATE_PATTERN.matcher(sql); Matcher m = RELATIVE_DATE_PATTERN.matcher(sql);
StringBuffer sb = new StringBuffer(sql.length()); StringBuffer sb = new StringBuffer(sql.length());
while (m.find()) { while (m.find()) {
@ -182,26 +183,23 @@ public class TestDataGenerator {
} }
private static String parseAndReplace(InputStream stream, Function<String, String> furtherProcessing, private static String parseAndReplace(InputStream stream, Function<String, String> furtherProcessing,
boolean replace) boolean replace) throws IOException {
throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
StringBuilder sql = new StringBuilder(); String sql;
String line;
if (replace) { if (replace) {
StringBuilder builder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
sql.append( builder.append(
furtherProcessing.apply( line.replaceAll("true|TRUE", "1")
line.replaceAll("true|TRUE", "1") .replaceAll("false|FALSE", "0")
.replaceAll("false|FALSE", "0") ).append(System.lineSeparator());
)
).append("\n");
} }
sql = builder.toString();
} else { } else {
while ((line = bufferedReader.readLine()) != null) { sql = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
sql.append(furtherProcessing.apply(line)).append("\n");
}
} }
return sql.toString(); return furtherProcessing.apply(sql);
} }
} }

View File

@ -13,6 +13,7 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -69,7 +70,7 @@ public class SampleDataGenerator {
* @param sql sql statement which may contain the above declared custom function. * @param sql sql statement which may contain the above declared custom function.
* @return sql statement with the given function resolved, if the 'sql' parameter contained any. * @return sql statement with the given function resolved, if the 'sql' parameter contained any.
*/ */
private static String convertToRelativeTime(LocalDateTime now, String sql) { private static String replaceRelativeTimeFunction(LocalDateTime now, String sql) {
Matcher m = RELATIVE_DATE_PATTERN.matcher(sql); Matcher m = RELATIVE_DATE_PATTERN.matcher(sql);
StringBuffer sb = new StringBuffer(sql.length()); StringBuffer sb = new StringBuffer(sql.length());
while (m.find()) { while (m.find()) {
@ -82,12 +83,8 @@ public class SampleDataGenerator {
private static String parseAndReplace(LocalDateTime now, InputStream stream) { private static String parseAndReplace(LocalDateTime now, InputStream stream) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
StringBuilder sql = new StringBuilder(); return replaceRelativeTimeFunction(now,
String line; bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())));
while ((line = bufferedReader.readLine()) != null) {
sql.append(convertToRelativeTime(now, line)).append("\n");
}
return sql.toString();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }