Search in sources :

Example 1 with TemplateLookupResult

use of freemarker.cache.TemplateLookupResult in project freemarker by apache.

the class TemplateNotFoundMessageTest method testOtherMessageDetails.

@Test
public void testOtherMessageDetails() throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
    cfg.setTemplateLoader(new StringTemplateLoader());
    {
        String errMsg = failWith("../x", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, allOf(containsStringIgnoringCase("reason"), containsStringIgnoringCase("root directory")));
    }
    {
        String errMsg = failWith("x\u0000y", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, allOf(containsStringIgnoringCase("reason"), containsStringIgnoringCase("null character")));
    }
    {
        String errMsg = failWith("x\\y", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, allOf(containsStringIgnoringCase("warning"), containsStringIgnoringCase("backslash")));
    }
    {
        String errMsg = failWith("x/./y", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, allOf(containsStringIgnoringCase("normalized"), containsStringIgnoringCase("x/y")));
    }
    {
        String errMsg = failWith("/x/y", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, not(containsStringIgnoringCase("normalized")));
    }
    {
        String errMsg = failWith("x/y", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, not(containsStringIgnoringCase("normalized")));
        assertThat(errMsg, not(containsStringIgnoringCase("lookup strategy")));
    }
    cfg.setTemplateLookupStrategy(new TemplateLookupStrategy() {

        @Override
        public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException {
            return ctx.lookupWithAcquisitionStrategy(ctx.getTemplateName());
        }
    });
    {
        String errMsg = failWith("x/y", cfg);
        showErrorMessage(errMsg);
        assertThat(errMsg, containsStringIgnoringCase("lookup strategy"));
    }
    try {
        cfg.getTemplate("./missing", null, "example.com", null, true, false);
        fail();
    } catch (TemplateNotFoundException e) {
        showErrorMessage(e.getMessage());
        assertThat(e.getMessage(), containsStringIgnoringCase("example.com"));
    }
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) TemplateLookupStrategy(freemarker.cache.TemplateLookupStrategy) IOException(java.io.IOException) TemplateLookupContext(freemarker.cache.TemplateLookupContext) TemplateLookupResult(freemarker.cache.TemplateLookupResult) Test(org.junit.Test)

Aggregations

StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 TemplateLookupContext (freemarker.cache.TemplateLookupContext)1 TemplateLookupResult (freemarker.cache.TemplateLookupResult)1 TemplateLookupStrategy (freemarker.cache.TemplateLookupStrategy)1 IOException (java.io.IOException)1 Test (org.junit.Test)1