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.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.sql.DataSource;
@ -151,7 +152,7 @@ public class TestDataGenerator {
attachmentSql = parseAndReplace(getClass().getResourceAsStream(ATTACHMENT), isDb2);
LocalDateTime now = LocalDateTime.now();
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.
* @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);
StringBuffer sb = new StringBuffer(sql.length());
while (m.find()) {
@ -182,26 +183,23 @@ public class TestDataGenerator {
}
private static String parseAndReplace(InputStream stream, Function<String, String> furtherProcessing,
boolean replace)
throws IOException {
boolean replace) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
StringBuilder sql = new StringBuilder();
String line;
String sql;
if (replace) {
StringBuilder builder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sql.append(
furtherProcessing.apply(
line.replaceAll("true|TRUE", "1")
.replaceAll("false|FALSE", "0")
)
).append("\n");
builder.append(
line.replaceAll("true|TRUE", "1")
.replaceAll("false|FALSE", "0")
).append(System.lineSeparator());
}
sql = builder.toString();
} else {
while ((line = bufferedReader.readLine()) != null) {
sql.append(furtherProcessing.apply(line)).append("\n");
}
sql = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
}
return sql.toString();
return furtherProcessing.apply(sql);
}
}

View File

@ -13,6 +13,7 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.sql.DataSource;
@ -69,7 +70,7 @@ public class SampleDataGenerator {
* @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.
*/
private static String convertToRelativeTime(LocalDateTime now, String sql) {
private static String replaceRelativeTimeFunction(LocalDateTime now, String sql) {
Matcher m = RELATIVE_DATE_PATTERN.matcher(sql);
StringBuffer sb = new StringBuffer(sql.length());
while (m.find()) {
@ -82,12 +83,8 @@ public class SampleDataGenerator {
private static String parseAndReplace(LocalDateTime now, InputStream stream) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
StringBuilder sql = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sql.append(convertToRelativeTime(now, line)).append("\n");
}
return sql.toString();
return replaceRelativeTimeFunction(now,
bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())));
} catch (IOException e) {
throw new RuntimeException(e);
}