FREEMARKER-55: unit tests for form.input directive

Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/ae94a3d4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/ae94a3d4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/ae94a3d4

Branch: refs/heads/3
Commit: ae94a3d494ac1da64c89e70a4f021170f064730c
Parents: 2b5e9b7
Author: Woonsan Ko <woon...@apache.org>
Authored: Fri Dec 29 23:54:07 2017 -0500
Committer: Woonsan Ko <woon...@apache.org>
Committed: Fri Dec 29 23:54:07 2017 -0500

----------------------------------------------------------------------
 ...aBoundFormElementTemplateDirectiveModel.java | 34 +++++++++-----------
 .../AbstractFormTemplateDirectiveModel.java     |  4 ++-
 ...stractHtmlElementTemplateDirectiveModel.java | 15 ++++++---
 ...tHtmlInputElementTemplateDirectiveModel.java |  1 +
 .../model/form/InputTemplateDirectiveModel.java |  1 +
 .../form/InputTemplateDirectiveModelTest.java   |  9 +++---
 .../test/model/form/input-directive-usages.ftlh | 27 ++++++++++++++--
 7 files changed, 59 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractDataBoundFormElementTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractDataBoundFormElementTemplateDirectiveModel.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractDataBoundFormElementTemplateDirectiveModel.java
index f50f81d..27bc4fd 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractDataBoundFormElementTemplateDirectiveModel.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractDataBoundFormElementTemplateDirectiveModel.java
@@ -49,7 +49,11 @@ public abstract class 
AbstractDataBoundFormElementTemplateDirectiveModel extends
 
     private static final int ID_PARAM_IDX = 1;
 
-    private static final String ID_PARAM_NAME = "id";
+    private static final String ID_ATTR_NAME = "id";
+
+    private static final String NAME_ATTR_NAME = "name";
+
+    private static final String ID_PARAM_NAME = ID_ATTR_NAME;
 
     protected static List<StringToIndexMap.Entry> NAMED_ARGS_ENTRY_LIST = 
Arrays.asList(
             new StringToIndexMap.Entry(ID_PARAM_NAME, ID_PARAM_IDX)
@@ -102,13 +106,12 @@ public abstract class 
AbstractDataBoundFormElementTemplateDirectiveModel extends
     }
 
     protected void writeDefaultAttributes(TagOutputter tagOut) throws 
TemplateException, IOException {
-        // FIXME
-        writeOptionalAttribute(tagOut, "id", resolveId());
-        writeOptionalAttribute(tagOut, "name", getName());
+        writeOptionalAttribute(tagOut, ID_ATTR_NAME, resolveId());
+        writeOptionalAttribute(tagOut, NAME_ATTR_NAME, getName());
     }
 
     protected String resolveId() throws TemplateException {
-        Object id = evaluate("id", getId());
+        Object id = evaluate(ID_PARAM_NAME, getId());
 
         if (id != null) {
             String idString = id.toString();
@@ -123,29 +126,22 @@ public abstract class 
AbstractDataBoundFormElementTemplateDirectiveModel extends
     }
 
     protected String getName() throws TemplateException {
-        // FIXME
-        return "name";
-        //return getPropertyPath();
+        return getPropertyPath();
     }
 
-    protected String getPropertyPath(Environment env,
-            ObjectWrapperAndUnwrapper objectWrapperAndUnwrapper, 
RequestContext requestContext, String path,
-            boolean ignoreNestedPath) throws TemplateException {
-        BindStatus status = getBindStatus(env, objectWrapperAndUnwrapper, 
requestContext, path, ignoreNestedPath);
-        String expression = status.getExpression();
+    protected String getPropertyPath() {
+        String expression = getBindStatus().getExpression();
         return (expression != null ? expression : "");
     }
 
     protected final String processFieldValue(Environment env, String name, 
String value, String type) throws TemplateException {
         RequestContext requestContext = getRequestContext(env, false);
         RequestDataValueProcessor processor = 
requestContext.getRequestDataValueProcessor();
+        HttpServletRequest request = getRequest();
 
-        // FIXME
-//        ServletRequest request = this.pageContext.getRequest();
-//
-//        if (processor != null && (request instanceof HttpServletRequest)) {
-//            value = processor.processFormFieldValue((HttpServletRequest) 
request, name, value, type);
-//        }
+        if (processor != null && request != null) {
+            value = processor.processFormFieldValue(request, name, value, 
type);
+        }
 
         return value;
     }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractFormTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractFormTemplateDirectiveModel.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractFormTemplateDirectiveModel.java
index 28dd1bf..45fa896 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractFormTemplateDirectiveModel.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractFormTemplateDirectiveModel.java
@@ -53,13 +53,15 @@ public abstract class AbstractFormTemplateDirectiveModel 
extends AbstractSpringT
             try {
                 propertyEditor.setValue(value);
                 String text = propertyEditor.getAsText();
+
                 if (text != null) {
                     return getDisplayString(text, htmlEscape);
                 }
             } catch (Throwable ex) {
-                // The PropertyEditor might not support this value... pass 
through.
+                // Ignore error if the PropertyEditor doesn't support this 
text value.
             }
         }
