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