FREEMARKER-55: Adding unit test for BindErrorsDirective
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/74a300f1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/74a300f1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/74a300f1 Branch: refs/heads/3 Commit: 74a300f144ce99d03e021416f4f6ce7d191024af Parents: 86a8a5f Author: Woonsan Ko <[email protected]> Authored: Tue Sep 12 17:30:02 2017 -0400 Committer: Woonsan Ko <[email protected]> Committed: Tue Sep 12 17:30:02 2017 -0400 ---------------------------------------------------------------------- .../spring/model/BindErrorsDirective.java | 10 ++- .../spring/example/mvc/users/User.java | 5 +- .../example/mvc/users/UserController.java | 15 ++++ .../spring/model/BindDirectiveTest.java | 2 +- .../spring/model/BindErrorsDirectiveTest.java | 83 ++++++++++++++++++++ .../spring/model/EvalFunctionTest.java | 2 +- .../spring/model/MessageFunctionTest.java | 4 +- .../spring/model/NestedPathDirectiveTest.java | 2 +- .../spring/model/ThemeFunctionTest.java | 2 +- .../spring/model/UrlFunctionTest.java | 2 +- .../model/binderrors-directive-basic-usages.ftl | 70 +++++++++++++++++ .../example/mvc/users/UsersMessages.properties | 1 + 12 files changed, 188 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindErrorsDirective.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindErrorsDirective.java b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindErrorsDirective.java index e35b6ee..6d0c954 100644 --- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindErrorsDirective.java +++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindErrorsDirective.java @@ -49,8 +49,14 @@ import org.springframework.web.servlet.support.RequestContext; * Some valid example(s): * </P> * <PRE> - * <@spring.hasBindErrors "email"; errors> - * <#-- nested content with using errors --> + * <@spring.hasBindErrors "user"; errors> + * <div class="errors"> + * <#list errors.allErrors as error> + * <div class="error"> + * ${spring.message(message=error)!} + * </div> + * </#list> + * </div> * </@spring.hasBindErrors> * </PRE> * <P> http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/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 1aa0515..e349a48 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 @@ -23,13 +23,16 @@ import java.util.Date; public class User { - private final Integer id; + private Integer id; private String password; private String email; private String firstName; private String lastName; private Date birthDate; + public User() { + } + public User(final Integer id) { this.id = id; } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/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 80a158d..43b06a4 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 @@ -27,6 +27,8 @@ 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.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -70,6 +72,19 @@ public class UserController { return (StringUtils.hasText(viewName)) ? viewName : DEFAULT_USER_EDIT_VIEW_NAME; } + @RequestMapping(value = "/users/", method = RequestMethod.POST) + public String createUser(@RequestParam(value = "viewName", required = false) String viewName, User user, + BindingResult bindingResult, Model model) { + model.addAttribute("user", user); + + if (!StringUtils.hasText(user.getEmail())) { + bindingResult.addError(new FieldError("user", "email", user.getEmail(), true, + new String[] { "user.error.invalid.email" }, new Object[] { user.getEmail() }, "E-Mail is blank.")); + } + + return (StringUtils.hasText(viewName)) ? viewName : DEFAULT_USER_EDIT_VIEW_NAME; + } + public UserRepository getUserRepository() { return userRepository; } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindDirectiveTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindDirectiveTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindDirectiveTest.java index cd1af42..b2a6cd5 100644 --- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindDirectiveTest.java +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindDirectiveTest.java @@ -58,7 +58,7 @@ public class BindDirectiveTest { } @Test - public void testMessageFunctionBasicUsages() throws Exception { + public void testBasicUsages() throws Exception { final Integer userId = userRepository.getUserIds().iterator().next(); final User user = userRepository.getUser(userId); mockMvc.perform(get("/users/{userId}.", userId).param("viewName", "test/model/bind-directive-basic-usages") http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindErrorsDirectiveTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindErrorsDirectiveTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindErrorsDirectiveTest.java new file mode 100644 index 0000000..15520cb --- /dev/null +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/BindErrorsDirectiveTest.java @@ -0,0 +1,83 @@ +/* + * 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.hamcrest.Matchers.equalToIgnoringWhiteSpace; +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.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; + +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.context.MessageSource; +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 BindErrorsDirectiveTest { + + @Autowired + private WebApplicationContext wac; + + @Autowired + private UserRepository userRepository; + + @Autowired + private MessageSource messageSource; + + private MockMvc mockMvc; + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); + } + + @Test + public void testBasicUsages() throws Exception { + final User user = new User(); + user.setFirstName("Paul"); + user.setLastName("Temple"); + // set invalid email intentionally to test BindErrorsDirective... + user.setEmail(""); + + mockMvc.perform(post("/users/").param("viewName", "test/model/binderrors-directive-basic-usages") + .param("firstName", user.getFirstName()).param("lastName", user.getLastName()) + .param("email", user.getEmail()).accept(MediaType.parseMediaType("text/html"))) + .andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print()) + .andExpect(xpath("//div[@class='error']").string(equalToIgnoringWhiteSpace( + messageSource.getMessage("user.error.invalid.email", new Object[] { user.getEmail() }, null)))) + .andExpect(xpath("//input[@name='firstName']/@value").string(user.getFirstName())) + .andExpect(xpath("//input[@name='lastName']/@value").string(user.getLastName())); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/EvalFunctionTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/EvalFunctionTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/EvalFunctionTest.java index be6f6dd..45d98b2 100644 --- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/EvalFunctionTest.java +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/EvalFunctionTest.java @@ -59,7 +59,7 @@ public class EvalFunctionTest { } @Test - public void testMessageFunctionBasicUsages() throws Exception { + public void testBasicUsages() throws Exception { final Integer userId = userRepository.getUserIds().iterator().next(); final User user = userRepository.getUser(userId); mockMvc.perform(get("/users/").param("viewName", "test/model/eval-function-basic-usages") http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/MessageFunctionTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/MessageFunctionTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/MessageFunctionTest.java index 0ddbe27..d043bfe 100644 --- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/MessageFunctionTest.java +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/MessageFunctionTest.java @@ -58,7 +58,7 @@ public class MessageFunctionTest { } @Test - public void testMessageFunctionBasicUsages() throws Exception { + public void testBasicUsages() throws Exception { final Integer userId = userRepository.getUserIds().iterator().next(); final User user = userRepository.getUser(userId); mockMvc.perform(get("/users/{userId}/", userId).param("viewName", "test/model/message-function-basic-usages") @@ -71,7 +71,7 @@ public class MessageFunctionTest { } @Test - public void testMessageFunctionWithMessageSourceResolvable() throws Exception { + public void testWithMessageSourceResolvable() throws Exception { final Integer nonExistingUserId = 0; mockMvc.perform( get("/users/{userId}/", nonExistingUserId).param("viewName", "test/model/message-function-basic-usages") http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/NestedPathDirectiveTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/NestedPathDirectiveTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/NestedPathDirectiveTest.java index d02478f..a4999f5 100644 --- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/NestedPathDirectiveTest.java +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/NestedPathDirectiveTest.java @@ -58,7 +58,7 @@ public class NestedPathDirectiveTest { } @Test - public void testMessageFunctionBasicUsages() throws Exception { + public void testBasicUsages() throws Exception { final Integer userId = userRepository.getUserIds().iterator().next(); final User user = userRepository.getUser(userId); mockMvc.perform(get("/users/{userId}.", userId).param("viewName", "test/model/nestedpath-directive-basic-usages") http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/ThemeFunctionTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/ThemeFunctionTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/ThemeFunctionTest.java index 81b186b..804b76d 100644 --- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/ThemeFunctionTest.java +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/ThemeFunctionTest.java @@ -62,7 +62,7 @@ public class ThemeFunctionTest { } @Test - public void testThemeFunctionBasicUsages() throws Exception { + public void testBasicUsages() throws Exception { final MessageSource defaultThemeMessageSource = themeSource.getTheme("default").getMessageSource(); final Integer userId = userRepository.getUserIds().iterator().next(); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/UrlFunctionTest.java ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/UrlFunctionTest.java b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/UrlFunctionTest.java index c2e1ce3..8435bfc 100644 --- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/UrlFunctionTest.java +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/UrlFunctionTest.java @@ -57,7 +57,7 @@ public class UrlFunctionTest { } @Test - public void testThemeFunctionBasicUsages() throws Exception { + public void testBasicUsages() throws Exception { final Integer userId = userRepository.getUserIds().iterator().next(); mockMvc.perform(get("/users/").param("viewName", "test/model/url-function-basic-usages") .accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk()) http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/binderrors-directive-basic-usages.ftl ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/binderrors-directive-basic-usages.ftl b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/binderrors-directive-basic-usages.ftl new file mode 100644 index 0000000..d608615 --- /dev/null +++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/binderrors-directive-basic-usages.ftl @@ -0,0 +1,70 @@ +<#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.hasBindErrors "user"; errors> + <div class="errors"> + <#list errors.allErrors as error> + <div class="error"> + ${spring.message(message=error)!} + </div> + </#list> + </div> +</@spring.hasBindErrors> + +<form method="POST" action="${spring.url('/users')}"> + <table class="table"> + <tbody> + <tr> + <th>E-Mail</th> + <td> + <@spring.bind "user.email"; status> + <input type="text" name="email" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <th>First Name</th> + <td> + <@spring.bind "user.firstName"; status> + <input type="text" name="firstName" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <th>Last Name</th> + <td> + <@spring.bind "user.lastName"; status> + <input type="text" name="lastName" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <td colspan="2"> + <input type="submit" name="save" value="Save" /> + </td> + </tr> + </tbody> + </table> +</form> + +</body> +</html> http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/74a300f1/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersMessages.properties ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersMessages.properties b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersMessages.properties index 497607c..aa12742 100644 --- a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersMessages.properties +++ b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersMessages.properties @@ -6,3 +6,4 @@ user.firstName=First name user.lastName=Last name user.birthDate=Birth Date user.error.notfound=User not found by ID: {0} +user.error.invalid.email=Invalid E-Mail address: {0}
