Title: [waffle-scm] [599] trunk: Initial commit of waffle-testing module.
Revision
599
Author
mauro
Date
2008-04-01 08:22:43 -0500 (Tue, 01 Apr 2008)

Log Message

Initial commit of waffle-testing module.  

Modified Paths


Added Paths

Diff

Modified: trunk/.classpath (598 => 599)

--- trunk/.classpath	2008-03-28 03:11:16 UTC (rev 598)
+++ trunk/.classpath	2008-04-01 13:22:43 UTC (rev 599)
@@ -22,6 +22,12 @@
 		</attributes>
 	</classpathentry>
 	<classpathentry excluding="**" kind="src" output="waffle-taglib/src/main/resources" path="waffle-taglib/src/main/resources"/>
+	<classpathentry kind="src" output="target-eclipse/classes" path="waffle-testing/src/main/java"/>
+	<classpathentry kind="src" output="target-eclipse/test-classes" path="waffle-testing/src/test/java">
+		<attributes>
+			<attribute name="maven.type" value="test"/>
+		</attributes>
+	</classpathentry>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER/modules/profiles[examples,integration]"/>
 	<classpathentry kind="output" path="target-eclipse/classes"/>

Modified: trunk/pom.xml (598 => 599)

--- trunk/pom.xml	2008-03-28 03:11:16 UTC (rev 598)
+++ trunk/pom.xml	2008-04-01 13:22:43 UTC (rev 599)
@@ -20,6 +20,7 @@
     <module>waffle-mock</module>
     <module>waffle-resources</module>
     <module>waffle-taglib</module>
+    <module>waffle-testing</module>
   </modules>
 
   <dependencyManagement>

Property changes: trunk/waffle-testing

Name: svn:ignore
   + target

Added: trunk/waffle-testing/pom.xml (0 => 599)

