freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [11/16] incubator-freemarker git commit: FREEMARKER-55: Adding FreemarkerViewResolverTest
Date Tue, 04 Jul 2017 07:44:22 GMT
FREEMARKER-55: Adding FreemarkerViewResolverTest


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/4088f91f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/4088f91f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/4088f91f

Branch: refs/heads/3
Commit: 4088f91f62b678d2c4bebc25375d7fc822f47420
Parents: b375e14
Author: Woonsan Ko <woonsan@apache.org>
Authored: Sun Jul 2 02:22:52 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Sun Jul 2 02:22:52 2017 -0400

----------------------------------------------------------------------
 .../spring/web/view/AbstractFreemarkerView.java | 13 +--
 .../spring/web/view/FreemarkerViewResolver.java | 22 +++++
 .../web/view/FreemarkerViewResolverTest.java    | 98 ++++++++++++++++++++
 .../spring/web/view/FreemarkerViewTest.java     |  2 +-
 4 files changed, 124 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4088f91f/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
index 38c2e3e..cb13ac4 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
@@ -39,7 +39,8 @@ public abstract class AbstractFreemarkerView extends AbstractTemplateView
{
 
     private Configuration configuration;
     private ObjectWrapperAndUnwrapper objectWrapper;
-    private String name;
+
+    // TODO: how to determine locale, customLookupCondition and ignoreMissing? From viewResolver
settings?
     private Locale locale;
     private Serializable customLookupCondition;
     private boolean ignoreMissing;
@@ -60,14 +61,6 @@ public abstract class AbstractFreemarkerView extends AbstractTemplateView
{
         this.objectWrapper = objectWrapper;
     }
 
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
     public Locale getLocale() {
         return locale;
     }
@@ -100,7 +93,7 @@ public abstract class AbstractFreemarkerView extends AbstractTemplateView
{
 
     protected Template getTemplate()
             throws TemplateNotFoundException, MalformedTemplateNameException, ParseException,
IOException {
-        return getConfiguration().getTemplate(getName(), getLocale(), getCustomLookupCondition(),
isIgnoreMissing());
+        return getConfiguration().getTemplate(getUrl(), getLocale(), getCustomLookupCondition(),
isIgnoreMissing());
     }
 
     protected abstract TemplateHashModel createModel(Map<String, Object> map,

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4088f91f/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
index ecacd42..5f7d6fb 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
@@ -39,6 +39,8 @@ public class FreemarkerViewResolver extends AbstractTemplateViewResolver
impleme
     private ServletContextHashModel servletContextModel;
     private TaglibFactory taglibFactory;
 
+    private String normalizedPrefix;
+
     public FreemarkerViewResolver() {
         setViewClass(requiredViewClass());
     }
@@ -58,6 +60,19 @@ public class FreemarkerViewResolver extends AbstractTemplateViewResolver
impleme
     }
 
     @Override
+    public void setPrefix(String prefix) {
+        super.setPrefix(prefix);
+
+        final String prefixValue = getPrefix();
+
+        if (prefixValue.startsWith("/")) {
+            normalizedPrefix = prefixValue.substring(1);
+        } else {
+            normalizedPrefix = prefixValue;
+        }
+    }
+
+    @Override
     public void afterPropertiesSet() throws Exception {
         if (configuration == null) {
             throw new IllegalStateException("Configuration is not set.");
@@ -101,6 +116,13 @@ public class FreemarkerViewResolver extends AbstractTemplateViewResolver
impleme
     @Override
     protected AbstractUrlBasedView buildView(String viewName) throws Exception {
         FreemarkerView view = (FreemarkerView) super.buildView(viewName);
+        final String url;
+        if (normalizedPrefix != null) {
+            url = normalizedPrefix + viewName + getSuffix();
+        } else {
+            url = viewName + getSuffix();
+        }
+        view.setUrl(url);
         view.setConfiguration(configuration);
         view.setObjectWrapper(objectWrapper);
         view.setPageContextServlet(pageContextServlet);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4088f91f/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolverTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolverTest.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolverTest.java
new file mode 100644
index 0000000..7baf7f2
--- /dev/null
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolverTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.web.view;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.freemarker.core.Configuration;
+import org.apache.freemarker.core.templateresolver.impl.StringTemplateLoader;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockServletContext;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.GenericWebApplicationContext;
+
+public class FreemarkerViewResolverTest {
+
+    private ServletContext servletContext;
+    private GenericWebApplicationContext applicationContext;
+
+    private StringTemplateLoader templateLoader;
+    private Configuration configuration;
+
+    private FreemarkerViewResolver viewResolver;
+
+    private String prefix = "/WEB-INF/freemarker/";
+    private String normalizedPrefix = "WEB-INF/freemarker/";
+    private String suffix = ".ftl";
+
+    @Before
+    public void setUp() throws Exception {
+        servletContext = new MockServletContext();
+
+        applicationContext = new GenericWebApplicationContext(servletContext);
+        applicationContext.refresh();
+        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
applicationContext);
+
+        templateLoader = new StringTemplateLoader();
+        templateLoader.putTemplate(normalizedPrefix + "hello" + suffix, "Hello, World!");
+
+        configuration = new Configuration.Builder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS)
+                .templateLoader(templateLoader).build();
+
+        viewResolver = new FreemarkerViewResolver();
+        viewResolver.setServletContext(servletContext);
+        viewResolver.setApplicationContext(applicationContext);
+        viewResolver.setConfiguration(configuration);
+        viewResolver.setPrefix(prefix);
+        viewResolver.setSuffix(suffix);
+        viewResolver.afterPropertiesSet();
+    }
+
+    @Test
+    public void testViewResolver() throws Exception {
+        MockHttpServletRequest request = new MockHttpServletRequest("GET", "/mytest.do");
+        request.setContextPath("/mycontext");
+        request.setServletPath("/myservlet");
+
+        MockHttpServletResponse response = new MockHttpServletResponse();
+
+        FreemarkerView view = resolveFreemarkerView("hello", null);//Locale.ENGLISH);
+        Map<String, Object> model = new HashMap<String, Object>();
+        view.render(model, request, response);
+
+        assertEquals("Hello, World!", response.getContentAsString());
+    }
+
+    private FreemarkerView resolveFreemarkerView(final String name, final Locale locale)
throws Exception {
+        FreemarkerView view = (FreemarkerView) viewResolver.resolveViewName(name, locale);
+        view.setServletContext(servletContext);
+        view.setApplicationContext(applicationContext);
+        return view;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4088f91f/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
index 4a6f4b1..32862c5 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
@@ -156,7 +156,7 @@ public class FreemarkerViewTest {
         view.setServletContextModel(viewResolver.getServletContextModel());
         view.setTaglibFactory(viewResolver.getTaglibFactory());
 
-        view.setName(name);
+        view.setUrl(name);
 
         return view;
     }


Mime
View raw message