// good
}
}
public void testSettingParser() throws TemplateException {
Configuration cfg = new Configuration();
cfg.setSetting("new_builtin_class_resolver",
"trusted_templates: foo.ftl, \"lib/*\"");
TemplateClassResolver res = cfg.getNewBuiltinClassResolver();
assertEquals(String.class, res.resolve("java.lang.String", null,
Template.getPlainTextTemplate("foo.ftl", "", cfg)));
assertEquals(String.class, res.resolve("java.lang.String", null,
Template.getPlainTextTemplate("lib/bar.ftl", "", cfg)));
try {
res.resolve("java.lang.String", null,
Template.getPlainTextTemplate("bar.ftl", "", cfg));
fail();
} catch (TemplateException e) {
// good
}
cfg.setSetting("new_builtin_class_resolver",
"allowed_classes: java.lang.String, java.lang.Integer");
res = cfg.getNewBuiltinClassResolver();
assertEquals(String.class, res.resolve("java.lang.String", null,
Template.getPlainTextTemplate("foo.ftl", "", cfg)));
assertEquals(Integer.class, res.resolve("java.lang.Integer", null,
Template.getPlainTextTemplate("foo.ftl", "", cfg)));
try {
res.resolve("java.lang.Long", null,
Template.getPlainTextTemplate("foo.ftl", "", cfg));
fail();
} catch (TemplateException e) {
// good
}
cfg.setSetting("new_builtin_class_resolver",
"trusted_templates: foo.ftl, 'lib/*', " +
"allowed_classes: 'java.lang.String', java.lang.Integer");
res = cfg.getNewBuiltinClassResolver();
assertEquals(String.class, res.resolve("java.lang.String", null,
Template.getPlainTextTemplate("x.ftl", "", cfg)));
assertEquals(Integer.class, res.resolve("java.lang.Integer", null,
Template.getPlainTextTemplate("x.ftl", "", cfg)));
try {
res.resolve("java.lang.Long", null,
Template.getPlainTextTemplate("x.ftl", "", cfg));
fail();
} catch (TemplateException e) {
// good
}
assertEquals(Long.class, res.resolve("java.lang.Long", null,
Template.getPlainTextTemplate("foo.ftl", "", cfg)));
assertEquals(Long.class, res.resolve("java.lang.Long", null,
Template.getPlainTextTemplate("lib/bar.ftl", "", cfg)));
try {
res.resolve("java.lang.Long", null,
Template.getPlainTextTemplate("x.ftl", "", cfg));
fail();
} catch (TemplateException e) {
// good
}
try {
cfg.setSetting("new_builtin_class_resolver", "wrong: foo");
fail();
} catch (TemplateException e) {
// good
}
cfg.setSetting("new_builtin_class_resolver",
"\"allowed_classes\" : java.lang.String , " +
"'trusted_templates' :\"lib:*\"");
res = cfg.getNewBuiltinClassResolver();
assertEquals(String.class, res.resolve("java.lang.String", null,
Template.getPlainTextTemplate("x.ftl", "", cfg)));
try {
res.resolve("java.lang.Long", null,
Template.getPlainTextTemplate("x.ftl", "", cfg));