@Override public String render(String template, StatementContext ctx) { final ExtensionMethod extensionMethod = ctx.getExtensionMethod(); final String originalResult = originalEngine.render(template, ctx); if (extensionMethod == null) { return originalResult; } final StringBuilder query = new StringBuilder(originalResult.length() + 100); query.append("/* "); final String className = extensionMethod.getType().getSimpleName(); if (!className.isEmpty()) { query.append(className).append('.'); } query.append(extensionMethod.getMethod().getName()); query.append(" */ "); query.append(originalResult); return query.toString(); } }
@Test public void testNullValue() { attributes.put("0", null); assertThat(templateEngine.render("{0} bar", ctx)) .isEqualTo("null bar"); }
@Test public void testNoPlaceholdersNoValues() { attributes.clear(); assertThat(templateEngine.render("foo bar", ctx)).isEqualTo("foo bar"); }
@Test public void testWithPlaceholdersAndValues() { attributes.put("02", "!"); attributes.put("000", "hello"); attributes.put("01", "world"); assertThat(templateEngine.render("{0} {1}{2}", ctx)).isEqualTo("hello world!"); }
@Test public void testEscaping() { attributes.put("0", "foo"); assertThat(templateEngine.render("select * from {0} where name = ''john'' and stuff = '''{0}'''", ctx)) .isEqualTo("select * from foo where name = 'john' and stuff = '{0}'"); }
private String render(String sql, Map<String, Object> attributes) { attributes.forEach((key, value) -> when(ctx.getAttribute(key)).thenReturn(value)); return templateEngine.render(sql, ctx); }
@Test public void testManyValues() { attributes.put("000", "a"); attributes.put("001", "b"); attributes.put("002", "c"); attributes.put("003", "d"); attributes.put("004", "e"); attributes.put("005", "f"); attributes.put("006", "g"); attributes.put("007", "h"); attributes.put("008", "i"); attributes.put("009", "j"); attributes.put("010", "k"); assertThat(templateEngine.render("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}", ctx)).isEqualTo("abcdefghijk"); }
@Test public void testSkippedKey() { attributes.put("0", "hello"); attributes.put("2", "world"); assertThatThrownBy(() -> templateEngine.render("{0} {1}", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("skip from 0 to 2"); }
@Test public void testNoPlaceholdersButWithValues() { attributes.put("0", "hello"); assertThatThrownBy(() -> templateEngine.render("foo bar", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("expected 0 keys but got 1"); }
@Test public void testNullAttribute() { attributes.put("x", null); TemplateEngine engine = StringSubstitutorTemplateEngine.between('<', '>'); assertThat(engine.render("select * from foo where x=<x>", ctx)) .isEqualTo("select * from foo where x=<x>"); }
@Test public void testDuplicateKey() { attributes.put("0", "hello"); attributes.put("00", "world"); assertThatThrownBy(() -> templateEngine.render("{0} {1}", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("0 was given more than once"); }
@Test public void testNonNumericKey() { attributes.put("abc", "hello"); assertThatThrownBy(() -> templateEngine.render("{0} bar", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("\"abc\""); }
@Test public void testCustomPrefixSuffix() { attributes.put("name", "foo"); TemplateEngine engine = StringSubstitutorTemplateEngine.between('<', '>'); assertThat(engine.render("create table <name>;", ctx)) .isEqualTo("create table foo;"); }
@Test public void testNegativeKey() { attributes.put("-1", "hello"); assertThatThrownBy(() -> templateEngine.render("{0} bar", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("must be 0"); }
@Test public void testWhitespaceInKey() { attributes.put(" 1 ", "hello"); assertThatThrownBy(() -> templateEngine.render("{0} bar", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("\" 1 \""); }
@Test public void testEscapeCharacter() { attributes.put("name", "foo"); TemplateEngine engine = StringSubstitutorTemplateEngine.between('<', '>', '@'); assertThat(engine.render("create table @<name>;", ctx)) .isEqualTo("create table <name>;"); } }
@Test public void testWithPlaceholdersButNoValues() { attributes.clear(); assertThatThrownBy(() -> templateEngine.render("{0} bar", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("expected 1 keys but got 0"); }
@Test public void testBlankKey() { attributes.put(" ", "hello"); assertThatThrownBy(() -> templateEngine.render("{0} bar", ctx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("\" \""); }
@Test public void testMissingAttribute() { attributes.clear(); TemplateEngine engine = StringSubstitutorTemplateEngine.between('<', '>'); assertThat(engine.render("select * from foo where x=<x>", ctx)) .isEqualTo("select * from foo where x=<x>"); }
/** * Locate the Script and split it into statements. * @return the split statements */ public List<String> getStatements() { return splitToStatements(getConfig(SqlStatements.class).getTemplateEngine().render(sql, getContext())); }