Title: [waffle-scm] [772] trunk/waffle-testing/src/test/webapp/freemarker: WAFFLE-81: Allow view harness to use custom configuration for processors.

Diff

Modified: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewHarness.java (771 => 772)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewHarness.java	2008-07-31 15:42:56 UTC (rev 771)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewHarness.java	2008-08-15 11:39:07 UTC (rev 772)
@@ -5,6 +5,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Map;
+import java.util.Properties;
 
 import org.codehaus.waffle.testing.view.freemarker.FreemarkerProcessor;
 import org.codehaus.waffle.testing.view.sitemesh.SitemeshDecorator;
@@ -21,6 +22,16 @@
         FREEMARKER
     };
 
+    private final Properties configuration;
+
+    public ViewHarness() {
+        this(new Properties());
+    }
+
+    public ViewHarness(Properties configuration) {
+        this.configuration = configuration;
+    }
+
     public String process(String resource, Object controller) {
         return process(typeFor(resource), resource, controller);
     }
@@ -29,19 +40,22 @@
         return processorFor(type).process(resource, controller);
     }
 
+    public ViewProcessor processorFor(String resource) {
+        return processorFor(typeFor(resource));
+    }
+
     public ViewProcessor processorFor(Type type) {
         switch (type) {
             case FREEMARKER:
-                return new FreemarkerProcessor();
+                if (configuration.isEmpty()) {
+                    return new FreemarkerProcessor();
+                }
+                return new FreemarkerProcessor(configuration);
             default:
                 throw new UnknownTemplateTypeException(type.name());
         }
     }
 
