freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [24/36] incubator-freemarker git commit: FREEMARKER-55: Adding unit test for message function.
Date Wed, 13 Sep 2017 09:49:25 GMT
FREEMARKER-55: Adding unit test for message 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/77b7f30c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/77b7f30c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/77b7f30c

Branch: refs/heads/3
Commit: 77b7f30c5f61158c138f42e0f61c3b47b8df6d2d
Parents: d5c31a1
Author: Woonsan Ko <woonsan@apache.org>
Authored: Tue Sep 12 10:58:28 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Tue Sep 12 10:58:28 2017 -0400

----------------------------------------------------------------------
 .../spring/model/MessageFunction.java           |  2 +-
 .../spring/example/mvc/users/User.java          |  6 +--
 .../example/mvc/users/UserController.java       | 31 +++++++++----
 .../example/mvc/users/UserRepository.java       | 14 +++---
 .../spring/model/MessageFunctionTest.java       | 34 ++++++++++++---
 .../views/example/users/userlist.ftl            |  2 +
 .../model/message-function-basic-usages.ftl     | 32 ++++++++++++++
 .../example/mvc/users/UsersMessages.properties  |  1 +
 .../example/mvc/users/users-mvc-context.xml     | 46 ++++++++++++++++++++
 .../model/MessageFunctionTest-context.xml       | 46 --------------------
 10 files changed, 143 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
index 94a1e0f..e64aba5 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/MessageFunction.java
@@ -65,7 +65,7 @@ import org.springframework.web.servlet.support.RequestContext;
  * &lt;#-- With 'code' positional parameter and message arguments (varargs) --&gt;
  * ${spring.message("message.user.form", user.firstName, user.lastName, user.email)}
  *
