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"));
}
}
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());
}
Aggregations