-    public ViewProcessor processorFor(String resource) {
-        return processorFor(typeFor(resource));
-    }
-
     public Type typeFor(String resource) {
         if (resource.endsWith(".ftl")) {
             return Type.FREEMARKER;
@@ -59,7 +73,7 @@
     }
 
     /**
-     * Processes a view
+     * Processes a view with default configuration
      * 
      * @param resource the template resource path
      * @param controller the controller instance
@@ -67,7 +81,20 @@
      * @return The processed resource
      */
     public static String processView(String resource, Object controller, boolean debug) {
-        String processed = new ViewHarness().process(resource, controller);
+        return processView(resource, new Properties(), controller, debug);
+    }
+
+    /**
+     * Processes a view with custom configuration
+     * 
+     * @param resource the template resource path
+     * @param configuration the view processor configuration 
+     * @param controller the controller instance
+     * @param debug the debug boolean flag
+     * @return The processed resource
+     */
+    public static String processView(String resource, Properties configuration, Object controller, boolean debug) {
+        String processed = new ViewHarness(configuration).process(resource, controller);
         if (debug) {
             System.out.println(processed);
         }
@@ -75,7 +102,7 @@
     }
 
     /**
-     * Decorates a view with Sitemesh
+     * Decorates a view with Sitemesh and default processor configuration
      * 
      * @param resource the template resource path
      * @param controller the controller instance
@@ -86,7 +113,24 @@
      */
     public static String decorateView(String resource, Object controller, String decoratorsResource,
             String decoratorName, Map<String, Object> decoratorDataModel) {
-        SitemeshDecorator decorator = new SitemeshDecorator(new ViewHarness().processorFor(resource));
+        return decorateView(resource, new Properties(), controller, decoratorsResource, decoratorName,
+                decoratorDataModel);
+    }
+
+    /**
+     * Decorates a view with Sitemesh and custom processor configuration
+     * 
+     * @param resource the template resource path
+     * @param configuration the view processor configuration 
+     * @param controller the controller instance
+     * @param decoratorsResource the Sitemesh decorators resource
+     * @param decoratorName the decorator name
+     * @param decoratorDataModel the decorator data model that can be used to override the processor data model
+     * @return The decorated resource
+     */
+    public static String decorateView(String resource, Properties configuration, Object controller,
+            String decoratorsResource, String decoratorName, Map<String, Object> decoratorDataModel) {
+        SitemeshDecorator decorator = new SitemeshDecorator(new ViewHarness(configuration).processorFor(resource));
         return decorator.decorate(resource, controller, decoratorsResource, decoratorName, decoratorDataModel);
     }
 

Modified: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/freemarker/FreemarkerProcessor.java (771 => 772)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/freemarker/FreemarkerProcessor.java	2008-07-31 15:42:56 UTC (rev 771)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/freemarker/FreemarkerProcessor.java	2008-08-15 11:39:07 UTC (rev 772)
@@ -1,5 +1,6 @@
 package org.codehaus.waffle.testing.view.freemarker;
 
+import static java.lang.Boolean.parseBoolean;
 import static org.codehaus.waffle.Constants.CONTROLLER_KEY;
 import static org.codehaus.waffle.Constants.ERRORS_KEY;
 import static org.codehaus.waffle.Constants.MESSAGES_KEY;
@@ -13,6 +14,7 @@
 import org.codehaus.waffle.testing.view.ViewProcessor;
 import org.codehaus.waffle.validation.DefaultErrorsContext;
 
+import freemarker.ext.beans.BeansWrapper;
 import freemarker.template.Configuration;
 import freemarker.template.ObjectWrapper;
 import freemarker.template.Template;
@@ -27,7 +29,7 @@
     public Configuration configuration;
 
     public FreemarkerProcessor() {
-        this(defaultConfigurationProperties());
+        this(defaultConfiguration());
     }
 
     public FreemarkerProcessor(Properties properties) {
@@ -35,17 +37,19 @@
     }
 
     /**
-     * Creates the default configuration properties, with
+     * Creates the default configuration with
      * 
      * <pre>
-     * properties.setProperty(&quot;templateLoadingPrefix&quot;, &quot;/&quot;);
+     * templateLoadingPrefix='/'
+     * simpleMapWrapper=false
      * </pre>
      * 
      * @return The configuration Properties
      */
-    private static Properties defaultConfigurationProperties() {
+    private static Properties defaultConfiguration() {
         Properties properties = new Properties();
         properties.setProperty("templateLoadingPrefix", "/");
+        properties.setProperty("simpleMapWrapper", "false");
         return properties;
     }
 
@@ -64,7 +68,9 @@
         Configuration configuration = new Configuration();
         configuration.setClassForTemplateLoading(FreemarkerProcessor.class, properties
                 .getProperty("templateLoadingPrefix"));
-        configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
+        BeansWrapper beansWrapper = (BeansWrapper) ObjectWrapper.BEANS_WRAPPER;
+        beansWrapper.setSimpleMapWrapper(parseBoolean(properties.getProperty("simpleMapWrapper")));
+        configuration.setObjectWrapper(beansWrapper);
         return configuration;
     }
 
@@ -92,13 +98,12 @@
     }
 
     /**
-     * Creates an data model for the given controller.
-     * The data model contains:
+     * Creates an data model for the given controller. The data model contains:
      * <ul>
-     *   <li>"base": ""</li>
-     *   <li>"controller": controller instance</li>
-     *   <li>"errors": the default errors context</li>
-     *   <li>"messages": the default messages context</li>
+     * <li>"base": ""</li>
+     * <li>"controller": controller instance</li>
+     * <li>"errors": the default errors context</li>
+     * <li>"messages": the default messages context</li>
      * </ul>
      * 
      * @param controller the controller instance

Added: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/Controller.java (0 => 772)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/Controller.java	                        (rev 0)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/Controller.java	2008-08-15 11:39:07 UTC (rev 772)
@@ -0,0 +1,7 @@
+package org.codehaus.waffle.testing;
+
+public interface Controller {
+
+    void process();
+
+}

Modified: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/ListController.java (771 => 772)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/ListController.java	2008-07-31 15:42:56 UTC (rev 771)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/ListController.java	2008-08-15 11:39:07 UTC (rev 772)
@@ -7,15 +7,15 @@
 /**
  * @author Mauro Talevi
  */
