Search in sources :

Example 6 with TemplateHashModel

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

the class Java8BeansWrapperBridgeMethodsTest method test.

private void test(Class<?> pClass) throws TemplateModelException {
    BeansWrapper ow = new BeansWrapperBuilder(Configuration.VERSION_2_3_26).build();
    TemplateHashModel wrapped;
    try {
        wrapped = (TemplateHashModel) ow.wrap(pClass.newInstance());
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    TemplateMethodModelEx m1 = (TemplateMethodModelEx) wrapped.get("m1");
    assertEquals(BridgeMethodsBean.M1_RETURN_VALUE, "" + m1.exec(Collections.emptyList()));
    TemplateMethodModelEx m2 = (TemplateMethodModelEx) wrapped.get("m2");
    assertNull(m2.exec(Collections.emptyList()));
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateModelException(freemarker.template.TemplateModelException)

Example 7 with TemplateHashModel

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

the class Java8BeansWrapperTest method testDefaultMethodRecognized.

@Test
public void testDefaultMethodRecognized() throws TemplateModelException {
    BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0);
    owb.setTreatDefaultMethodsAsBeanMembers(true);
    BeansWrapper ow = owb.build();
    TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean());
    {
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString());
    }
    {
        // This is overridden in the subclass, so it's visible even without default method support:
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString());
    }
    {
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP_VALUE, prop.getAsString());
    }
    {
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We see default method indexed read method, but it's invalidated by normal getter in the subclass
        TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber());
    }
    {
        // The default method read method invalidates the indexed read method in the subclass
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2_VALUE, prop.getAsString());
    }
    {
        // The default method read method invalidates the indexed read method in the subclass
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3_VALUE_0, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We see the default method indexed reader, which overrides the plain array reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2_VALUE_0, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We do see the default method non-indexed reader, but the subclass has a matching indexed reader, so that
        // takes over.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // Only present in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.INDEXED_PROP_4);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) Test(org.junit.Test)

Example 8 with TemplateHashModel

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

the class Java8BeansWrapperTest method testDefaultMethodNotRecognized.

@Test
public void testDefaultMethodNotRecognized() throws TemplateModelException {
    BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0);
    // owb.setTreatDefaultMethodsAsBeanMembers(false);
    BeansWrapper ow = owb.build();
    TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean());
    {
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString());
    }
    {
        // This is overridden in the subclass, so it's visible even without default method support:
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString());
    }
    assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP));
    assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP));
    {
        // We don't see the default method indexed reader, but see the plain reader method in the subclass.
        TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber());
    }
    {
        // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_2_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_3_VALUE, ((TemplateNumberModel) prop.get(0)).getAsNumber());
    }
    {
        // We don't see the default method indexed reader, but see the plain array reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.ARRAY_PROP_2_VALUE_0, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // Only present in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.INDEXED_PROP_4);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We don't see the default method non-indexed reader
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_ACTION));
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) Test(org.junit.Test)

Example 9 with TemplateHashModel

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

the class BeansWrapperMiscTest method java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest.

@Test
public void java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest() throws TemplateModelException {
    assertTrue("This test case must be ran on Java 8 or later", _JavaVersions.JAVA_8 != null);
    assertFalse(Modifier.isPublic(BeanWithInaccessibleIndexedProperty.class.getModifiers()));
    for (Version ici : new Version[] { Configuration.VERSION_2_3_26, Configuration.VERSION_2_3_27 }) {
        BeansWrapper bw = new BeansWrapper(ici);
        TemplateHashModel beanTM = (TemplateHashModel) bw.wrap(new BeanWithInaccessibleIndexedProperty());
        TemplateModel fooTM = beanTM.get("foo");
        assertThat(fooTM, instanceOf(TemplateSequenceModel.class));
        assertEquals("b", ((TemplateScalarModel) ((TemplateSequenceModel) fooTM).get(1)).getAsString());
        // Even with 2.3.26, where the indexed reader was preferred, as it's inaccessible, we use the normal reader:
        assertEquals(2, ((TemplateSequenceModel) fooTM).size());
        TemplateModel barTM = beanTM.get("bar");
        // all read methods inaccessible
        assertNull(barTM);
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) Version(freemarker.template.Version) TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test)

Example 10 with TemplateHashModel

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

the class BeansWrapperSingletonsTest method exposesProperties.

private boolean exposesProperties(BeansWrapper bw) throws TemplateModelException {
    TemplateHashModel thm = (TemplateHashModel) bw.wrap(new C());
    TemplateScalarModel r = (TemplateScalarModel) thm.get("bar");
    if (r == null)
        return false;
    assertEquals("BAR", r.getAsString());
    return true;
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Aggregations

TemplateHashModel (freemarker.template.TemplateHashModel)26 TemplateScalarModel (freemarker.template.TemplateScalarModel)9 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)8 TemplateModelException (freemarker.template.TemplateModelException)8 Test (org.junit.Test)8 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)7 TemplateModel (freemarker.template.TemplateModel)6 TemplateNumberModel (freemarker.template.TemplateNumberModel)4 TemplateException (freemarker.template.TemplateException)3 Version (freemarker.template.Version)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 Configuration (freemarker.template.Configuration)2 SimpleScalar (freemarker.template.SimpleScalar)2 IOException (java.io.IOException)2 URL (java.net.URL)2 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1