TdOperatorFactory implements OperatorFactory { @VisibleForTesting static String insertCommandStatement(String command, String original) { Matcher ml = INSERT_LINE_PATTERN.matcher(original); if (ml.find()) { return ml.replaceFirst(ml.group(1) + command); } Matcher mc = HEADER_COMMENT_BLOCK_PATTERN.matcher(original); if (mc.find()) { return mc.group(1) + "\n" + command + "\n" + mc.group(2); } return command + "\n" + original; } @Inject  TdOperatorFactory(TemplateEngine templateEngine, @Environment Map<String, String> env, Config systemConfig); String getType(); @Override Operator newOperator(OperatorContext context);  }
@Test public void verifyCommandInserts() { assertEquals( "INSERT\n" + "select 1", insertCommandStatement("INSERT", "select 1")); assertEquals( "with a as (select 1)\n" + "INSERT\n" + "select 1", insertCommandStatement("INSERT", "with a as (select 1)\n" + "-- DIGDAG_INSERT_LINE\n" + "select 1")); assertEquals( "with a as (select 1)\n" + "INSERT\n" + "select 1", insertCommandStatement("INSERT", "with a as (select 1)\n" + "-- DIGDAG_INSERT_LINE xyz\n" + "select 1")); assertEquals( "with a as (select 1)\n" + "INSERT\n" + "-- comment\n" + "select 1", insertCommandStatement("INSERT", "with a as (select 1)\n" + "--DIGDAG_INSERT_LINE\n" + "-- comment\n" + "select 1")); assertEquals( "-- comment\n" + "INSERT\n" + "select 1", insertCommandStatement("INSERT", "-- comment\n" + "select 1")); assertEquals( "INSERT\n" + "select 1\n" + "-- comment\n" + "from table", insertCommandStatement("INSERT", "select 1\n" + "-- comment\n" + "from table")); assertEquals( "-- comment\r\n" + "INSERT\n" + "select 1", insertCommandStatement("INSERT", "-- comment\r\n" + "select 1")); assertEquals( "-- comment1\n" + "--comment2\n" + "INSERT\n" + "select 1", insertCommandStatement("INSERT", "-- comment1\n" + "--comment2\n" + "select 1")); { String command = "INSERT"; String query = "SELECT\n" + "-- comment1\n" + "1;\n" + "-- comment2\n"; String expected = command + "\n" + query; assertThat(insertCommandStatement(command, query), is(expected)); } }