Search in sources :

Example 1 with TemplateLookupContext

use of freemarker.cache.TemplateLookupContext 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)

Example 2 with TemplateLookupContext

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

the class ConfigurationTest method testTemplateLookupStrategyDefaultAndSet.

public void testTemplateLookupStrategyDefaultAndSet() throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    assertSame(TemplateLookupStrategy.DEFAULT_2_3_0, cfg.getTemplateLookupStrategy());
    cfg.setClassForTemplateLoading(ConfigurationTest.class, "");
    assertSame(TemplateLookupStrategy.DEFAULT_2_3_0, cfg.getTemplateLookupStrategy());
    CacheStorageWithGetSize cache = (CacheStorageWithGetSize) cfg.getCacheStorage();
    cfg.setClassForTemplateLoading(ConfigurationTest.class, "");
    assertEquals(0, cache.getSize());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cache.getSize());
    cfg.setTemplateLookupStrategy(TemplateLookupStrategy.DEFAULT_2_3_0);
    assertEquals(1, cache.getSize());
    final TemplateLookupStrategy myStrategy = new TemplateLookupStrategy() {

        @Override
        public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException {
            return ctx.lookupWithAcquisitionStrategy(ctx.getTemplateName());
        }
    };
    cfg.setTemplateLookupStrategy(myStrategy);
    assertEquals(0, cache.getSize());
    assertSame(myStrategy, cfg.getTemplateLookupStrategy());
    cfg.getTemplate("toCache1.ftl");
    assertEquals(1, cache.getSize());
    cfg.setTemplateLookupStrategy(myStrategy);
    assertEquals(1, cache.getSize());
    cfg.setTemplateLookupStrategy(TemplateLookupStrategy.DEFAULT_2_3_0);
    assertEquals(0, cache.getSize());
}
Also used : CacheStorageWithGetSize(freemarker.cache.CacheStorageWithGetSize) TemplateLookupStrategy(freemarker.cache.TemplateLookupStrategy) TemplateLookupContext(freemarker.cache.TemplateLookupContext)

Aggregations

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