+
         return getDisplayString(value, htmlEscape);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlElementTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlElementTemplateDirectiveModel.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlElementTemplateDirectiveModel.java
index d982111..1bd8d5c 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlElementTemplateDirectiveModel.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlElementTemplateDirectiveModel.java
@@ -56,9 +56,11 @@ public abstract class 
AbstractHtmlElementTemplateDirectiveModel
 
     private static final int CSS_CLASS_PARAM_IDX = NAMED_ARGS_OFFSET;
     private static final String CSS_CLASS_PARAM_NAME = "cssClass";
+    private static final String CSS_CLASS_ATTR_NAME = "class";
 
     private static final int CSS_STYLE_PARAM_IDX = NAMED_ARGS_OFFSET + 1;
     private static final String CSS_STYLE_PARAM_NAME = "cssStyle";
+    private static final String CSS_STYLE_ATTR_NAME = "style";
 
     private static final int LANG_PARAM_IDX = NAMED_ARGS_OFFSET + 2;
     private static final String LANG_PARAM_NAME = "lang";
@@ -105,6 +107,7 @@ public abstract class 
AbstractHtmlElementTemplateDirectiveModel
     private static final int CSSERRORCLASS_PARAM_IDX = NAMED_ARGS_OFFSET + 16;
     private static final String CSSERRORCLASS_PARAM_NAME = "cssErrorClass";
 
+    @SuppressWarnings("unchecked")
     protected static List<StringToIndexMap.Entry> NAMED_ARGS_ENTRY_LIST =
             _CollectionUtils.mergeImmutableLists(false,
                     
AbstractDataBoundFormElementTemplateDirectiveModel.NAMED_ARGS_ENTRY_LIST,
@@ -318,8 +321,9 @@ public abstract class 
AbstractHtmlElementTemplateDirectiveModel
     }
 
     protected void writeOptionalAttributes(TagOutputter tagOut) throws 
