public static Template findTemplate(Class<?> type, String templateName) { String path = new ClasspathBuilder() .appendFullyQualifiedClassName(type) .appendVerbatim(templateName) .setExtension("sql.ftl") .build(); try { return CONFIGURATION.getTemplate(path); } catch (Exception e) { throw new IllegalStateException("Failed to load Freemarker template " + templateName + " in " + path, e); } }
/** * Loads the StringTemplateGroup for the given type. Example: Given a type <code>com.foo.Bar</code>, returns a * StringTemplateGroup loaded from the resource named <code>com/foo/Bar.sql.stg</code> on the classpath. * * @param type the type that "owns" the given StringTemplate group file. Dictates the filename of the * StringTemplate group file on the classpath. * @return the loaded StringTemplateGroup. */ public static STGroup findStringTemplateGroup(Class<?> type) { String path = new ClasspathBuilder() .appendFullyQualifiedClassName(type) .setExtension("sql.stg") .build(); return findStringTemplateGroup(type.getClassLoader(), path); }
@Test public void testDotPath() { assertThat(builder.appendDotPath("foo.bar.baz").build()) .isEqualTo("foo/bar/baz"); }
@Test public void testFqClass() { assertThat(builder.appendFullyQualifiedClassName(String.class).build()) .isEqualTo("java/lang/String"); }
@Test public void testWhitespace() { assertThat(builder.appendVerbatim("// abc // / xyz / /").build()) .isEqualTo("abc/xyz"); }
@Test public void testWeirdVerbatim() { assertThat(builder.appendVerbatim("foo.bar@x-y-z").build()) .isEqualTo("foo.bar@x-y-z"); }
@Test public void testFqNestedClass() { assertThat(builder.appendFullyQualifiedClassName(Foo.class).build()) .isEqualTo("org/jdbi/v3/core/locator/internal/TestClasspathBuilder$Foo"); }
/** * Locates SQL for the given type and name. Example: Given a type <code>com.foo.Bar</code> and a name of * <code>baz</code>, looks for a resource named <code>com/foo/Bar/baz.sql</code> on the classpath and returns its * contents as a String. * * @param type the type that "owns" the given SQL. Dictates the directory path to the SQL resource file on the * classpath. * @param methodName the SQL statement name (usually a method or field name from the type). * @return the located SQL. */ public static String findSqlOnClasspath(Class<?> type, String methodName) { String path = new ClasspathBuilder() .appendFullyQualifiedClassName(type) .appendVerbatim(methodName) .setExtension(SQL_EXTENSION) .build(); return getResourceOnClasspath(type.getClassLoader(), path); }
/** * Locates SQL for the given fully-qualified name. Example: Given the name <code>com.foo.Bar.baz</code>, looks for * a resource named <code>com/foo/Bar/baz.sql</code> on the classpath and returns its contents as a String. * * @param name fully qualified name. * @return the located SQL. */ public static String findSqlOnClasspath(String name) { ClasspathBuilder builder = new ClasspathBuilder() .appendDotPath(name) .setExtension(SQL_EXTENSION); return getResourceOnClasspath(selectClassLoader(), builder.build()); }
@Test public void testSimpleVerbatimWithExtension() { assertThat(builder.appendVerbatim("foo").appendVerbatim("bar").setExtension("baz").build()) .isEqualTo("foo/bar.baz"); }