Search in sources :

Example 11 with TemplateMethodModelEx

use of freemarker.template.TemplateMethodModelEx in project freemarker by apache.

the class ErrorMessagesTest method markupOutputParameter.

@Test
public void markupOutputParameter() throws Exception {
    TemplateHTMLOutputModel html = HTMLOutputFormat.INSTANCE.fromMarkup("<p>a");
    for (Version ici : new Version[] { Configuration.VERSION_2_3_0, Configuration.VERSION_2_3_24 }) {
        BeansWrapper bw = new BeansWrapperBuilder(ici).build();
        TemplateHashModel thm = (TemplateHashModel) bw.wrap(new TestBean());
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("m1");
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("String"), containsString("convert"), containsString("markup_output"), containsString("Tip:"), containsString("?markup_string")));
            }
        }
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("m2");
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("Date"), containsString("convert"), containsString("markup_output"), not(containsString("?markup_string"))));
            }
        }
        for (String methodName : new String[] { "mOverloaded", "mOverloaded3" }) {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get(methodName);
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("No compatible overloaded"), containsString("String"), containsString("markup_output"), containsString("Tip:"), containsString("?markup_string")));
            }
        }
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("mOverloaded2");
            try {
                m.exec(Collections.singletonList(html));
                fail();
            } catch (TemplateModelException e) {
                assertThat(e.getMessage(), allOf(containsString("No compatible overloaded"), containsString("Integer"), containsString("markup_output"), not(containsString("?markup_string"))));
            }
        }
        {
            TemplateMethodModelEx m = (TemplateMethodModelEx) thm.get("mOverloaded4");
            Object r = m.exec(Collections.singletonList(html));
            if (r instanceof TemplateScalarModel) {
                r = ((TemplateScalarModel) r).getAsString();
            }
            assertEquals("<p>a", r);
        }
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateHashModel(freemarker.template.TemplateHashModel) Version(freemarker.template.Version) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateHTMLOutputModel(freemarker.core.TemplateHTMLOutputModel) Test(org.junit.Test)

Aggregations

TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)11 TemplateHashModel (freemarker.template.TemplateHashModel)8 TemplateScalarModel (freemarker.template.TemplateScalarModel)7 TemplateModel (freemarker.template.TemplateModel)6 TemplateModelException (freemarker.template.TemplateModelException)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Map (java.util.Map)3 TemplateException (freemarker.template.TemplateException)2 TemplateNumberModel (freemarker.template.TemplateNumberModel)2 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)2 IOException (java.io.IOException)2 TemplateHTMLOutputModel (freemarker.core.TemplateHTMLOutputModel)1 MalformedTemplateNameException (freemarker.template.MalformedTemplateNameException)1 SimpleHash (freemarker.template.SimpleHash)1 SimpleScalar (freemarker.template.SimpleScalar)1 Template (freemarker.template.Template)1 TemplateDirectiveBody (freemarker.template.TemplateDirectiveBody)1 TemplateDirectiveModel (freemarker.template.TemplateDirectiveModel)1