Skip to content

Commit

Permalink
Merge branch 'master' into 731-proxy-bean
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-anisimov committed Jun 7, 2016
2 parents 4c41458 + 165b520 commit b811c87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 78 deletions.
@@ -1,3 +1,4 @@
ifdef::env-github[:outfilesuffix: .asciidoc]
= Adding Components to a Template

You can add child components to templates using the `Component` or `Element` API as long as the template element does not have any children.
Expand Down
Expand Up @@ -122,7 +122,8 @@ default void importBean(Object bean) {
*/
default <T> T getProxy(String modelPath, Class<T> beanType) {
// The method is handled by proxy handler
return null;
throw new UnsupportedOperationException(
"The method implementation is povided by proxy handler");
}

/**
Expand Down
Expand Up @@ -20,9 +20,9 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -40,6 +40,8 @@ public class TemplateModelBeanUtil {
private static final Class<?>[] SUPPORTED_PROPERTY_TYPES = new Class[] {
Boolean.class, Double.class, Integer.class, String.class };

private static final Pattern DOT_PATTERN = Pattern.compile("\\.");

/**
* Internal implementation of Pair / Tuple that encapsulates a value and the
* method that it was retrieved from.
Expand Down Expand Up @@ -138,15 +140,13 @@ static Object getProxy(StateNode stateNode, Object[] args) {
}

StateNode node = stateNode;
StringTokenizer tokenizer = new StringTokenizer(modelPath, ".");
String last = null;
while (tokenizer.hasMoreTokens()) {
String name = tokenizer.nextToken();
node = resolveStateNode(node, name);
last = name;

String[] path = DOT_PATTERN.split(modelPath);
for (int i = 0; i < path.length; i++) {
node = resolveStateNode(node, path[i]);
}
return getModelValue(node.getParent().getFeature(ModelMap.class), last,
beanClass);
return getModelValue(node.getParent().getFeature(ModelMap.class),
path[path.length - 1], beanClass);
}

private static void setModelValueBasicType(ModelMap modelMap,
Expand Down Expand Up @@ -290,4 +290,4 @@ private static Object getPrimitiveDefaultValue(Class<?> primitiveType) {
+ ", all supported types are: "
+ getSupportedTypesString());
}
}
}
Expand Up @@ -507,8 +507,8 @@ public void getProxyInterface_getValueFromProxy_proxyIsNotNullAndProxyValueEqual
SubBeansModel model = template.getModel();
SubBeanIface proxy = model.getProxy("bean", SubBeanIface.class);

verifyBeanValue(template, () -> proxy.getValue(), "bean", "value",
"foo");
setModelPropertyAndVerifyGetter(template, () -> proxy.getValue(),
"bean", "value", "foo");
}

@Test
Expand All @@ -518,8 +518,8 @@ public void getProxyInterface_getSubIfacePropertyValueFromProxy_proxyIsNotNullAn
SubSubBeanIface proxy = model.getProxy("bean.bean",
SubSubBeanIface.class);

verifyBeanValue(template, () -> proxy.getValue(), "bean", "bean",
"value", 2);
setModelPropertyAndVerifyGetter(template, () -> proxy.getValue(),
"bean.bean", "value", 2);
}

@Test
Expand All @@ -528,8 +528,8 @@ public void getProxyInterface_getSubClassPropertyValueFromProxy_proxyIsNotNullAn
SubBeansModel model = template.getModel();
SubBean proxy = model.getProxy("bean.beanClass", SubBean.class);

verifyBeanValue(template, () -> proxy.isVisible(), "bean", "beanClass",
"visible", true);
setModelPropertyAndVerifyGetter(template, () -> proxy.isVisible(),
"bean.beanClass", "visible", true);
}

@Test
Expand All @@ -539,8 +539,8 @@ public void getProxyClass_getSubIfacePropertyValueFromProxy_proxyIsNotNullAndPro
SubSubBeanIface proxy = model.getProxy("beanClass.bean",
SubSubBeanIface.class);

verifyBeanValue(template, () -> proxy.getValue(), "beanClass", "bean",
"value", 5);
setModelPropertyAndVerifyGetter(template, () -> proxy.getValue(),
"beanClass.bean", "value", 5);
}

@Test
Expand All @@ -559,8 +559,8 @@ public void getProxyClass_getValueFromProxy_proxyIsNotNullAndProxyValueEqualsMod
SubBeansModel model = template.getModel();
SubBean proxy = model.getProxy("beanClass", SubBean.class);

verifyBeanValue(template, () -> proxy.isVisible(), "beanClass",
"visible", true);
setModelPropertyAndVerifyGetter(template, () -> proxy.isVisible(),
"beanClass", "visible", true);
}

@Test
Expand All @@ -585,7 +585,7 @@ public void getProxyInterface_getSubIfacePropertyValueFromProxy_proxyIsNotNullAn
subBean.setValue("foo");

verifyModel(template, "bean", "value", "foo");
verifyModel(template, "bean", "bean", "value", 3);
verifyModel(template, "bean.bean", "value", 3);
}

@Test
Expand All @@ -599,7 +599,7 @@ public void getProxyInterface_getSubClassPropertyValueFromProxy_proxyIsNotNullAn
subBean.setValue("foo");

verifyModel(template, "bean", "value", "foo");
verifyModel(template, "bean", "beanClass", "visible", true);
verifyModel(template, "bean.beanClass", "visible", true);
}

@Test
Expand All @@ -614,7 +614,7 @@ public void getProxyClass_getSubIfacePropertyValueFromProxy_proxyIsNotNullAndVal
subBean.setVisible(true);

verifyModel(template, "beanClass", "visible", true);
verifyModel(template, "beanClass", "bean", "value", 3);
verifyModel(template, "beanClass.bean", "value", 3);
}

@Test
Expand All @@ -629,7 +629,7 @@ public void getProxyClass_getSubClassPropertyValueFromProxy_proxyIsNotNullAndVal
subBean.setVisible(true);

verifyModel(template, "beanClass", "visible", true);
verifyModel(template, "beanClass", "subBean", "value", 5d);
verifyModel(template, "beanClass.subBean", "value", 5d);
}

@Test
Expand Down Expand Up @@ -660,69 +660,38 @@ public int getValue() {
Assert.assertEquals(4, subProxy.getValue());
}

private void verifyBeanValue(Template template, Supplier<Object> getter,
String beanPath, String property, Serializable expected) {
Serializable bean = template.getElement().getNode()
.getFeature(ModelMap.class).getValue(beanPath);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
StateNode node = (StateNode) bean;
verifyBeanValue(node.getFeature(ModelMap.class), getter, property,
expected);
}

private void verifyBeanValue(ModelMap feature, Supplier<Object> getter,
String property, Serializable expected) {
feature.setValue(property, expected);
Assert.assertEquals(expected, getter.get());
}

private void verifyBeanValue(Template template, Supplier<Object> getter,
String beanPath1, String beanPath2, String property,
private void setModelPropertyAndVerifyGetter(Template template,
Supplier<Object> getter, String beanPath, String property,
Serializable expected) {
Serializable bean = template.getElement().getNode()
.getFeature(ModelMap.class).getValue(beanPath1);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
StateNode node = (StateNode) bean;
StateNode node = template.getElement().getNode();
String[] path = beanPath.split("\\.");
for (int i = 0; i < path.length; i++) {
Serializable bean = node.getFeature(ModelMap.class)
.getValue(path[i]);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
node = (StateNode) bean;
}

ModelMap feature = node.getFeature(ModelMap.class);
bean = feature.getValue(beanPath2);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
node = (StateNode) bean;
verifyBeanValue(node.getFeature(ModelMap.class), getter, property,
expected);
feature.setValue(property, expected);
Assert.assertEquals(expected, getter.get());
}

private void verifyModel(Template template, String beanPath,
String property, Serializable expected) {
Serializable bean = template.getElement().getNode()
.getFeature(ModelMap.class).getValue(beanPath);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
StateNode node = (StateNode) bean;
StateNode node = template.getElement().getNode();
String[] path = beanPath.split("\\.");
for (int i = 0; i < path.length; i++) {
Serializable bean = node.getFeature(ModelMap.class)
.getValue(path[i]);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
node = (StateNode) bean;
}
ModelMap feature = node.getFeature(ModelMap.class);
Assert.assertNotNull(feature);
Assert.assertEquals(expected, feature.getValue(property));
}

private void verifyModel(Template template, String beanPath1,
String beanPath2, String property, Serializable expected) {
Serializable bean = template.getElement().getNode()
.getFeature(ModelMap.class).getValue(beanPath1);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
StateNode node = (StateNode) bean;
ModelMap feature = node.getFeature(ModelMap.class);
Assert.assertNotNull(feature);

bean = feature.getValue(beanPath2);
Assert.assertNotNull(bean);
Assert.assertTrue(bean instanceof StateNode);
node = (StateNode) bean;
feature = node.getFeature(ModelMap.class);

Assert.assertEquals(expected, feature.getValue(property));
}
}

0 comments on commit b811c87

Please sign in to comment.