--- trunk/waffle-testing/pom.xml	                        (rev 0)
+++ trunk/waffle-testing/pom.xml	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,42 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.codehaus.waffle</groupId>
+    <artifactId>waffle</artifactId>
+    <version>1.1-SNAPSHOT</version>
+  </parent>
+  <packaging>jar</packaging>
+  <artifactId>waffle-testing</artifactId>
+  <name>Waffle Testing</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>waffle-core</artifactId>
+      <version>${pom.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.freemarker</groupId>
+      <artifactId>freemarker</artifactId>   
+      <version>2.3.12</version>
+      <scope>compile</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <testResources>
+      <testResource>
+        <directory>${basedir}/src/test/java</directory>
+        <excludes>
+          <exclude>**/*.java</exclude>
+        </excludes>
+      </testResource>
+      <testResource>
+        <directory>${basedir}/src/test/webapp</directory>
+      </testResource>
+    </testResources>
+    </build>
+</project>
\ No newline at end of file

Added: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/registrar/RegistrarHelper.java (0 => 599)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/registrar/RegistrarHelper.java	                        (rev 0)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/registrar/RegistrarHelper.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,81 @@
+package org.codehaus.waffle.testing.registrar;
+
+import org.codehaus.waffle.bind.DefaultStringTransmuter;
+import org.codehaus.waffle.bind.ognl.OgnlValueConverterFinder;
+import org.codehaus.waffle.context.ContextLevel;
+import org.codehaus.waffle.context.pico.PicoLifecycleStrategy;
+import org.codehaus.waffle.monitor.SilentMonitor;
+import org.codehaus.waffle.registrar.Registrar;
+import org.codehaus.waffle.registrar.pico.DefaultParameterResolver;
+import org.codehaus.waffle.registrar.pico.PicoRegistrar;
+import org.picocontainer.defaults.DefaultPicoContainer;
+import org.picocontainer.monitors.NullComponentMonitor;
+
+/**
+ * Registrar helper class. Retrieves controller instances registered in a Registrar and allows the registration of all
+ * the components in a given context level.
+ * 
+ * @author Mauro Talevi
+ */
+public class RegistrarHelper {
+
+    /**
+     * Returns the registered controller
+     * 
+     * @param registrarType the Class representing the registrar type
+     * @param level the ContextLevel
+     * @param path the path under which the controller is registered
+     * @return The controller instance or <code>null</code> if not found
+     */
+    public Object controllerFor(Class<?> registrarType, ContextLevel level, String path) {
+        DefaultPicoContainer registrarContainer = new DefaultPicoContainer();
+        registerComponentsFor(registrarType, level, registrarContainer);
+        return registrarContainer.getComponentInstance(path);
+    }
+
+    /**
+     * Registers the components for the given registrar and level
+     * 
+     * @param registrarType the Class representing the registrar type
+     * @param level the ContextLevel
+     */
+    public void componentsFor(Class<?> registrarType, ContextLevel level) {
+        registerComponentsFor(registrarType, level, new DefaultPicoContainer());
+    }
+
+    private void registerComponentsFor(Class<?> registrarType, ContextLevel level,
+            DefaultPicoContainer registrarContainer) {
+        Registrar registrar = createRegistrar(registrarContainer, registrarType);
+        switch (level) {
+            case APPLICATION:
+                registrar.application();
+                break;
+            case SESSION:
+                registrar.session();
+                break;
+            case REQUEST:
+                registrar.request();
+                break;
+        }
+    }
+
+    private Registrar createRegistrar(DefaultPicoContainer container, Class<?> type) {
+        String name = type.getName();
+        try {
+            DefaultPicoContainer initContainer = new DefaultPicoContainer();
+            initContainer.registerComponentInstance(container);
+            initContainer.registerComponentImplementation(NullComponentMonitor.class);
+            initContainer.registerComponentImplementation(PicoLifecycleStrategy.class);
+            initContainer.registerComponentImplementation(SilentMonitor.class);
+            initContainer.registerComponentImplementation(PicoRegistrar.class);
+            initContainer.registerComponentImplementation(DefaultParameterResolver.class);
+            initContainer.registerComponentImplementation(DefaultStringTransmuter.class);
+            initContainer.registerComponentImplementation(OgnlValueConverterFinder.class);
+            initContainer.registerComponentImplementation(name, Class.forName(name));
+            return (Registrar) initContainer.getComponentInstance(name);
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to create Registrar for " + name, e);
+        }
+    }
+
+}

Added: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewHarness.java (0 => 599)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewHarness.java	                        (rev 0)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewHarness.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,59 @@
+package org.codehaus.waffle.testing.view;
+
+import org.codehaus.waffle.testing.view.freemarker.FreemarkerProcessor;
+
+/**
+ * Facade that enable processing of views by different template engines. The choice of engine is made based on the
+ * extension of the template resource, eg .ftl will trigger use of Freemarker processor.
+ * 
+ * @author Mauro Talevi
+ */
+public class ViewHarness {
+
+    enum Template {
+        FREEMARKER
+    };
+
+    public String process(String resource, Object controller) {
+        Template template = templateFor(resource);
+        switch (template) {
+            case FREEMARKER:
+                FreemarkerProcessor processor = new FreemarkerProcessor();
+                return processor.process(resource, controller);
+            default:
+                throw new UnknownTemplateTypeException(resource);
+        }
+    }
+
+    private Template templateFor(String resource) {
+        if (resource.endsWith(".ftl")) {
+            return Template.FREEMARKER;
+        }
+        throw new UnknownTemplateTypeException(resource);
+    }
+
+    @SuppressWarnings("serial")
+    private static final class UnknownTemplateTypeException extends RuntimeException {
+
+        public UnknownTemplateTypeException(String message) {
+            super(message);
+        }
+
+    }
+
+    /**
+     * Static entry point to ViewHarness
+     * 
+     * @param resource the template resource path
+     * @param controller the controller instance
+     * @param debug the debug boolean flag 
+     * @return The processed resource
+     */
+    public static String processView(String resource, Object controller, boolean debug) {
+        String processed = new ViewHarness().process(resource, controller);
+        if (debug) {
+            System.out.println(processed);
+        }
+        return processed;
+    }
+}

Added: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewProcessor.java (0 => 599)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewProcessor.java	                        (rev 0)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/ViewProcessor.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,21 @@
+package org.codehaus.waffle.testing.view;
+
+/**
+ * ViewProcessor abstracts the view processing implemented by different template engines, eg Freemarker, Velocity etc.
+ * 
+ * The view expects a single controller instance under key "controller".
+ * 
+ * @author Mauro Talevi
+ */
+public interface ViewProcessor {
+
+    /**
+     * Processes view content with given controller
+     * 
+     * @param resource the template resource
+     * @param controller the controller instance
+     * @return The processed content
+     */
+    String process(String resource, Object controller);
+
+}

Added: trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/freemarker/FreemarkerProcessor.java (0 => 599)

--- trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/freemarker/FreemarkerProcessor.java	                        (rev 0)
+++ trunk/waffle-testing/src/main/java/org/codehaus/waffle/testing/view/freemarker/FreemarkerProcessor.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,101 @@
+package org.codehaus.waffle.testing.view.freemarker;
+
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.codehaus.waffle.testing.view.ViewProcessor;
+
+import freemarker.template.Configuration;
+import freemarker.template.ObjectWrapper;
+import freemarker.template.Template;
+
+/**
+ * Freemarker template processor
+ * 
+ * @author Mauro Talevi
+ */
+public class FreemarkerProcessor implements ViewProcessor {
+
+    private Configuration configuration;
+
+    public FreemarkerProcessor() {
+        this(defaultConfigurationProperties());
+    }
+
+    public FreemarkerProcessor(Properties properties) {
+        this.configuration = configure(properties);
+    }
+
+    /**
+     * Creates the default configuration properties, with
+     * 
+     * <pre>
+     * properties.setProperty(&quot;templateLoadingPrefix&quot;, &quot;/&quot;);
+     * </pre>
+     * 
+     * @return The configuration Properties
+     */
+    private static Properties defaultConfigurationProperties() {
+        Properties properties = new Properties();
+        properties.setProperty("templateLoadingPrefix", "/");
+        return properties;
+    }
+
+    /**
+     * Creates a configuration which retrieve resources from classpath
+     * 
+     * @param properties the initialisation Properties
+     * @return The Configuration
+     */
+    private Configuration configure(Properties properties) {
+        if (properties == null || !properties.containsKey("templateLoadingPrefix")) {
+            throw new RuntimeException(
+                    "Freemarker configuration properties requires property 'templateLoadingPrefix': " + properties,
+                    null);
+        }
+        Configuration configuration = new Configuration();
+        configuration.setClassForTemplateLoading(FreemarkerProcessor.class, properties
+                .getProperty("templateLoadingPrefix"));
+        configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
+        return configuration;
+    }
+
+    public String process(String resource, Object controller) {
+        return process(resource, createDataModel(controller));
+    }
+
+    /**
+     * Processes template content with given data model
+     * 
+     * @param resource the template resource
+     * @param dataModel the Map<String, Object> holding the data model
+     * @return The processed content
+     */
+    public String process(String resource, Map<String, Object> dataModel) {
+        try {
+            Template template = configuration.getTemplate(resource);
+            StringWriter writer = new StringWriter();
+            template.process(dataModel, writer);
+            return writer.toString();
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to process Freemaker template " + resource + " with data model  "
+                    + dataModel, e);
+        }
+    }
+
+    /**
+     * Creates an data model for the given controller
+     * 
+     * @param controller the controller instance
+     * @return A Map<String, Object>
+     */
+    public Map<String, Object> createDataModel(Object controller) {
+        Map<String, Object> model = new HashMap<String, Object>();
+        model.put("base", "/");
+        model.put("controller", controller);
+        return model;
+    }
+
+}

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

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/ListController.java	                        (rev 0)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/ListController.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,21 @@
+package org.codehaus.waffle.testing;
+
+import static java.util.Arrays.asList;
+
+import java.util.List;
+
+/**
+ * @author Mauro Talevi
+ */
+public class ListController {
+    private List<String> names;
+
+    public List<String> getNames() {
+        return names;
+    }
+    
+    public void list(){
+        names = asList("one", "two");
+    }
+
+}
\ No newline at end of file