- * &lt;#-- With 'message' named parameter (<code>MessageResolvable</code>
object) --&gt;
+ * &lt;#-- With 'message' named parameter (assuming a <code>MessageResolvable</code>
object is set to a model attribute) --&gt;
  * ${spring.message(message=myMessageResolvable)}
  * </PRE>
  * <P>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/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 a1550b2..1aa0515 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,18 +23,18 @@ import java.util.Date;
 
 public class User {
 
-    private final String id;
+    private final Integer id;
     private String password;
     private String email;
     private String firstName;
     private String lastName;
     private Date birthDate;
 
-    public User(final String id) {
+    public User(final Integer id) {
         this.id = id;
     }
 
-    public String getId() {
+    public Integer getId() {
         return id;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/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 e8c0d86..80a158d 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
@@ -23,36 +23,51 @@ import java.util.LinkedList;
 import java.util.List;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.support.DefaultMessageSourceResolvable;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 
 @Controller
 public class UserController {
 
+    private static final String DEFAULT_USER_LIST_VIEW_NAME = "example/users/userlist";
+
+    private static final String DEFAULT_USER_EDIT_VIEW_NAME = "example/users/useredit";
+
     @Autowired
     private UserRepository userRepository;
 
-    @RequestMapping(value = "/users", method = RequestMethod.GET)
-    public String listUsers(Model model) {
+    @RequestMapping(value = "/users/", method = RequestMethod.GET)
+    public String listUsers(@RequestParam(value = "viewName", required = false) String viewName,
Model model) {
         List<User> users = new LinkedList<>();
 
-        for (String id : userRepository.getUserIds()) {
+        for (Integer id : userRepository.getUserIds()) {
             users.add(userRepository.getUser(id));
         }
 
         model.addAttribute("users", users);
 
-        return "example/users/userlist";
+        return (StringUtils.hasText(viewName)) ? viewName : DEFAULT_USER_LIST_VIEW_NAME;
     }
 
-    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
-    public String editUser(@PathVariable("id") String id, Model model) {
+    @RequestMapping(value = "/users/{id:\\d+}", method = RequestMethod.GET)
+    public String getUser(@PathVariable("id") Integer id,
+            @RequestParam(value = "viewName", required = false) String viewName, Model model)
{
         User user = userRepository.getUser(id);
-        model.addAttribute("user", user);
-        return "example/users/useredit";
+
+        if (user != null) {
+            model.addAttribute("user", user);
+        } else {
+            model.addAttribute("errorMessage",
+                    new DefaultMessageSourceResolvable(new String[] { "user.error.notfound"
}, new Object[] { id }));
+        }
+
+        return (StringUtils.hasText(viewName)) ? viewName : DEFAULT_USER_EDIT_VIEW_NAME;
     }
 
     public UserRepository getUserRepository() {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserRepository.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserRepository.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserRepository.java
index 3e53d03..5049521 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserRepository.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserRepository.java
@@ -30,9 +30,9 @@ import org.springframework.stereotype.Repository;
 @Repository
 public class UserRepository {
 
-    private Map<String, User> usersMap = new ConcurrentHashMap<>();
+    private Map<Integer, User> usersMap = new ConcurrentHashMap<>();
     {
-        String id = "13c2ec8c-102c-4883-a282-3fe983e61515";
+        Integer id = 101;
         User user = new User(id);
         user.setEmail("john@example.com");
         user.setFirstName("John");
@@ -44,7 +44,7 @@ public class UserRepository {
         user.setBirthDate(birthDate.getTime());
         usersMap.put(id, user);
 
-        id = "04d6080b-2098-4eaf-90ee-7331caab5e91";
+        id = 102;
         user = new User(id);
         user.setEmail("jane@example.com");
         user.setFirstName("Jane");
@@ -57,11 +57,11 @@ public class UserRepository {
         usersMap.put(id, user);
     }
 
-    public synchronized Set<String> getUserIds() {
+    public synchronized Set<Integer> getUserIds() {
         return new TreeSet<>(usersMap.keySet());
     }
 
-    public synchronized User getUser(final String id) {
+    public synchronized User getUser(final Integer id) {
         if (id == null) {
             throw new IllegalArgumentException("ID must be non-null.");
         }
@@ -76,7 +76,7 @@ public class UserRepository {
     }
 
     public synchronized User addOrUpdateUser(final User user) {
-        final String id = user.getId();
+        final Integer id = user.getId();
         User newUser = cloneUser(user, id);
         usersMap.put(id, newUser);
         return cloneUser(newUser, id);
@@ -91,7 +91,7 @@ public class UserRepository {
         return user != null;
     }
 
-    private User cloneUser(final User source, final String id) {
+    private User cloneUser(final User source, final Integer id) {
         User clone = new User(id);
         clone.setPassword(source.getPassword());
         clone.setEmail(source.getEmail());

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/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 03c32f4..803e3de 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
@@ -20,10 +20,13 @@
 package org.apache.freemarker.spring.model;
 
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+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;
@@ -38,12 +41,15 @@ import org.springframework.web.context.WebApplicationContext;
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @WebAppConfiguration("classpath:META-INF/web-resources")
-@ContextConfiguration("MessageFunctionTest-context.xml")
+@ContextConfiguration(locations = { "classpath:org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml"
})
 public class MessageFunctionTest {
 
     @Autowired
     private WebApplicationContext wac;
 
+    @Autowired
+    private UserRepository userRepository;
+
     private MockMvc mockMvc;
 
     @Before
@@ -52,10 +58,26 @@ public class MessageFunctionTest {
     }
 
     @Test
-    public void getUsers() throws Exception {
-        mockMvc.perform(get("/users").accept(MediaType.parseMediaType("text/html")))
-            .andExpect(status().isOk())
-            .andExpect(content().contentTypeCompatibleWith("text/html"))
-            .andExpect(xpath("/html/head/title").string("Spring MVC Form Example - Users"));
+    public void getMessageFunctionBasicUsages() 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")
+                .accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk())
+                .andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
+                .andExpect(xpath("//div[@id='userId']/text()").string(wac.getMessage("user.id",
null, null)))
+                .andExpect(xpath("//div[@id='userEmail']/text()").string(wac.getMessage("user.email",
null, null)))
+                .andExpect(xpath("//div[@id='userInfoWithArgs']/text()").string(wac.getMessage("user.form.message",
+                        new Object[] { user.getFirstName(), user.getLastName(), user.getEmail()
}, null)));
+    }
+
+    @Test
+    public void getMessageFunctionWithMessageSourceResolvable() throws Exception {
+        final Integer nonExistingUserId = 0;
+        mockMvc.perform(
+                get("/users/{userId}/", nonExistingUserId).param("viewName", "test/model/message-function-basic-usages")
+                        .accept(MediaType.parseMediaType("text/html")))
+                .andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
+                .andExpect(xpath("//div[@id='errorMessage']/text()")
+                        .string(wac.getMessage("user.error.notfound", new Object[] { nonExistingUserId
}, null)));
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/userlist.ftl
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/userlist.ftl
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/userlist.ftl
index 25c757f..aef5c91 100644
--- a/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/userlist.ftl
+++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/userlist.ftl
@@ -21,6 +21,7 @@
 <head>
 <title>Spring MVC Form Example - Users</title>
 </head>
+<body>
 
 <h1>Users</h1>
 
@@ -41,4 +42,5 @@
   </tbody>
 </table>
 
+</body>
 </html>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/message-function-basic-usages.ftl
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/message-function-basic-usages.ftl
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/message-function-basic-usages.ftl
new file mode 100644
index 0000000..fe1e65e
--- /dev/null
+++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/message-function-basic-usages.ftl
@@ -0,0 +1,32 @@
+<#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>
+
+<#if user??>
+  <div id="userId">${spring.message("user.id")!}</div>
+  <div id="userEmail">${spring.message("user.email")!}</div>
+  <div id="userInfoWithArgs">${spring.message("user.form.message", user.firstName,
user.lastName, user.email)!}</div>
+<#else>
+  <div id="errorMessage">${spring.message(message=errorMessage)!}</div>
+</#if>
+
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/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 ade76f4..497607c 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
@@ -5,3 +5,4 @@ user.email=E-Mail
 user.firstName=First name
 user.lastName=Last name
 user.birthDate=Birth Date
+user.error.notfound=User not found by ID: {0}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml
b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml
new file mode 100644
index 0000000..0dbc950
--- /dev/null
+++ b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/users-mvc-context.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+   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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:mvc="http://www.springframework.org/schema/mvc"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
+
+  <context:component-scan base-package="org.apache.freemarker.spring.example.mvc.users"
/>
+
+  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
+    <property name="basename" value="classpath:org/apache/freemarker/spring/example/mvc/users/UsersMessages"
/>
+  </bean>
+
+  <bean id="configuration" class="org.apache.freemarker.spring.ConfigurationFactoryBean">
+    <property name="localizedTemplateLookup" value="false" />
+    <property name="templateLoader">
+      <bean class="org.apache.freemarker.spring.SpringResourceTemplateLoader">
+      </bean>
+    </property>
+  </bean>
+
+  <bean id="viewResolver" class="org.apache.freemarker.spring.web.view.FreeMarkerViewResolver">
+    <property name="configuration" ref="configuration" />
+    <property name="prefix" value="/views/" />
+    <property name="suffix" value=".ftl" />
+  </bean>
+
+</beans>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/77b7f30c/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/MessageFunctionTest-context.xml
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/MessageFunctionTest-context.xml
b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/MessageFunctionTest-context.xml
deleted file mode 100644
index 862de44..0000000
--- a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/MessageFunctionTest-context.xml
+++ /dev/null
@@ -1,46 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<!--
-   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.
--->
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xmlns:context="http://www.springframework.org/schema/context"
-       xmlns:mvc="http://www.springframework.org/schema/mvc"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
-                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-  <context:component-scan base-package="org.apache.freemarker.spring.example.mvc.users"
/>
-
-  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
-    <property name="basename" value="classpath:org.apache.freemarker.spring.example.mvc.users.UserMessages"
/>
-  </bean>
-
-  <bean id="configuration" class="org.apache.freemarker.spring.ConfigurationFactoryBean">
-    <property name="localizedTemplateLookup" value="false" />
-    <property name="templateLoader">
-      <bean class="org.apache.freemarker.spring.SpringResourceTemplateLoader">
-      </bean>
-    </property>
-  </bean>
-
-  <bean id="viewResolver" class="org.apache.freemarker.spring.web.view.FreeMarkerViewResolver">
-    <property name="configuration" ref="configuration" />
-    <property name="prefix" value="/views/" />
-    <property name="suffix" value=".ftl" />
-  </bean>
-
-</beans>


Mime
View raw message