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

Branch: refs/heads/3
Commit: d7916709f9897c4ff8c6ae146ee191df77a43427
Parents: 9987acf
Author: Woonsan Ko <woon...@apache.org>
Authored: Tue Sep 12 13:41:02 2017 -0400
Committer: Woonsan Ko <woon...@apache.org>
Committed: Tue Sep 12 13:41:02 2017 -0400

----------------------------------------------------------------------
 .../freemarker/spring/model/EvalFunction.java   | 19 ++++-
 .../spring/model/EvalFunctionTest.java          | 80 ++++++++++++++++++++
 .../spring/model/MessageFunctionTest.java       |  2 +-
 .../spring/model/ThemeFunctionTest.java         |  2 +-
 .../test/model/eval-function-basic-usages.ftl   | 46 +++++++++++
 5 files changed, 146 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d7916709/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
index 6ae0bb5..f155df1 100644
--- 
a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
+++ 
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/EvalFunction.java
@@ -51,9 +51,26 @@ import 
org.springframework.web.servlet.support.RequestContext;
  * Some valid example(s):
  * </P>
  * <PRE>
+ * &lt;#assign expression="T(java.lang.Math).max(12.34, 56.78)" /&gt;
+ * Max number: ${spring.eval(expression)}
+ * 
+ * &lt;#assign expression="user.id" /&gt;
+ * User ID: ${spring.eval(expression)!}
+ * 
+ * User ID: ${spring.eval('user.id')!}
+ * 
+ * &lt;#assign expression="user.firstName + ' ' + user.lastName" /&gt;
+ * User Name: ${spring.eval(expression)!}
+ * 
+ * &lt;#assign expression="users[0].id" /&gt;
+ * First User's ID: ${spring.eval(expression)!}
+ *
+ * &lt;#assign expression="{0,1,1,2,3,5,8,13}" /&gt;
+ * &lt;#assign numbers=spring.eval(expression) /&gt;
+ * Numbers: &lt;#list numbers as number&gt;${number}&lt;#sep&gt;, 
&lt;/#list&gt;
  * </PRE>
  * <P>
- * <EM>Note:</EM> Unlike Spring Framework's <code>&lt;spring:message 
/&gt;</code> JSP Tag Library, this function
+ * <EM>Note:</EM> Unlike Spring Framework's <code>&lt;spring:eval /&gt;</code> 
JSP Tag Library, this function
  * does not support <code>htmlEscape</code> parameter. It always returns the 
message not to escape HTML's
  * because it is much easier to control escaping in FreeMarker Template 
expressions.
  * </P>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d7916709/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
new file mode 100644
index 0000000..953b571
--- /dev/null
+++ 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/model/EvalFunctionTest.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.model;
+
+import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
+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;
+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 EvalFunctionTest {
+
+    @Autowired
+    private WebApplicationContext wac;
+
+    @Autowired
+    private UserRepository userRepository;
+
+    private MockMvc mockMvc;
+
+    private long startTimeMillis;
+
+    @Before
+    public void setUp() {
+        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
+
+        startTimeMillis = System.currentTimeMillis();
+        System.setProperty("EvalFunctionTest.startTimeMillis", 
Long.toString(startTimeMillis));
+    }
+
+    @Test
+    public void testMessageFunctionBasicUsages() 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")
+                
.accept(MediaType.parseMediaType("text/html"))).andExpect(status().isOk())
+                
.andExpect(content().contentTypeCompatibleWith("text/html")).andDo(print())
+                
.andExpect(xpath("//div[@id='maxNumber']/text()").number(56.78))
+                .andExpect(xpath("//div[@id='user-%s']/text()", userId)
+                        .string(equalToIgnoringWhiteSpace(user.getFirstName() 
+ " " + user.getLastName())))
+                
.andExpect(xpath("//div[@id='firstUserId']/text()").string(userId.toString()))
+                .andExpect(xpath("//div[@id='fibonacci']/text()").string("0, 
1, 1, 2, 3, 5, 8, 13"));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d7916709/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 5be6daf..0ddbe27 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
@@ -53,7 +53,7 @@ public class MessageFunctionTest {
     private MockMvc mockMvc;
 
     @Before
-    public void setup() {
+    public void setUp() {
         mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d7916709/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 ed00f5e..81b186b 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
@@ -57,7 +57,7 @@ public class ThemeFunctionTest {
     private MockMvc mockMvc;
 
     @Before
-    public void setup() {
+    public void setUp() {
         mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d7916709/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/eval-function-basic-usages.ftl
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/eval-function-basic-usages.ftl
 
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/eval-function-basic-usages.ftl
new file mode 100644
index 0000000..b5b1dd6
--- /dev/null
+++ 
b/freemarker-spring/src/test/resources/META-INF/web-resources/views/test/model/eval-function-basic-usages.ftl
@@ -0,0 +1,46 @@
+<#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>
+
+<#assign expression="T(java.lang.Math).max(12.34, 56.78)" />
+<div id="maxNumber">${spring.eval(expression)}</div>
+
+<ul>
+  <#list users as user>
+    <li>
+      <#assign expression="user.id" />
+      <div id="user-${spring.eval(expression)!}">
+        <#assign expression="user.firstName + ' ' + user.lastName" />
+        ${spring.eval(expression)!}
+      </div>
+    </li>
+  </#list>
+</ul>
+
+<#assign expression="users[0].id" />
+<div id="firstUserId">${spring.eval(expression)!}</div>
+
+<#assign expression="{0,1,1,2,3,5,8,13}" />
+<#assign numbers=spring.eval(expression) />
+<div id="fibonacci"><#list numbers as number>${number}<#sep>, </#list></div>
+
+</body>
+</html>

Reply via email to