FREEMARKER-55: unit test template code
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/96c038d9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/96c038d9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/96c038d9 Branch: refs/heads/3 Commit: 96c038d9ccc4a5d38cc8896370870d27af46af1f Parents: 2076c5e Author: Woonsan Ko <[email protected]> Authored: Mon Sep 11 23:21:50 2017 -0400 Committer: Woonsan Ko <[email protected]> Committed: Mon Sep 11 23:21:50 2017 -0400 ---------------------------------------------------------------------- .../spring/example/mvc/users/User.java | 80 ++++++++++++++ .../example/mvc/users/UserController.java | 66 ++++++++++++ .../example/mvc/users/UserRepository.java | 103 +++++++++++++++++++ .../spring/model/MessageFunctionTest.java | 61 +++++++++++ .../views/example/users/useredit.ftl | 88 ++++++++++++++++ .../views/example/users/userlist.ftl | 44 ++++++++ .../model/MessageFunctionTest-context.xml | 46 +++++++++ .../spring/model/UsersMessages.properties | 7 ++ 8 files changed, 495 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/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 new file mode 100644 index 0000000..a1550b2 --- /dev/null +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/User.java @@ -0,0 +1,80 @@ +/* + * 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.example.mvc.users; + +import java.util.Date; + +public class User { + + private final String id; + private String password; + private String email; + private String firstName; + private String lastName; + private Date birthDate; + + public User(final String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Date getBirthDate() { + return birthDate; + } + + public void setBirthDate(Date birthDate) { + this.birthDate = birthDate; + } +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/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 new file mode 100644 index 0000000..e8c0d86 --- /dev/null +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserController.java @@ -0,0 +1,66 @@ +/* + * 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.example.mvc.users; + +import java.util.LinkedList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class UserController { + + @Autowired + private UserRepository userRepository; + + @RequestMapping(value = "/users", method = RequestMethod.GET) + public String listUsers(Model model) { + List<User> users = new LinkedList<>(); + + for (String id : userRepository.getUserIds()) { + users.add(userRepository.getUser(id)); + } + + model.addAttribute("users", users); + + return "example/users/userlist"; + } + + @RequestMapping(value = "/users/{id}", method = RequestMethod.GET) + public String editUser(@PathVariable("id") String id, Model model) { + User user = userRepository.getUser(id); + model.addAttribute("user", user); + return "example/users/useredit"; + } + + public UserRepository getUserRepository() { + return userRepository; + } + + public void setUserRepository(UserRepository userRepository) { + this.userRepository = userRepository; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/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 new file mode 100644 index 0000000..3e53d03 --- /dev/null +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/example/mvc/users/UserRepository.java @@ -0,0 +1,103 @@ +/* + * 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.example.mvc.users; + +import java.util.Calendar; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; + +import org.springframework.stereotype.Repository; + +@Repository +public class UserRepository { + + private Map<String, User> usersMap = new ConcurrentHashMap<>(); + { + String id = "13c2ec8c-102c-4883-a282-3fe983e61515"; + User user = new User(id); + user.setEmail("[email protected]"); + user.setFirstName("John"); + user.setLastName("Doe"); + Calendar birthDate = Calendar.getInstance(); + birthDate.set(Calendar.YEAR, 1971); + birthDate.set(Calendar.MONTH, Calendar.JANUARY); + birthDate.set(Calendar.DATE, 5); + user.setBirthDate(birthDate.getTime()); + usersMap.put(id, user); + + id = "04d6080b-2098-4eaf-90ee-7331caab5e91"; + user = new User(id); + user.setEmail("[email protected]"); + user.setFirstName("Jane"); + user.setLastName("Doe"); + birthDate = Calendar.getInstance(); + birthDate.set(Calendar.YEAR, 1970); + birthDate.set(Calendar.MONTH, Calendar.FEBRUARY); + birthDate.set(Calendar.DATE, 7); + user.setBirthDate(birthDate.getTime()); + usersMap.put(id, user); + } + + public synchronized Set<String> getUserIds() { + return new TreeSet<>(usersMap.keySet()); + } + + public synchronized User getUser(final String id) { + if (id == null) { + throw new IllegalArgumentException("ID must be non-null."); + } + + User user = usersMap.get(id); + + if (user != null) { + return cloneUser(user, user.getId()); + } + + return null; + } + + public synchronized User addOrUpdateUser(final User user) { + final String id = user.getId(); + User newUser = cloneUser(user, id); + usersMap.put(id, newUser); + return cloneUser(newUser, id); + } + + public synchronized boolean deleteUser(final String username) { + if (username == null) { + throw new IllegalArgumentException("Username must be non-null."); + } + + final User user = usersMap.remove(username); + return user != null; + } + + private User cloneUser(final User source, final String id) { + User clone = new User(id); + clone.setPassword(source.getPassword()); + clone.setEmail(source.getEmail()); + clone.setFirstName(source.getFirstName()); + clone.setLastName(source.getLastName()); + clone.setBirthDate(source.getBirthDate()); + return clone; + } +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/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 new file mode 100644 index 0000000..03c32f4 --- /dev/null +++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/MessageFunctionTest.java @@ -0,0 +1,61 @@ +/* + * 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.get; +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.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("MessageFunctionTest-context.xml") +public class MessageFunctionTest { + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); + } + + @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")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/useredit.ftl ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/useredit.ftl b/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/useredit.ftl new file mode 100644 index 0000000..c206e9a --- /dev/null +++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/useredit.ftl @@ -0,0 +1,88 @@ +<#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> +<head> +<title>Spring MVC Form Example - User Edit Form</title> +</head> +<body> + +<h1>Editing User: ${spring.eval("user.firstName + ' ' + user.lastName")}</h1> + +<p>${spring.message("user.form.message", user.firstName, user.lastName, user.email)}</p> + +<form method="POST" action="${spring.url('/usereditaction.do', context='/othercontext', param1='value1', param2='value2')}"> + <table border="2"> + <tbody> + <tr> + <th>${spring.message("user.id")!}</th> + <td>${user.id}</td> + </tr> + <tr> + <th>${spring.message("user.password")!}</th> + <td> + <@spring.bind "user.password"; status> + <input type="password" name="password" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <th>${spring.message("user.email")!}</th> + <td> + <@spring.bind "user.email"; status> + <input type="text" name="email" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <th>${spring.message("user.firstName")!}</th> + <td> + <@spring.bind "user.firstName"; status> + <input type="text" name="firstName" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <th>${spring.message("user.lastName")!}</th> + <td> + <@spring.bind "user.lastName"; status> + <input type="text" name="lastName" value="${status.value!}" /> + </@spring.bind> + </td> + </tr> + <tr> + <th>${spring.message("user.birthDate")!}</th> + <td> + <@spring.bind "user.birthDate"; status> + ${spring.transform(status.editor, status.actualValue)!} + </@spring.bind> + </td> + </tr> + <tr> + <td colspan="2"> + <input type="submit" value="${spring.message('user.form.submit')!'Save'}" /> + <input type="reset" value="${spring.message('user.form.submit')!'Reset'}" /> + </td> + </tr> + </tbody> + </table> +</form> + +</body> +</html> http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/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 new file mode 100644 index 0000000..25c757f --- /dev/null +++ b/freemarker-spring/src/test/resources/META-INF/web-resources/views/example/users/userlist.ftl @@ -0,0 +1,44 @@ +<#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> +<head> +<title>Spring MVC Form Example - Users</title> +</head> + +<h1>Users</h1> + +<table border="2"> + <thead> + <tr> + <th>Name</th> + <th>E-Mail</th> + </tr> + </thead> + <tbody> + <#list users as user> + <tr> + <td><a href="users/${user.id}">${user.firstName} ${user.lastName}</a></td> + <td><a href="mailto:${user.email}">${user.email}</a></td> + </tr> + </#list> + </tbody> +</table> + +</html> http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/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 new file mode 100644 index 0000000..f68f584 --- /dev/null +++ b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/MessageFunctionTest-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.model.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> http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/96c038d9/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/UsersMessages.properties ---------------------------------------------------------------------- diff --git a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/UsersMessages.properties b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/UsersMessages.properties new file mode 100644 index 0000000..ade76f4 --- /dev/null +++ b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/model/UsersMessages.properties @@ -0,0 +1,7 @@ +user.form.message=Edit info for {0} {1} <{2}> +user.id=ID +user.password=Password +user.email=E-Mail +user.firstName=First name +user.lastName=Last name +user.birthDate=Birth Date