-public class ListController {
+public class ListController implements Controller {
     private List<String> names;
 
     public List<String> getNames() {
         return names;
     }
-    
-    public void list(){
+
+    public void process() {
         names = asList("one", "two");
     }
 
-}
\ No newline at end of file
+}

Added: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/MapController.java (0 => 772)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/MapController.java	                        (rev 0)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/MapController.java	2008-08-15 11:39:07 UTC (rev 772)
@@ -0,0 +1,38 @@
+package org.codehaus.waffle.testing;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Mauro Talevi
+ */
+public class MapController implements Controller {
+    private Map<MapKey, String> names;
+
+    public Map<MapKey, String> getNames() {
+        return names;
+    }
+
+    public void process() {
+        names = new HashMap<MapKey, String>();
+        names.put(new MapKey("one"), "One");
+        names.put(new MapKey("two"), "Two");
+    }
+
+    public static class MapKey {
+        private final String key;
+
+        public MapKey(String key) {
+            this.key = key;
+        }
+
+        public String getKey() {
+            return key;
+        }
+
+        @Override
+        public String toString() {
+            return key;
+        }
+    }
+}

Modified: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/view/ViewHarnessTest.java (771 => 772)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/view/ViewHarnessTest.java	2008-07-31 15:42:56 UTC (rev 771)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/view/ViewHarnessTest.java	2008-08-15 11:39:07 UTC (rev 772)
@@ -9,7 +9,9 @@
 import java.io.IOException;
 import java.util.HashMap;
 
+import org.codehaus.waffle.testing.Controller;
 import org.codehaus.waffle.testing.ListController;
+import org.codehaus.waffle.testing.MapController;
 import org.junit.Test;
 
 /**
@@ -19,31 +21,37 @@
 
     @Test
     public void canProcessFreemarkerView() {
-        ListController controller = new ListController();
-        controller.list();
-        String processed = processView("freemarker/list.ftl", controller, false);
+        assertViewProcessed("freemarker/list.ftl", new ListController());
+        assertViewProcessed("freemarker/map.ftl", new MapController());
+    }
+
+    private void assertViewProcessed(String resource, Controller controller) {
+        controller.process();
+        String processed = processView(resource, controller, false);
         assertTrue(processed.length() > 0);
     }
 
     @Test
     public void canDecorateWithSitemesh() {
         ListController controller = new ListController();
-        controller.list();
-        String decorated = decorateView("freemarker/list.ftl", controller, "WEB-INF/testing-decorators.xml", "layout", new HashMap<String, Object>());
+        controller.process();
+        String decorated = decorateView("freemarker/list.ftl", controller, "WEB-INF/testing-decorators.xml", "layout",
+                new HashMap<String, Object>());
         assertTrue(decorated.length() > 0);
     }
 
     @Test
     public void canExportViews() throws IOException {
         ListController controller = new ListController();
-        controller.list();
+        controller.process();
         File list = new File("target/export/list.html");
         exportView(processView("freemarker/list.ftl", controller, false), list);
         assertTrue(list.exists());
         File decorated = new File("target/export/decorated-list.html");
         HashMap<String, Object> decoratorDataModel = new HashMap<String, Object>();
         decoratorDataModel.put("base", "..");
-        exportView(decorateView("freemarker/list.ftl", controller, "WEB-INF/testing-decorators.xml", "layout", decoratorDataModel), decorated);
+        exportView(decorateView("freemarker/list.ftl", controller, "WEB-INF/testing-decorators.xml", "layout",
+                decoratorDataModel), decorated);
         assertTrue(decorated.exists());
     }
 }

Added: trunk/waffle-testing/src/test/webapp/freemarker/map.ftl (0 => 772)

--- trunk/waffle-testing/src/test/webapp/freemarker/map.ftl	                        (rev 0)
+++ trunk/waffle-testing/src/test/webapp/freemarker/map.ftl	2008-08-15 11:39:07 UTC (rev 772)
@@ -0,0 +1,19 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+<body>
+<form action="" method="post">
+	
+	<h2>Map names</h2>
+	
+    <table>
+        <#assign names=controller.names>
+        <#list names.keySet() as name>
+            <#assign value=names.get(name)>
+            <tr>
+                <td>${name},${value}</td>
+            </tr>
+        </#list>        
+    </table>
+
+</form>
+</body>
+</html>
\ No newline at end of file


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to