TemplateException, IOException {
-        tagOut.writeOptionalAttributeValue("class", resolveCssClass());
-        tagOut.writeOptionalAttributeValue("style", 
ObjectUtils.getDisplayString(evaluate("cssStyle", getCssStyle())));
+        tagOut.writeOptionalAttributeValue(CSS_CLASS_ATTR_NAME, 
resolveCssClass());
+        tagOut.writeOptionalAttributeValue(CSS_STYLE_ATTR_NAME,
+                ObjectUtils.getDisplayString(evaluate(CSS_STYLE_PARAM_NAME, 
getCssStyle())));
         writeOptionalAttribute(tagOut, LANG_PARAM_NAME, getLang());
         writeOptionalAttribute(tagOut, TITLE_PARAM_NAME, getTitle());
         writeOptionalAttribute(tagOut, DIR_PARAM_NAME, getDir());
@@ -335,9 +339,10 @@ public abstract class 
AbstractHtmlElementTemplateDirectiveModel
         writeOptionalAttribute(tagOut, ONKEYUP_PARAM_NAME, getOnkeyup());
         writeOptionalAttribute(tagOut, ONKEYDOWN_PARAM_NAME, getOnkeydown());
 
-        if (!this.unmodifiableDynamicAttributes.isEmpty()) {
-            for (String attr : this.dynamicAttributes.keySet()) {
-                tagOut.writeOptionalAttributeValue(attr, 
getDisplayString(this.dynamicAttributes.get(attr), false));
+        if (!unmodifiableDynamicAttributes.isEmpty()) {
+            for (String attr : unmodifiableDynamicAttributes.keySet()) {
+                tagOut.writeOptionalAttributeValue(attr,
+                        
getDisplayString(unmodifiableDynamicAttributes.get(attr), false));
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlInputElementTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlInputElementTemplateDirectiveModel.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlInputElementTemplateDirectiveModel.java
index b77bc9d..ef81c1d 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlInputElementTemplateDirectiveModel.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/AbstractHtmlInputElementTemplateDirectiveModel.java
@@ -61,6 +61,7 @@ public abstract class 
AbstractHtmlInputElementTemplateDirectiveModel extends Abs
     private static final int READONLY_PARAM_IDX = NAMED_ARGS_OFFSET + 5;
     private static final String READONLY_PARAM_NAME = "readonly";
 
+    @SuppressWarnings("unchecked")
     protected static List<StringToIndexMap.Entry> NAMED_ARGS_ENTRY_LIST =
             _CollectionUtils.mergeImmutableLists(false,
                     
AbstractHtmlElementTemplateDirectiveModel.NAMED_ARGS_ENTRY_LIST,

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModel.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModel.java
index e53eb0c..e348da8 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModel.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModel.java
@@ -60,6 +60,7 @@ public class InputTemplateDirectiveModel extends 
AbstractHtmlInputElementTemplat
     private static final int AUTOCOMPLETE_PARAM_IDX = NAMED_ARGS_OFFSET + 4;
     private static final String AUTOCOMPLETE_PARAM_NAME = "autocomplete";
 
+    @SuppressWarnings("unchecked")
     protected static List<StringToIndexMap.Entry> NAMED_ARGS_ENTRY_LIST =
             _CollectionUtils.mergeImmutableLists(false,
                     
AbstractHtmlInputElementTemplateDirectiveModel.NAMED_ARGS_ENTRY_LIST,

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModelTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModelTest.java
 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModelTest.java
index 3ce947b..9d8a6e2 100644
--- 
a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModelTest.java
+++ 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/form/InputTemplateDirectiveModelTest.java
@@ -37,6 +37,7 @@ import static 
org.springframework.test.web.servlet.request.MockMvcRequestBuilder
 import static 
org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
 import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
 import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @WebAppConfiguration("classpath:META-INF/web-resources")
@@ -62,10 +63,10 @@ public class InputTemplateDirectiveModelTest {
         final User user = userRepository.getUser(userId);
         mockMvc.perform(get("/users/{userId}/", userId).param("viewName", 
"test/model/form/input-directive-usages")
                 
.accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk())
-                
.andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print());
-                // FIXME
-                
//.andExpect(xpath("//div[@id='userEmail']/input/@type").string("text"));
-                
//.andExpect(xpath("//div[@id='userEmail']/input/@value").string(user.getEmail()));
+                
.andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
+                
.andExpect(xpath("//form[@id='form1']//input[@id='customEmailId' and 
@name='email']/@value").string(user.getEmail()))
+                .andExpect(xpath("//form[@id='form1']//input[@id='firstName' 
and @name='firstName']/@value").string(user.getFirstName()))
+                .andExpect(xpath("//form[@id='form1']//input[@id='lastName' 
and @name='lastName']/@value").string(user.getLastName()));
     }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ae94a3d4/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/input-directive-usages.ftlh
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/input-directive-usages.ftlh
 
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/input-directive-usages.ftlh
index 40beea0..614d2ae 100644
--- 
a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/input-directive-usages.ftlh
+++ 
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/form/input-directive-usages.ftlh
@@ -19,9 +19,30 @@
 <html>
 <body>
 
-  <div id="userEmail">
-    <@spring.form.input 'user.email' value='${user.email!}' />
-  </div>
+  <h1>Form 1</h1>
+  <hr/>
+  <form id="form1">
+    <table>
+      <tr>
+        <th>E-Mail:</th>
+        <td>
+          <@spring.form.input 'user.email' id="customEmailId" />
+        </td>
+      </tr>
+      <tr>
+        <th>First name:</th>
+        <td>
+          <@spring.form.input 'user.firstName' />
+        </td>
+      </tr>
+      <tr>
+        <th>Last name:</th>
+        <td>
+          <@spring.form.input 'user.lastName' />
+        </td>
+      </tr>
+    </table>
+  </form>
 
 </body>
 </html>

Reply via email to