FREEMARKER-55: Adding unit test for theme 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/a274d2a6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/a274d2a6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/a274d2a6

Branch: refs/heads/3
Commit: a274d2a6149b432656c5aa61eedd5b6213168378
Parents: 77b7f30
Author: Woonsan Ko <woon...@apache.org>
Authored: Tue Sep 12 11:22:15 2017 -0400
Committer: Woonsan Ko <woon...@apache.org>
Committed: Tue Sep 12 11:22:15 2017 -0400

----------------------------------------------------------------------
 .../freemarker/spring/model/ThemeFunction.java  |  8 +--
 .../spring/model/MessageFunctionTest.java       |  4 +-
 .../spring/model/ThemeFunctionTest.java         | 72 ++++++++++++++++++++
 .../test/model/theme-function-basic-usages.ftl  | 26 +++++++
 .../mvc/users/UsersTheme-default.properties     |  1 +
 .../example/mvc/users/users-mvc-context.xml     |  8 +++
 6 files changed, 111 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a274d2a6/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/ThemeFunction.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/ThemeFunction.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/ThemeFunction.java
index 15a4103..b03055e 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/ThemeFunction.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/ThemeFunction.java
@@ -45,13 +45,9 @@ import 
org.springframework.web.servlet.support.RequestContext;
  * </P>
  * <PRE>
  * &lt;#-- With 'code' positional parameter only --&gt;
- * ${spring.theme("label.user.firstName")!}
+ * ${spring.theme("styleSheet")!}
  *
- * &lt;#-- With 'code' positional parameter and message arguments (varargs) 
--&gt;
- * ${spring.theme("message.user.form", user.firstName, user.lastName, 
user.email)}
- *
- * &lt;#-- With 'message' named parameter (<code>MessageResolvable</code> 
object) --&gt;
- * ${spring.theme(message=myMessageResolvable)}
+ * &lt;link rel="stylesheet" href="${spring.theme('styleSheet')}" 
type="text/css" /&gt;
  * </PRE>
  * <P>
  * <EM>Note:</EM> Unlike Spring Framework's <code>&lt;spring:theme 
/&gt;</code> JSP Tag Library, this function

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a274d2a6/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 803e3de..5be6daf 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 getMessageFunctionBasicUsages() throws Exception {
+    public void testMessageFunctionBasicUsages() 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 getMessageFunctionWithMessageSourceResolvable() throws 
Exception {
+    public void testMessageFunctionWithMessageSourceResolvable() 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/a274d2a6/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
new file mode 100644
index 0000000..637ca5f
--- /dev/null
+++ 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/ThemeFunctionTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.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.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.ui.context.ThemeSource;
+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 ThemeFunctionTest {
+
+    @Autowired
+    private WebApplicationContext wac;
+
+    @Autowired
+    private UserRepository userRepository;
+
+    @Autowired
+    private ThemeSource themeSource;
+
+    private MockMvc mockMvc;
+
+    @Before
+    public void setup() {
+        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
+    }
+
+    @Test
+    public void testThemeFunctionBasicUsages() throws Exception {
+        final Integer userId = userRepository.getUserIds().iterator().next();
+        mockMvc.perform(get("/users/{userId}/", userId).param("viewName", 
"test/model/theme-function-basic-usages")
+                
.accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk())
+                
.andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
+                .andExpect(xpath("//link[@rel='stylesheet']/@href").string(
+                        
themeSource.getTheme("default").getMessageSource().getMessage("styleSheet", 
null, null)));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a274d2a6/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/theme-function-basic-usages.ftl
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/theme-function-basic-usages.ftl
 
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/theme-function-basic-usages.ftl
new file mode 100644
index 0000000..bc50875
--- /dev/null
+++ 
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/theme-function-basic-usages.ftl
@@ -0,0 +1,26 @@
+<#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>
+<link rel="stylesheet" href="${spring.theme('styleSheet')}" type="text/css" />
+</head>
+<body>
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a274d2a6/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersTheme-default.properties
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersTheme-default.properties
 
b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersTheme-default.properties
new file mode 100644
index 0000000..1d7bcc2
--- /dev/null
+++ 
b/freemarker-spring/src/test/resources/org/apache/freemarker/spring/example/mvc/users/UsersTheme-default.properties
@@ -0,0 +1 @@
+styleSheet=/themes/style/default.css

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a274d2a6/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
index 0dbc950..f5fcf24 100644
--- 
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
@@ -43,4 +43,12 @@
     <property name="suffix" value=".ftl" />
   </bean>
 
+  <bean id="themeSource" 
class="org.springframework.ui.context.support.ResourceBundleThemeSource">
+    <property name="basenamePrefix" 
value="org/apache/freemarker/spring/example/mvc/users/UsersTheme-" />
+  </bean>
+
+  <bean id="themeResolver" 
class="org.springframework.web.servlet.theme.CookieThemeResolver">
+    <property name="defaultThemeName" value="default" />
+  </bean>
+
 </beans>

Reply via email to