Added: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/registrar/MyRegistrar.java (0 => 599)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/registrar/MyRegistrar.java	                        (rev 0)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/registrar/MyRegistrar.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,21 @@
+package org.codehaus.waffle.testing.registrar;
+
+import org.codehaus.waffle.registrar.AbstractRegistrar;
+import org.codehaus.waffle.registrar.Registrar;
+import org.codehaus.waffle.testing.ListController;
+
+/**
+ * @author Mauro Talevi
+ */
+public class MyRegistrar extends AbstractRegistrar {
+
+    public MyRegistrar(Registrar delegate) {
+        super(delegate);
+    }
+
+    @Override
+    public void application() {
+        register("list", ListController.class);
+    }
+
+}

Added: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/registrar/RegistrarHelperTest.java (0 => 599)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/registrar/RegistrarHelperTest.java	                        (rev 0)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/registrar/RegistrarHelperTest.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,25 @@
+package org.codehaus.waffle.testing.registrar;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.codehaus.waffle.context.ContextLevel;
+import org.junit.Test;
+
+/**
+ * @author Mauro Talevi
+ */
+public class RegistrarHelperTest {
+
+    @Test
+    public void canRegisterComponentsAtDifferentLevels() {
+        RegistrarHelper helper = new RegistrarHelper();
+        helper.componentsFor(MyRegistrar.class, ContextLevel.APPLICATION);
+    }
+
+    @Test
+    public void canRetrieveControllers() {
+        RegistrarHelper helper = new RegistrarHelper();
+        assertNotNull(helper.controllerFor(MyRegistrar.class, ContextLevel.APPLICATION, "list"));
+    }
+
+}

Added: trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/view/ViewHarnessTest.java (0 => 599)

--- trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/view/ViewHarnessTest.java	                        (rev 0)
+++ trunk/waffle-testing/src/test/java/org/codehaus/waffle/testing/view/ViewHarnessTest.java	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,21 @@
+package org.codehaus.waffle.testing.view;
+
+import static org.codehaus.waffle.testing.view.ViewHarness.processView;
+
+import org.codehaus.waffle.testing.ListController;
+import org.junit.Test;
+
+/**
+ * @author Mauro Talevi
+ */
+public class ViewHarnessTest {
+
+    @Test
+    public void canProcessFreemarkerTemplate() {
+        ListController controller = new ListController();
+        controller.list();
+        processView("freemarker/list.ftl", controller, false);
+    }
+
+
+}

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

--- trunk/waffle-testing/src/test/webapp/freemarker/list.ftl	                        (rev 0)
+++ trunk/waffle-testing/src/test/webapp/freemarker/list.ftl	2008-04-01 13:22:43 UTC (rev 599)
@@ -0,0 +1,17 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+<body>
+<form action="" method="post">
+	
+	<h2>List names</h2>
+	
+    <table>
+        <#list controller.names as name>
+            <tr>
+                <td>${name}</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