You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2017/09/13 09:49:34 UTC
[33/36] incubator-freemarker git commit: FREEMARKER-55: Adding unit
test for transform function
FREEMARKER-55: Adding unit test for transform function
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/bd564327
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/bd564327
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/bd564327
Branch: refs/heads/3
Commit: bd564327db67c19a6a39b092a1be806ed4b7af08
Parents: 74a300f
Author: Woonsan Ko <wo...@apache.org>
Authored: Tue Sep 12 21:11:31 2017 -0400
Committer: Woonsan Ko <wo...@apache.org>
Committed: Tue Sep 12 21:11:31 2017 -0400
----------------------------------------------------------------------
.../spring/model/TransformFunction.java | 2 +-
.../spring/example/mvc/users/User.java | 6 ++
.../example/mvc/users/UserController.java | 18 ++++-
.../spring/model/TransformFunctionTest.java | 76 ++++++++++++++++++++
.../model/transform-function-basic-usages.ftl | 28 ++++++++
5 files changed, 127 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/bd564327/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/TransformFunction.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/TransformFunction.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/TransformFunction.java
index ee3cc1c..d6c381e 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/TransformFunction.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/TransformFunction.java
@@ -46,7 +46,7 @@ import org.springframework.web.servlet.support.RequestContext;
* </P>
* <PRE>
* <@spring.bind "user"; status>
- * ${spring.transform(status, user.birthDate)}
+ * ${spring.transform(status.editor, user.birthDate)}
* </@spring.bind>
* </PRE>
* <P>
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/bd564327/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/User.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/User.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/User.java
index e349a48..23d31dc 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/User.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/User.java
@@ -80,4 +80,10 @@ public class User {
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
+
+ @Override
+ public String toString() {
+ return super.toString() + " {id=" + id + ", firstName='" + firstName + "', lastName='" + lastName + "', email='"
+ + email + "', birthDate='" + birthDate + "'}";
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/bd564327/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserController.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserController.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserController.java
index 43b06a4..09a416d 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserController.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserController.java
@@ -19,16 +19,22 @@
package org.apache.freemarker.spring.example.mvc.users;
+import java.text.SimpleDateFormat;
+import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -44,6 +50,12 @@ public class UserController {
@Autowired
private UserRepository userRepository;
+ @InitBinder("user")
+ public void customizeBinding(WebDataBinder binder) {
+ binder.registerCustomEditor(Date.class, "birthDate",
+ new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
+ }
+
@RequestMapping(value = "/users/", method = RequestMethod.GET)
public String listUsers(@RequestParam(value = "viewName", required = false) String viewName, Model model) {
List<User> users = new LinkedList<>();
@@ -73,8 +85,8 @@ public class UserController {
}
@RequestMapping(value = "/users/", method = RequestMethod.POST)
- public String createUser(@RequestParam(value = "viewName", required = false) String viewName, User user,
- BindingResult bindingResult, Model model) {
+ public String createUser(@RequestParam(value = "viewName", required = false) String viewName,
+ @ModelAttribute("user") User user, BindingResult bindingResult, Model model) {
model.addAttribute("user", user);
if (!StringUtils.hasText(user.getEmail())) {
@@ -82,6 +94,8 @@ public class UserController {
new String[] { "user.error.invalid.email" }, new Object[] { user.getEmail() }, "E-Mail is blank."));
}
+ // No saving for now...
+
return (StringUtils.hasText(viewName)) ? viewName : DEFAULT_USER_EDIT_VIEW_NAME;
}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/bd564327/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/TransformFunctionTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/TransformFunctionTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/TransformFunctionTest.java
new file mode 100644
index 0000000..d3fd680
--- /dev/null
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/TransformFunctionTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.freemarker.spring.model;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+
+import org.apache.freemarker.spring.example.mvc.users.User;
+import org.apache.freemarker.spring.example.mvc.users.UserRepository;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.context.WebApplicationContext;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@WebAppConfiguration("classpath:META-INF/web-resources")
+@ContextConfiguration(locations = { "classpath:org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml" })
+public class TransformFunctionTest {
+
+ @Autowired
+ private WebApplicationContext wac;
+
+ @Autowired
+ private UserRepository userRepository;
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setUp() {
+ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
+ }
+
+ @Test
+ public void testBasicUsages() throws Exception {
+ final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ final User user = new User();
+ user.setFirstName("Paul");
+ user.setLastName("Temple");
+ user.setBirthDate(dateFormat.parse("1980-12-23"));
+ user.setEmail("paul.temple@example.com");
+
+ mockMvc.perform(post("/users/").param("viewName", "test/model/transform-function-basic-usages")
+ .param("firstName", user.getFirstName()).param("lastName", user.getLastName())
+ .param("email", user.getEmail()).param("birthDate", dateFormat.format(user.getBirthDate()))
+ .accept(MediaType.parseMediaType("text/html"))).andDo(print())
+ .andExpect(xpath("//div[@id='userBirthDate']/text()").string(dateFormat.format(user.getBirthDate())));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/bd564327/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/transform-function-basic-usages.ftl
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/transform-function-basic-usages.ftl b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/transform-function-basic-usages.ftl
new file mode 100644
index 0000000..0fb9e29
--- /dev/null
+++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/transform-function-basic-usages.ftl
@@ -0,0 +1,28 @@
+<#ftl outputFormat="HTML">
+<#--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<html>
+<body>
+
+<@spring.bind "user.birthDate"; status>
+ <div id="userBirthDate">${spring.transform(status.editor, status.actualValue)}</div>
+</...@spring.bind>
+
+</body>
+</html>
\ No newline at end of file