TSK-765: reduced complexity of string replacements
This commit is contained in:
parent
4b39cafd50
commit
23d8d900cc
|
|
@ -12,7 +12,6 @@ import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
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.Collectors;
|
||||||
|
|
@ -143,16 +142,16 @@ public class TestDataGenerator {
|
||||||
|
|
||||||
private SQLReplacer(String dbProductName) throws IOException {
|
private SQLReplacer(String dbProductName) throws IOException {
|
||||||
boolean isDb2 = TaskanaEngineImpl.isDb2(dbProductName);
|
boolean isDb2 = TaskanaEngineImpl.isDb2(dbProductName);
|
||||||
classificationSql = parseAndReplace(getClass().getResourceAsStream(CLASSIFICATION), isDb2);
|
|
||||||
workbasketSql = parseAndReplace(getClass().getResourceAsStream(WORKBASKET), isDb2);
|
|
||||||
taskSql = parseAndReplace(getClass().getResourceAsStream(TASK), isDb2);
|
|
||||||
workbasketAccessListSql = parseAndReplace(getClass().getResourceAsStream(WORKBASKET_ACCESS_LIST), isDb2);
|
|
||||||
distributionTargetSql = parseAndReplace(getClass().getResourceAsStream(DISTRIBUTION_TARGETS), isDb2);
|
|
||||||
objectReferenceSql = parseAndReplace(getClass().getResourceAsStream(OBJECT_REFERENCE), isDb2);
|
|
||||||
attachmentSql = parseAndReplace(getClass().getResourceAsStream(ATTACHMENT), isDb2);
|
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
monitoringTestDataSql = parseAndReplace(getClass().getResourceAsStream(MONITOR_SAMPLE_DATA),
|
classificationSql = parseAndReplace(getClass().getResourceAsStream(CLASSIFICATION), now, isDb2);
|
||||||
x -> replaceRelativeTimeFunction(now, x), isDb2);
|
workbasketSql = parseAndReplace(getClass().getResourceAsStream(WORKBASKET), now, isDb2);
|
||||||
|
taskSql = parseAndReplace(getClass().getResourceAsStream(TASK), now, isDb2);
|
||||||
|
workbasketAccessListSql = parseAndReplace(getClass().getResourceAsStream(WORKBASKET_ACCESS_LIST), now,
|
||||||
|
isDb2);
|
||||||
|
distributionTargetSql = parseAndReplace(getClass().getResourceAsStream(DISTRIBUTION_TARGETS), now, isDb2);
|
||||||
|
objectReferenceSql = parseAndReplace(getClass().getResourceAsStream(OBJECT_REFERENCE), now, isDb2);
|
||||||
|
attachmentSql = parseAndReplace(getClass().getResourceAsStream(ATTACHMENT), now, isDb2);
|
||||||
|
monitoringTestDataSql = parseAndReplace(getClass().getResourceAsStream(MONITOR_SAMPLE_DATA), now, isDb2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -178,30 +177,18 @@ public class TestDataGenerator {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String parseAndReplace(InputStream stream, boolean replace) throws IOException {
|
private static String replaceBooleanWithInteger(String sql) {
|
||||||
return parseAndReplace(stream, Function.identity(), replace);
|
return sql.replaceAll("(?i)true", "1").replaceAll("(?i)false", "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String parseAndReplace(InputStream stream, Function<String, String> furtherProcessing,
|
private static String parseAndReplace(InputStream stream, LocalDateTime now, boolean isDb2) throws IOException {
|
||||||
boolean replace) throws IOException {
|
|
||||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
|
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
|
||||||
String sql;
|
String sql = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||||
if (replace) {
|
if (isDb2) {
|
||||||
StringBuilder builder = new StringBuilder();
|
sql = replaceBooleanWithInteger(sql);
|
||||||
String line;
|
|
||||||
while ((line = bufferedReader.readLine()) != null) {
|
|
||||||
builder.append(
|
|
||||||
line.replaceAll("true|TRUE", "1")
|
|
||||||
.replaceAll("false|FALSE", "0")
|
|
||||||
).append(System.lineSeparator());
|
|
||||||
}
|
}
|
||||||
sql = builder.toString();
|
return replaceRelativeTimeFunction(now, sql);
|
||||||
} else {
|
|
||||||
sql = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
|
|
||||||
}
|
}
|
||||||
return furtherProcessing.apply(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue